Search code examples
node.jsaws-lambdaamazon-rdsserverless-frameworkserverless-architecture

AWS Lambda with poor performance when using RDS


I've implemented an AWS Lambda function using the Serverless Framework. That Lambda function is using RDS and MongoDB. The MongoDB endpoint runs around 500ms, but RDS runs on 12 sec (cold start) and ~3 sec (hot start).

Note: I am using Sequelize in this endpoint.

How to speed up my RDS Lambda endpoint?


Solution

  • On the first line after your functions module definition, add the following line

    context.callbackWaitsForEmptyEventLoop = false;
    

    callbackWaitsForEmptyEventLoop

    • The default value is true
    • Useful only to modify the default behavior of the callback.

    You can set this property to false to request AWS Lambda to freeze the process soon after the callback is called, even if there are events in the event loop. AWS Lambda will freeze the process, any state data and the events in the Node.js event loop (any remaining events in the event loop processed when the Lambda function is called next and if AWS Lambda chooses to use the frozen process)

    More details read this article