Redis是单线程的,那我为什么要使用lettuce呢?

5

在 Redis 4.0 之后,Redis 可以执行一些多线程函数(例如:1. 在后台删除对象等),但 Redis 仍然通常使用单线程。FAQ - Redis

所以我猜 lettuce 是无用的。Lettuce 是一个 Redis 客户端,可以在 1 个连接中使用多个线程,但 Redis 在 1 个连接中只能使用单个线程。

你能推荐使用 lettuce 作为 Redis 客户端吗?为什么?

1个回答

5

因为您不仅在Redis执行命令时花费时间,还在传输数据(发送命令、接收结果)时花费时间。在单线程模式下,当您传输数据时,Redis无法工作。而当Redis工作时,就没有数据传输。多个连接或一个流水线连接可帮助您饱和带宽和CPU周期。

而Luttece不仅关乎速度,还通过异步和反应式API帮助您更好地组织代码。

回到性能问题,这里有一个简单的基准测试,可以让您大致了解线程和池对性能的影响。请注意,虽然池略慢(您需要一些时间进行池操作),但它允许您隔离操作(使错误不影响其他线程)并使用MULTI和阻塞命令。

以下是我的结果(本地系统有4个核心,远程系统的CPU大约慢2倍):

Threads=1

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

线程数=2

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

线程数=4

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

您可以看到,性能随着线程数量的增加而显著提高,因此lettuce并非无用。

1
请问您能否分享一下配置Lettuce连接池连接数的示例代码?我找不到任何好的资料。 - user1571307
1
var config = new GenericObjectPoolConfig(); config.setMaxTotal(5); var pool = ConnectionPoolSupport.createGenericObjectPool( client::connect, new GenericObjectPoolConfig());``` - Imaskar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接