Skip to content

feat(cloud-sql): enable lazy refresh for postgres, mysql and sql server connectors#13146

Merged
jackwotherspoon merged 7 commits into
mainfrom
csql-lazy-refresh-feat
Feb 11, 2025
Merged

feat(cloud-sql): enable lazy refresh for postgres, mysql and sql server connectors#13146
jackwotherspoon merged 7 commits into
mainfrom
csql-lazy-refresh-feat

Conversation

@kgala2

@kgala2 kgala2 commented Feb 10, 2025

Copy link
Copy Markdown
Contributor

Enabled lazy refresh for the Python Cloud SQL connector (Postgres, MySQL, and SQL Server).

@kgala2
kgala2 requested review from a team as code owners February 10, 2025 21:08
@product-auto-label product-auto-label Bot added samples Issues that are directly related to samples. api: cloudsql labels Feb 10, 2025
refresh_strategy = RefreshStrategy.LAZY

connector = Connector(ip_type)
connector = Connector(ip_type=ip_type, refresh_strategy=refresh_strategy)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: there is no need to set refresh_strategy as it is only used in one place.

The Cloud SQL Python Connector also allows taking a str for refresh_strategy instead of the custom type.
If we use "lazy" it is a bit more user friendly for the sample as customers will not need to import the custom type of RefreshStrategy 😄

Let's also add to the existing comment to explain why we are setting it.

Something along the lines of "Setting 'refresh_strategy' argument to "lazy" enables lazy refresh mode which is recommended for serverless environments to avoid background refreshes from throttling CPU."

Suggested change
connector = Connector(ip_type=ip_type, refresh_strategy=refresh_strategy)
connector = Connector(ip_type=ip_type, refresh_strategy="lazy")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same applies to other engine types.

@iennae iennae left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit - I think the comment might be a little misleading because it comes across that it's only recommended for serverless environments; AFAIK, it's less useful in non-serverless environments because those instances are long lived so the performance gains may not be as pronounced but it's perfectly ok to use in non-serverless environments as well.

@jackwotherspoon jackwotherspoon changed the title feat: enable lazy refresh for postgres, mysql and sql server connectors feat(cloud-sql): enable lazy refresh for postgres, mysql and sql server connectors Feb 11, 2025
@jackwotherspoon

Copy link
Copy Markdown
Contributor

AFAIK, it's less useful in non-serverless environments because those instances are long lived so the performance gains may not be as pronounced but it's perfectly ok to use in non-serverless environments as well.

This is correct, it is perfectly acceptable to use refresh_strategy="lazy" in non-serverless environments. It just means that the first connection attempt after the "cache" has expired will be slightly less performant as it blocks on a "refresh".

I am fine with removing the comment to make the sample more robust and less confusing. 😄

@jackwotherspoon
jackwotherspoon merged commit d99c471 into main Feb 11, 2025
@jackwotherspoon
jackwotherspoon deleted the csql-lazy-refresh-feat branch February 11, 2025 19:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: cloudsql samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants