Node.js Redis Connection Pooling
Asked Answered
U

2

30

When using node_redis Node.js module with Redis, should I just use one connection as Redis is single thread process or shall I create a pool of connections to improve performance?

Ugric answered 23/2, 2014 at 23:37 Comment(1)
A doubt, why do you need connection poolMollymollycoddle
Y
27

Just use a single connection. Both Node and Redis are effectively single thread. I don't think you'll gain anything by having multiple connections. I asked a similar question before starting to develop with Redis and it seems that one client/one application is pretty effective pattern.

Highlighting an important note from the comments on this question. (Thanks to oskarth & stockholmux)

For long running processes like servers, it is okay to use a single long lived connection.

Yttriferous answered 7/3, 2014 at 12:49 Comment(8)
If I'm running a long-lived node web server, should I just create a client and let it live, without ever running .end or .quit on it?Compeer
@Compeer Yep. I've done this in production for years now. The only reason to call .quit is if you want to gracefully shut down your process for some reason (.end is rather brutal, I avoid it).Yttriferous
If you use PUBLISH/SUBSCRIBE or WATCH then you'll need a separate connection(s).Intrastate
Do I still need pooling If I only use : var batch = redis_client.batch(); batch.set(key, value); batch.publish(key, value); batch.exec();Narcotic
@MuhammadHassan No. You don't need pooling. batch is a language construct and you publishing with publish doesn't block the client.Yttriferous
This answer can be seconded with this page. Node.js with Redis. An Excerpt from the page Since Node.js and Redis are both effectively single threaded there is no need to use multiple client instances or any pooling mechanism save for a few exceptions;Happening
@MasterChief to be fair, I wrote that page you're referring to.Yttriferous
@stockholmux, Didn't knew that sir, well written article. Thoroughly enjoyed it. Cheers.Happening
H
13

There is a use case for pooling multiple connections, actually: blocking commands like BRPOP and SUBSCRIBE.

Haar answered 3/5, 2016 at 13:36 Comment(3)
VPhantom, that's useful information, but please include a complete answer to the OP's question.Nix
I haven't yet used generic-pool with Redis, so I can't formulate a more complete answer. I merely clicked on "add a comment" but it appears that because I am new, what I typed ended up being a full answer. That was not my intent. :(Haar
In deed, blocking commands will block your connection so if you try to do a command such as XADD while you have a blocking XREAD or XREADGROUP your add will be blocked until the reads unblock...Raynard

© 2022 - 2024 — McMap. All rights reserved.