GoogleCloudPlatform/cloud-sql-go-connector

Lazy refresh should delay RSA key generation until first connection attempt

Closed this issue · 3 comments

Feature Description

When running in lazy refresh mode, the dialer should generate an RSA key only when a connection attempt arrives. Before that it should do nothing.

Sample code

No response

Alternatives Considered

No response

Additional Details

No response

This would cause the first database connection attempt could take 2-3 seconds under some circumstances.

The impact on time to make the first database connection based on the 4 permutations of "When the app instance starts" X "When the connector computes the key"

There are two options for when the connector should compute the private key:

  • Calculate key on startup
  • Calculate key on first request

There are two options for when the application instance is started:

  • Before a request arrives
  • After a request arrives
Startup before request Startup after request
Key on startup no impact + ~2 seconds
Key on first request + ~2 seconds + ~2 seconds

If the application is started BEFORE the first request arrives, then there is no impact on the connect time. If the application is started AFTER the first request arrives, then the first request will take 2-3 seconds regardless of when the key is calculated.

The code to calculate the key on startup is much simpler than handling the concurrent access issues to calculate it on first request.

I wonder if computing the key on startup is a more flexible way to go.

I wonder if computing the key on startup is a more flexible way to go.

That's what we do today. However, it introduces more cost to a cold start (as in Cloud Run).

Apparently, lazy initialization of globals is a cloud functions best practice https://cloud.google.com/functions/docs/bestpractices/tips#do_lazy_initialization_of_global_variables