alish
alish

Reputation: 93

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?

Upvotes: 7

Views: 1281

Answers (2)

Rad Aragón
Rad Aragón

Reputation: 43

You may use the old context.done function to return immediately or more specifically context.succeed/context.fail. This function is still available on Node 4.

Though it doesn't abruptly ends the running Lambda but gives a response to the caller (like API Gateway) and keep running on the background, if needed, for at most ~15 seconds.

Funny extra: if you schedules a function to run a little later using setTimeout you have those ~15 seconds to run free of charge, because Lambda only holds charge to explicitly asynchronous functions calls.

Upvotes: 0

Niroshan Ranapathi
Niroshan Ranapathi

Reputation: 3047

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

Upvotes: 11

Related Questions