perf: increase pg pool connection idle timeout #1349
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Increasing pg pool connection idle timeout
We're seeing about 1s - 1.5s to establish new connections for Braverly's case with their Singapore database. I cannot explain yet why it is so long, but what we can do is reduce the number of times this needs to occur.
This change increases the idle time before closing a connection in the local pool (from 10s default) to 2 minutes. Establishing a new connection on (almost) every functions query can be expensive, so this will reduce having to open connections as regularly. https://node-postgres.com/apis/pool
NOTE: We should consider setting this to 0 (i.e. never pool locally) and open and close connections with each invocation. This is because the freeze/thaw nature of lambdas can cause problems with long-lived connections - see brianc/node-postgres#2718. Once we're "fully regional" this should not be a performance problem anymore.
Although I doubt we will run into these freeze/thaw issues if idleTimeoutMillis is always shorter than the time is takes for a lambda to freeze (which is not a constant, but could be as short as several minutes, https://www.pluralsight.com/resources/blog/cloud/how-long-does-aws-lambda-keep-your-idle-functions-around-before-a-cold-start)