Reuse of database connections by a lambda.

0

Hello, We are having problems because the database is getting overwhelmed because all the database connections are being consumed by a lambda. I would like to know the best practices to avoid consuming all the connections. It is a development environment so provisioning the database with another higher instance type with more VCPU doesn't make sense. I do not really understand the parallelism of the lambdas. What we assumed so far:

  1. A request is made --> lambda X is raised --> a connection is created.
  2. Will requests executed on the hot lambda in an already started environment reuse the same database connection?
  3. For each "provisioning of a new environment" (init in the image), a connection is created? Is this statement correct?

Enter image description here

  1. When would the connection be released if the connection is not explicitly closed? Will the db-connection to the Lambda death event be released? Is it better practice to close the connection explicitly or to leave it open? Is there a cost to open and close connections for each request processed?

https://docs.aws.amazon.com/lambda/latest/dg/lambda-concurrency.html https://stackoverflow.com/questions/70317250/should-i-close-an-rds-proxy-connection-inside-an-aws-lambda-function

1 Answer
1

Hi,

To maximize efficiency and simplify RDS connection sharing with Lambdas, it is recommended to use the RDS Proxy: a good starting point is https://docs.aws.amazon.com/lambda/latest/dg/services-rds.html

Also, this best practice blog has a section on RDS Proxy: https://medium.com/platform-engineer/aws-lambda-performance-best-practices-50968e5bb075

Best,

Didier

profile pictureAWS
EXPERT
answered 2 months ago
profile picture
EXPERT
reviewed 2 months ago
profile pictureAWS
EXPERT
iBehr
reviewed 2 months ago
  • Would it be needed to close the connections with the RDS proxy? Would implementing the proxy properly involve code changes or just change the endopoint ?

  • Hi Diego, no, management of sessions is done by the proxy, which multiplexes their use by different requesters to maximize efficiency.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions