Should you leave Redis open or open and exit after every use in node?

I have a socket.io server using redis called "server.js" that starts the node server. This is currently something like this:

var client = redis.createClient() var io = require('socket.io').listen(80); io.sockets.on('connection', function (socket) { client.set(); // do something with redis }); 

Then I start my server and it just stays alive. It is not right? Should it be like that?

 var io = require('socket.io').listen(80); io.sockets.on('connection', function (socket) { var client = redis.createClient() client.set(); // do something with redis client.quit(); }); 

Should I continue to open and close redis, or can I just open it once and leave it open? Which of the above snippets is the right way to start the server?

+8
source share
1 answer

The first is the preferred syntax because you do not want to create a new redis connection every time clients connect to Socket.IO. If you have 1000 users connected, do you want to have 1000 connections to Redis or just one (normally, maybe more, since you will create more servers)?

As @racar suggested, you should also take a look at this question:

How to reuse redis connection in socket.io?

+2
source share

All Articles