【发布时间】:2018-03-21 14:43:49
【问题描述】:
Redis 4.0以后,Redis可以多线程执行一些功能(1.删除后台对象等),但是Redis通常还是使用单线程。 FAQ - Redis
所以我猜生菜没用。 Lettuce 是 Redis 客户端,可以在 1 个连接中使用多个线程,但 Redis 在 1 个连接中只能使用单个线程。
你能推荐 Redis 客户端使用 lettuce 吗?为什么?
【问题讨论】:
Redis 4.0以后,Redis可以多线程执行一些功能(1.删除后台对象等),但是Redis通常还是使用单线程。 FAQ - Redis
所以我猜生菜没用。 Lettuce 是 Redis 客户端,可以在 1 个连接中使用多个线程,但 Redis 在 1 个连接中只能使用单个线程。
你能推荐 Redis 客户端使用 lettuce 吗?为什么?
【问题讨论】:
因为您不仅会在 Redis 执行命令时花费时间,还会在传输数据(发送命令、接收结果)时花费时间。在传输时在单线程模式下,Redis 不起作用。 Redis 工作时,不会发生传输。多个连接或一个流水线连接可帮助您使带宽和 CPU 周期达到饱和。
而 luttece 不仅与速度有关。它还可以帮助您使用异步和响应式 API 更好地组织代码。
回到性能主题,here 是一个简单的基准测试,可以让您大致了解线程和池的影响。请注意,虽然池有点慢(您在池操作上花费了一些时间),但它允许您隔离操作(因此错误不会影响其他线程)并使用 MULTI 和阻塞命令。
这是我的结果(本地系统有 4 个核心,远程系统 CPU 大约慢 2 倍):
Benchmark (address) Mode Cnt Score Error Units
LettuceThreads.pooled socket thrpt 25 35389.995 ± 1325.198 ops/s
LettuceThreads.pooled localhost thrpt 25 32075.870 ± 416.220 ops/s
LettuceThreads.pooled remote thrpt 25 3883.193 ± 67.622 ops/s
LettuceThreads.shared socket thrpt 25 39419.772 ± 1966.023 ops/s
LettuceThreads.shared localhost thrpt 25 34293.245 ± 1737.349 ops/s
LettuceThreads.shared remote thrpt 25 3919.251 ± 49.897 ops/s
Benchmark (address) Mode Cnt Score Error Units
LettuceThreads.pooled socket thrpt 25 56938.187 ± 2727.772 ops/s
LettuceThreads.pooled localhost thrpt 25 49420.748 ± 2091.631 ops/s
LettuceThreads.pooled remote thrpt 25 7791.706 ± 133.507 ops/s
LettuceThreads.shared socket thrpt 25 81195.900 ± 1593.424 ops/s
LettuceThreads.shared localhost thrpt 25 78404.688 ± 3878.044 ops/s
LettuceThreads.shared remote thrpt 25 3992.023 ± 39.092 ops/s
Benchmark (address) Mode Cnt Score Error Units
LettuceThreads.pooled socket thrpt 25 87345.126 ± 8149.009 ops/s
LettuceThreads.pooled localhost thrpt 25 75003.031 ± 4481.289 ops/s
LettuceThreads.pooled remote thrpt 25 15807.410 ± 225.376 ops/s
LettuceThreads.shared socket thrpt 25 169112.489 ± 3749.897 ops/s
LettuceThreads.shared localhost thrpt 25 115464.778 ± 5099.728 ops/s
LettuceThreads.shared remote thrpt 25 7952.492 ± 133.521 ops/s
您可以在此处看到性能与线程数的比例非常好,因此生菜并非无用。
【讨论】:
var client = RedisClient.create(uri); var config = new GenericObjectPoolConfig(); config.setMaxTotal(5); var pool = ConnectionPoolSupport.createGenericObjectPool( client::connect, new GenericObjectPoolConfig());