Redis is single thread. Then why should I use lettuce?

荒凉一梦 提交于 2019-12-01 12:22:08

Because you spend time not only while Redis executes commands, but also transferring data (sending commands, recieving results). In single thread mode while you transfer, Redis doesn't work. While Redis works, no transfer occures. Multiple connections or one pipelined connection are here to help you saturate both bandwidth and CPU cycles.

And luttece is not only about speed. It also helps you organize your code better with asynchronous and reactive API.

Back to performance topic, here is a simple benchmark to get general understanding of threading and pooling impact. Note, that while pooling is a bit slower (you spend some time on pool operations), it allows you to isolate actions (so an error doesn't affect other threads) and use MULTI and blocking commands.

Here are my results (local system has 4 cores, remote system CPU is about 2 times slower):

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

Threads=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

Threads=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

You can see here that performance scales very well with the number of threads, so lettuce is not useless.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!