7

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?

Talenel
  • 422
  • 2
  • 6
  • 25
alish
  • 93
  • 1
  • 6

2 Answers2

11

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

Niroshan Ranapathi
  • 3,007
  • 1
  • 25
  • 35
  • @alish, you have marked this as the correct answer. Could please share how much it has improved in performance? You had 3 sec - 12 sec, what do you have now? – Zanon Feb 17 '17 at 10:53
  • Initially it was taking 7-8 seconds in my case. Now execution is around 2-3 seconds. Thank you for the solution. – Dikshit Kathuria Sep 16 '19 at 05:59
0

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.

  • You have any documentation to back your answer up? I can't find any. I thought anything left in the event loop would run on the next invocation. – Alex May 09 '17 at 18:20
  • Which part? The context functions were part of the AWS Lambda Programming Model (http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-using-old-runtime.html#nodejs-prog-model-oldruntime-context-methods), but though it is outdated it still exists for backwards compatibility and you may test this point out. The `setTimeout` delay is a undocumented trivia. – Rad Aragón May 15 '17 at 18:41