Node.js + Express + Redis, when to close connection? Node.js + Express + Redis, when to close connection? express express

Node.js + Express + Redis, when to close connection?


You have a couple options.

  1. Be lazy and set an idle timeout for all clients on the redis server. Then when a client is idle for too long the server would just kill their connection.
  2. Kill the connection when the node process exits.

.

process.on("exit", function(){    redisClient.quit();});


: The question was 3 years ago and you might get answer already, anyway this might be useful for some new people.

You can make nodejs process listens to SIGINT which is interrupt sent from keyboard via the following code

process.on('SIGINT', function() {    redisClient.quit();    console.log('redis client quit');});

Then you can either Ctrl+C from the keyboard, or send such signal via kill -SIGINT <process id>. When that happens, that code will be executed.

This is when we don't know exactly when to quit redis client, or we want external control to cleanly quit/clear resource we was using.