DEV Community

Discussion on: 6 Keys to Managing Database Load in AWS Lambda

Collapse
 
nakib profile image
Sharifuzzaman Nakib

I am new to the serverless world and I might be wrong but I think opening and closing connections for each invocation is not always good because of the latency of creating a new connection every time. It will be a nightmare for a high number of concurrent requests.

The question you raised can be solved in a better way.

Your first question was.
...when and how connections are closed?

Since we are reusing the connection then they don't need to be closed. Right? Because closing means we need to open another connection later. But we want to reuse the opened connection as many times as possible, that's why we don't close the connection on the container.
But on the database, we can set a max-idle-time after which idle connections will be closed automatically or on some databases, we may create a corn job to do this. This way there will be no idle connection for too much time and the connections will be reused as more as possible.

Your second question was,
...how many connections are opened and where they’re being used?

It depends on the number of serverless functions running at that moment which requires a database connection.
However, we don't need to know exactly how many connections are opened. We just need to manage how many concurrent serverless functions may run at a time and set the limit of database connections accordingly.
We don't need to know where the connections are used. Each serverless function will reuse them as more as possible but if any connections are idle for too much time then the Database will close them.

This will be a huge performance optimization for situations where the number of concurrent requests which triggers serverless function is high since for each connection we don't need to create a new database connection.

Please explain if I am wrong.