Google-cloud-java: Clarify BigtableDataClient lifespan best practices.

Created on 6 Aug 2019  路  3Comments  路  Source: googleapis/google-cloud-java

Hi, thanks for your work, however I just need some clarification, on the BigtableDataClient usage, but it probably affects other clients though.

The documentation advise to use / limit the lifespan of BigtableDataClient within a try-with-resources block, in order to control the resources that the client may use (because this java construct will call the close method).

try(BigtableDataClient bigtableDataClient = BigtableDataClient.create(bigtableDataSettingsBuilder.build())) {
// ..
}
(2)

Note: close() needs to be called on the bigtableDataClient object to clean up resources such as threads. In the example above, try-with-resources is used, which automatically calls close().
(1)

this usage suggest a one time usage, like for batch jobs. But this doesn't seem appropriate for long running process like a webserver that takes requests and for each request interacts with the BigtableDataClient.
Creating this client creates gRPC related resources, without diving too much in the code my guess is that creating then closing repetitively would hurt quite badly the performance in such context. So I think in this case it would be advised to have a BigtableDataClient with the same lifespan as the application and invoke the close method on the application shutdown.

Is this correct to assume that? Or Is there some other elements to consider? Could you clarify that ?

bigtable question

All 3 comments

Hi @bric3

Yes, you are correct. We should definitely reuse the BigtableDataClient instance till the end of your application lifecycle.

The Javadoc examples are to showcase the basic use-case of each operation individually that's why it is with try-with-resources. In real-world applications, you should consider proper configuration for each operation, which can be done under BigtableDataSettings#stubSettings(). eg:

BigtableDataSettings.newBuilder()
        .stubSettings()
        .readRowsSettings()
        .setRetryableCodes(...)
        .setRetrySettings(...)

You can find more details on defaults values of each operation at EnhancedBigtableStubSettings javadoc.

@igorbernstein2 Please add any points, which I missed or needs to consider for web apps.

You are right client creation is expensive and there should only be a single instance. I've updated the docs in #6037.

Thanks for reporting

Thank you very much @rahulKQL and @igorbernstein2 for the clarification. (And the quick response !)

Was this page helpful?
0 / 5 - 0 ratings