一个关于Redis 的疑问

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/harryhare/article/details/83041015

问题

既然redis 是单线程的,那么为什么redis 客户端中要有连接池这种东西?

看到这个问题也有其他人问:

思考

我们知道连接池的一般好处,不用再每次使用时都重新建立连接。省去了开始的三次握手(和结尾的四次握手?)。但是如果我们使用一个常开的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多路复用?

猜你喜欢

转载自blog.csdn.net/harryhare/article/details/83041015