Redis 是单线程的.那我为什么要用生菜呢? [英] Redis is single thread. Then why should I use lettuce?

查看:41
本文介绍了Redis 是单线程的.那我为什么要用生菜呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

After Redis 4.0, Redis can execute multi thread some functions (1. deleting objects in backgrounds, etc.), but Redis still usually uses single thread. FAQ - Redis

所以我猜生菜没用.Lettuce是redis客户端,1个连接可以使用多线程,而Redis只能1个连接使用单线程.

So I guess lettuce is useless. Lettuce is redis client that can use multiple threads in 1 connections, but Redis can use only single thread in 1 connection.

你能推荐在 Redis 客户端使用生菜吗?为什么?

Can you recommend to use lettuce for Redis client? Why?

推荐答案

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

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.

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

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

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

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.

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

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

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

您可以在此处看到性能与线程数量的关系非常好,因此生菜并非毫无用处.

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

这篇关于Redis 是单线程的.那我为什么要用生菜呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆