4

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.

1

2 Answers 2

7

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:

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Another way to tackle this kind of issue is to user an AWS RDS Proxy: https://aws.amazon.com/fr/rds/proxy/

Many applications, including those built on modern serverless architectures, can have a large number of open connections to the database server, and may open and close database connections at a high rate, exhausting database memory and compute resources. Amazon RDS Proxy allows applications to pool and share connections established with the database, improving database efficiency and application scalability. With RDS Proxy, failover times for Aurora and RDS databases are reduced by up to 66% and database credentials, authentication, and access can be managed through integration with AWS Secrets Manager and AWS Identity and Access Management (IAM).

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.