Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reuse Redis connections for NodeJS Lambda function

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?

like image 256
Ssander Avatar asked Oct 17 '17 15:10

Ssander


2 Answers

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

like image 152
Justin Kruse Avatar answered Sep 20 '22 20:09

Justin Kruse


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.

like image 42
Risto Novik Avatar answered Sep 21 '22 20:09

Risto Novik