问题
既然redis 是单线程的,那么为什么redis 客户端中要有连接池这种东西?
看到这个问题也有其他人问:
- segmentfault: redis连接池问题?
- groups.google:Redis connection pooling - useful or not?
- groups.google:Can we use connection pool in Redis?
- 这里有明确支持单链接的回答 StackOverflow:Node.js Redis Connection Pooling
思考
我们知道连接池的一般好处,不用再每次使用时都重新建立连接。省去了开始的三次握手(和结尾的四次握手?)。但是如果我们使用一个常开的redis连接,同样没有新建连接的开销。
另外有人会提到阻塞的redis 命令(BRPOP),或者 SUBSCRIBE 或者watch,或者有实现时采用同步语义。如果不使用这些命令呢?
还有一个角度是,客户端本身是多线程的,如果用一个redis 连接,会竞争资源,加锁则会影响性能。
上面第三个链接的一个回答(似乎是Jedis 客户端的开发者):
In JRedis you have two options if you are using a front-end that
serves a lot of concurrent processes.
For low (concurrent) number of processes (threads), you can use
JRedisService. This connector maintains a pool of connections to
Redis and cycles through next available connection to serve service
requests.
For very high (concurrent) number of processes (thread count in the
thousands) – specially if you will be maintaining a constant load on
the connector – use the Pipeline with sync() semantics. Pipeline
uses a single connection, but has much higher throughput.
其他
连接池:
redis 连接池的使用
连接池的源码分析(python)
一个相关的问题:
为什么DB连接管理一般不采用IO多路复用?