Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aws lambda function too many connection issue rds

I have more than 20 lambda function for my mobile app API, as in starting the user based is less so it was all going good but now as the user increase (3000 to 4000) I am facing too many connection issues in my lambda function because of which I started getting internal server error form my API, I know i am missing something while creating the connection in lambda but after lot of hit and try i was not able to find out that missing link, the below code I am using for creating the connection

      var con;


      exports.handler = async (event, context) => {
          context.callbackWaitsForEmptyEventLoop = false;
       if (!con || con.state == "disconnected" || con === "undefined") {

    con = secret
        .then((result) => {
            var data = JSON.parse(result.SecretString);
            var connection = mysql.createConnection(
                {
                    "host": data.host,
                    "user": data.username,
                    "password": data.password,
                    "database": data.db
                }
            );
            connection.connect();
            return connection;
        }).catch((err) => {
            throw err;
        });
}

I have tried adding con.destroy() before sending the response, but it does not seem to solve the problem, so if there is anything else I can do then pls let me know.

like image 747
The Coder Avatar asked Sep 14 '25 23:09

The Coder


1 Answers

It's complicated to know exactly what's going on, my first guess always revolves on setting context.callbackWaitsForEmptyEventLoop = false and storing the connection outside the function scope - both which you've correctly done.

With that said, managing connection pools in Lambda kind of goes the other way serverless is by definition, it "lacks" ephemerality. This does not mean that you can't scale with connections, you'll have to dig deeper infos on your issue.

Jeremy Daly provides good practices on dealing with this on the following posts on his blog:

  • Reusing DB connections
  • Managing RDS connections + AWS Lambda

Also, he has made a lib that manages this for you, it's called serverless-mysql - which is built to address this specific issue.

Personal experience: I had trouble with connections + lambdas, and due to this fact I've migrated to their DataAPI solution (I had to migrate my RDS to Aurora Serverless, which is not a big pain) - its GA release was about 2/3 weeks ago. If you want more info on Aurora SLS, check it out here.

like image 83
Guilherme Matuella Avatar answered Sep 16 '25 13:09

Guilherme Matuella