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).
我使用无服务器框架实现了AWS Lambda功能。 Lambda函数使用RDS和MongoDB。 MongoDB端点运行大约500ms,但RDS运行12秒(冷启动)和~3秒(热启动)。
Note: I am using Sequelize in this endpoint.
注意:我在此端点中使用Sequelize。
How to speed up my RDS Lambda endpoint?
如何加速我的RDS Lambda端点?
2 个解决方案
#1
8
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.
默认值是true
仅用于修改回调的默认行为。
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)
您可以将此属性设置为false,以便在调用回调后立即请求AWS Lambda冻结进程,即使事件循环中存在事件也是如此。 AWS Lambda将冻结进程,任何状态数据和Node.js事件循环中的事件(当下一次调用Lambda函数并且AWS Lambda选择使用冻结进程时,处理事件循环中的任何剩余事件)
More details read this article
更多细节阅读本文
#2
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.
您可以使用旧的context.done函数立即返回或更具体地返回context.succeed / context.fail。此功能仍在节点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.
虽然它不会突然结束正在运行的Lambda,但会给调用者(如API网关)提供响应,并在必要时继续在后台运行,最多约15秒。
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.
有趣的额外:如果您使用setTimeout安排一个稍后运行的函数,那么您可以免费运行~15秒,因为Lambda只对显式异步函数调用负责。
#1
8
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.
默认值是true
仅用于修改回调的默认行为。
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)
您可以将此属性设置为false,以便在调用回调后立即请求AWS Lambda冻结进程,即使事件循环中存在事件也是如此。 AWS Lambda将冻结进程,任何状态数据和Node.js事件循环中的事件(当下一次调用Lambda函数并且AWS Lambda选择使用冻结进程时,处理事件循环中的任何剩余事件)
More details read this article
更多细节阅读本文
#2
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.
您可以使用旧的context.done函数立即返回或更具体地返回context.succeed / context.fail。此功能仍在节点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.
虽然它不会突然结束正在运行的Lambda,但会给调用者(如API网关)提供响应,并在必要时继续在后台运行,最多约15秒。
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.
有趣的额外:如果您使用setTimeout安排一个稍后运行的函数,那么您可以免费运行~15秒,因为Lambda只对显式异步函数调用负责。