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

2

21

I have a Node application that uses Express and node_redis. I'm following the approach outlined in the Learning Node book and creating a single client for the life of the application. Given this approach, when do I call close() on the redis client? Do I even need to?

Relevant code

var express = require( 'express' ),
    redis = require( 'redis' );

var app = express(),
    config = require( './config/application' )[ app.get( 'env' ) ];

// create Redis client
var redisClient = redis.createClient();
redisClient.on( 'error', function( err ) {
  console.log( 'Error' + err );
} );
// select the database
redisClient.select( config.redis_database );

...
/* more setup, route defintions, etc. */
...

http.createServer( app ).listen( 4000, function() {
  console.log( 'Server started and ready for action!' );
});
Elsi answered 13/3, 2014 at 18:4 Comment(0)
S
19

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();
});
Storey answered 18/3, 2014 at 0:7 Comment(4)
You can only perform synchronous operations inside "exit" event handler. The process will die before any "quit" command is send to the redis server.Wavellite
close() function doesn't exists. Use redisClient.quit();Janessa
If you are killing the node process anyway, why do you need to explicitly "quit" the redis client? When the node process dies, any connection resources associated with it will be released/disconnected anyway.Burweed
@Phil, u wanna say if I disconnect from my data source, there are no reasons to disconnect redis?Gelding
C
7

: 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.

Cinerary answered 17/8, 2017 at 14:36 Comment(4)
In addition to SIGINT, it might also help to also listen to SIGTERM to handle the kill <process id> commandBarbie
@ifelsemonkey that makes sense too.Cinerary
Ultimately, is there no reason to quit executing each request? I mean, for example, we have a lot of different requests. In some of these requests, we need to get some redis data. Are there no reasons for each request to make a connection and at the end of this request to make a disconnection?Gelding
@Barbie Additional note: SIGTERM is NOT SUPPORTED on Windows. But it can be just listened to. The SIGILL and SIGTERM signals are not generated under Windows. They are included for ANSI compatibility. See microsoft docs and nodejs docsOona

© 2022 - 2024 — McMap. All rights reserved.