How to (re-)use redis client connections in nodejs/express?
Asked Answered
P

3

8

Given a simple example:

var express = require("express")
var redis = require('redis')
var app = express()

var client = redis.createClient()

app.get('/', function(req, res) {
    req.connection.setTimeout(2 * 1000)
    client.set("test", 1, function (err, resp) {
        res.send('Hello World')
    })
})

app.listen(80)

Redis connection doesn't need to be re-established for every request, does it?

Do you need to use redis connection pool?

Project answered 30/4, 2013 at 8:14 Comment(0)
B
4

You only need one connection to redis for your server, and you only need to close it if your server ever stops. Your server just runs as a single process.

Bondwoman answered 30/4, 2013 at 8:29 Comment(3)
If you use pub\sub channels this is false. It's also false if your connection sets up watches for atomic operations. Do not do this.Favianus
Does it also hold good for server that is handling 50K requests per minute? and every request doing atleast 2 Redis operations (GET/SET). Will single connection to Redis suffice?Tangent
@AyazPasha Use "redis-benchmark" command with the operation that you will use, example: redis-benchmark -t ping,set,get -n 50000Lux
M
9

Replying to incarnate's connection pool post:

Hi there -- first clarification, Redis server IS single-threaded.

What you're seeing in terms of command ordering is an artifact of the way you're using async, and unrelated to either node_redis library or Redis itself.

Connection pooling with node_redis is definitely not required. You can certainly use it, but it is not suggested. Connection pooling will reduce the effectiveness of Redis pipelining, and due to the stateful nature of the Redis protocol, can be problematic with some commands.

Reason #1: I'm not sure this is significant, or possibly worse by creating additional clients which are an additional GC burden.

Reason #2: That's not really how it works. The Redis server is single-threaded. If you're waiting on the server, all clients are waiting on the server. If you're blocked in javascript, all clients in that process are blocked in javascript.

Reason #3: The node_redis library already reconnects after failures.

Maegan answered 20/11, 2013 at 5:36 Comment(1)
Thanks for your detailed explanation. The key point I've certainly missed is that Redis IS in fact single-threaded. Thank you again!Mottled
B
4

You only need one connection to redis for your server, and you only need to close it if your server ever stops. Your server just runs as a single process.

Bondwoman answered 30/4, 2013 at 8:29 Comment(3)
If you use pub\sub channels this is false. It's also false if your connection sets up watches for atomic operations. Do not do this.Favianus
Does it also hold good for server that is handling 50K requests per minute? and every request doing atleast 2 Redis operations (GET/SET). Will single connection to Redis suffice?Tangent
@AyazPasha Use "redis-benchmark" command with the operation that you will use, example: redis-benchmark -t ping,set,get -n 50000Lux
E
1

You needn't open and close the connection on every request.

In your example, even if requests would be processe asyncronous, the callback would always be exectued in the right context. But you should be careful with non-atomic transactions, cause they can mess up youre database. use MULTI Command to be aware of this

Exalted answered 30/4, 2013 at 8:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.