Configure connection pools

Some Cloud Bigtable client libraries let you configure the number of gRPCchannels in a client's connection pool, also known as a channel pool.In mostcases, the default configuration is correct, and there is no need to changeit.

Connection pools are automatically resized as needed when you use theCloud Bigtable client library for Java version 2.23.0 or later, and when youuse the Cloud Bigtable HBase client for Java version 2.9.1 or later.

This page describes how to determine the optimal connection pool size for yourapplication, and it presents code snippets that show how to configure theconnection pools.

Before you read this page, read the overview ofBigtable connection pools to learn how theywork and whether you should change yours.

The following client libraries offer connection pooling and let you configurethe number of pools:

Determine the best connection pool size

Ideally, to leave room for traffic fluctuations, a connection pool has abouttwice the number of connections it takes for maximum saturation. Because aconnection can handle a maximum of 100 concurrent requests, between 10 and 50outstanding requests per connection is optimal. This concept is described inmore detail inConnection pools.

Monitor your traffic after making changes and adjust the number ofconnections in your pool if necessary.

The following steps help you calculate the optimal number of connections inyour channel pool using client-side metrics such as those available fromOpenCensus.

  1. From your client-side metrics, gather the following information:
    1. The maximum number of queries per second (QPS) per client when yourapplication is running a typical workload.
    2. The average latency (the response time for a single request) in ms.
  2. Determine the number of requests that you can send serially per second bydividing 1,000 by the average latency value.
  3. Divide the QPS in seconds by the number of serial requests per second.
  4. Divide the result by 50 requests per channel to determine the minimumoptimal channel pool size. (If your calculation is less than 2, use atleast 2 channels anyway, to ensure redundancy.)
  5. Divide the same result by 10 requests per channel to determine the maximumoptimal channel pool size.

These steps are expressed in the following equations:

  • (QPS sec ÷ (1,000 ÷ latency ms)) ÷ 50 streams = Minimum optimalnumber of connections

  • (QPS sec ÷ (1,000 ÷ latency ms)) ÷ 10 streams = Maximum optimalnumber of connections

Example

Your application typically sends 50,000 requests per second, and theaverage latency is 10 ms. Divide 1,000 by 10 ms to determine that you can send100 requests serially per second. Divide that number into 50,000 to get theparallelism needed to send 50,000 QPS: 500. Each channel can have at most100 requests out concurrently, and your target channel utilization is between10 and 50 concurrent streams. Therefore, to calculate the minimum, divide 500by 50 to get 10. To find the maximum, divide 500 by 10 to get 50. This meansthat your channel pool size for this example should be between 10 and 50connections.

Set the pool size

The following code samples demonstrate how to configure the number of poolsin the client libraries that let you set the pool size.

Go

import("context""fmt""io""cloud.google.com/go/bigtable""google.golang.org/api/option")funcconfigureConnectionPool(wio.Writer,projectID,instanceIDstring)error{// projectID := "my-project-id"// instanceID := "my-instance-id"ctx:=context.Background()// Set up Bigtable data operations client.poolSize:=10client,err:=bigtable.NewClient(ctx,projectID,instanceID,option.WithGRPCConnectionPool(poolSize))deferclient.Close()iferr!=nil{returnfmt.Errorf("bigtable.NewClient: %w",err)}fmt.Fprintf(w,"Connected with pool size of %d",poolSize)returnnil}

HBase

This sample is applicable only for client library versions earlier than2.9.1, when automatic resizing was introduced.

import staticcom.google.cloud.bigtable.hbase.BigtableOptionsFactory.BIGTABLE_DATA_CHANNEL_COUNT_KEY;importcom.google.cloud.bigtable.hbase.BigtableConfiguration;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.hbase.client.Connection;publicclassConfigureConnectionPool{publicstaticvoidconfigureConnectionPool(StringprojectId,StringinstanceId){// String projectId = "my-project-id";// String instanceId = "my-instance-id";Configurationconfig=BigtableConfiguration.configure(projectId,instanceId);config.setInt(BIGTABLE_DATA_CHANNEL_COUNT_KEY,10);try(Connectionconnection=BigtableConfiguration.connect(config)){intpoolSize=connection.getConfiguration().getInt(BIGTABLE_DATA_CHANNEL_COUNT_KEY,0);System.out.println(String.format("Connected with pool size of %d",poolSize));}catch(Exceptione){System.out.println("Error during ConfigureConnectionPool: \n"+e.toString());}}}

Java

This sample is applicable only for client library versions earlier than2.23.0, when automatic resizing was introduced.

importcom.google.api.gax.grpc.InstantiatingGrpcChannelProvider;importcom.google.cloud.bigtable.data.v2.BigtableDataClient;importcom.google.cloud.bigtable.data.v2.BigtableDataSettings;importcom.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStubSettings;importjava.io.IOException;publicclassConfigureConnectionPool{publicstaticvoidconfigureConnectionPool(StringprojectId,StringinstanceId){// String projectId = "my-project-id";// String instanceId = "my-instance-id";BigtableDataSettings.BuildersettingsBuilder=BigtableDataSettings.newBuilder().setProjectId(projectId).setInstanceId(instanceId);settingsBuilder.stubSettings().setTransportChannelProvider(EnhancedBigtableStubSettings.defaultGrpcTransportProviderBuilder().setPoolSize(10).build());BigtableDataSettingssettings=settingsBuilder.build();try(BigtableDataClientdataClient=BigtableDataClient.create(settings)){InstantiatingGrpcChannelProviderprovider=(InstantiatingGrpcChannelProvider)settings.getStubSettings().getTransportChannelProvider();intpoolSize=provider.toBuilder().getPoolSize();System.out.println(String.format("Connected with pool size of %d",poolSize));}catch(IOExceptione){System.out.println("Error during ConfigureConnectionPool: \n"+e.toString());}}}

C++

namespacecbt=::google::cloud::bigtable;namespacegc=::google::cloud;[](std::stringconst&project_id,std::stringconst&instance_id,std::stringconst&table_id){autoconstexprkPoolSize=10;autooptions=gc::Options{}.set<gc::GrpcNumChannelsOption>(kPoolSize);cbt::Tabletable(cbt::MakeDataConnection(options),cbt::TableResource(project_id,instance_id,table_id));std::cout <<"Connected with channel pool size of " <<kPoolSize <<"\n";}

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.