I have written the following Lambda function:
exports.handler = (event, context, callback) => {
const redis = require('redis');
const redis_client = redis.createClient({
host: 'hostname',
port: 6379
});
redis_client.set("foo", "bar");
redis_client.get("foo", function(err, reply) {
redis_client.quit();
callback(null, reply);
});
};
This works fine. However, I would like to reuse the Redis connection between Lambda invocations to improve performance. In theory this would be possible by moving the createClient() to outside of the handler. However, because of the "redis_client.quit()" line, that connection is killed. If I do not quit the client, the Lambda function will time out however.
What is the proper way to reuse a Redis in NodeJS when using AWS Lambda?
To avoid the timeout, set callbackWaitsForEmptyEventLoop
to false
in the AWS Context of your Lambda. This way you do not have to close the redis connection and your lambda will not wait for it (or other connections, i.e. db connections) to close before returning:
https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html
As you already mentioned your approach is correct way but you have to keep in mind that your Redis instance has connection limits, example AWS Elasticache maxclients
is set to 65000. AWS currently allows executing 1k Lambdas in parallel so be careful with the external connections.
Currently, there is no silver bullet for Lambda external DB connections. One possible solution would be to create internal web API service which handles the communication between the DB.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With