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?
redis.createClient
? – Fante