Redis 是单线程的。那我为什么要用生菜呢?

Suj*_*hin 5 redis lettuce

Redis 4.0之后,Redis可以多线程执行一些功能(1.删除后台对象等),但Redis通常仍然使用单线程。 常见问题解答-Redis

所以我觉得生菜没什么用。Lettuce 是 Redis 客户端,可以在 1 个连接中使用多个线程,但 Redis 只能在 1 个连接中使用单线程。

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

Ima*_*ica 5

因为您不仅花费时间在 Redis 执行命令上,还花费时间传输数据(发送命令、接收结果)。在单线程模式下传输时,Redis 不起作用。当 Redis 工作时,不会发生任何传输。多个连接或一个管道连接可帮助您使带宽和 CPU 周期饱和。

\n\n

luttece 不仅仅关乎速度。它还可以帮助您使用异步和反应式 API 更好地组织代码。

\n\n

回到性能主题,这里有一个简单的基准测试,可以帮助您大致了解线程和池化的影响。请注意,虽然池化速度有点慢(您在池操作上花费一些时间),但它允许您隔离操作(因此错误不会影响其他线程)以及使用和MULTI阻止命令。

\n\n

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

\n\n

线程=1

\n\n
Benchmark              (address)   Mode  Cnt      Score      Error  Units\nLettuceThreads.pooled     socket  thrpt   25  35389.995 \xc2\xb1 1325.198  ops/s\nLettuceThreads.pooled  localhost  thrpt   25  32075.870 \xc2\xb1  416.220  ops/s\nLettuceThreads.pooled     remote  thrpt   25   3883.193 \xc2\xb1   67.622  ops/s\nLettuceThreads.shared     socket  thrpt   25  39419.772 \xc2\xb1 1966.023  ops/s\nLettuceThreads.shared  localhost  thrpt   25  34293.245 \xc2\xb1 1737.349  ops/s\nLettuceThreads.shared     remote  thrpt   25   3919.251 \xc2\xb1   49.897  ops/s\n
Run Code Online (Sandbox Code Playgroud)\n\n

线程=2

\n\n
Benchmark              (address)   Mode  Cnt      Score      Error  Units\nLettuceThreads.pooled     socket  thrpt   25  56938.187 \xc2\xb1 2727.772  ops/s\nLettuceThreads.pooled  localhost  thrpt   25  49420.748 \xc2\xb1 2091.631  ops/s\nLettuceThreads.pooled     remote  thrpt   25   7791.706 \xc2\xb1  133.507  ops/s\nLettuceThreads.shared     socket  thrpt   25  81195.900 \xc2\xb1 1593.424  ops/s\nLettuceThreads.shared  localhost  thrpt   25  78404.688 \xc2\xb1 3878.044  ops/s\nLettuceThreads.shared     remote  thrpt   25   3992.023 \xc2\xb1   39.092  ops/s\n
Run Code Online (Sandbox Code Playgroud)\n\n

线程=4

\n\n
Benchmark              (address)   Mode  Cnt       Score      Error  Units\nLettuceThreads.pooled     socket  thrpt   25   87345.126 \xc2\xb1 8149.009  ops/s\nLettuceThreads.pooled  localhost  thrpt   25   75003.031 \xc2\xb1 4481.289  ops/s\nLettuceThreads.pooled     remote  thrpt   25   15807.410 \xc2\xb1  225.376  ops/s\nLettuceThreads.shared     socket  thrpt   25  169112.489 \xc2\xb1 3749.897  ops/s\nLettuceThreads.shared  localhost  thrpt   25  115464.778 \xc2\xb1 5099.728  ops/s\nLettuceThreads.shared     remote  thrpt   25    7952.492 \xc2\xb1  133.521  ops/s\n
Run Code Online (Sandbox Code Playgroud)\n\n

您可以在这里看到,性能随着线程数量的增加而变化得非常好,因此生菜并非毫无用处。

\n