【问题标题】:Redis is single thread. Then why should I use lettuce?Redis 是单线程的。那我为什么要用生菜呢?
【发布时间】:2018-03-21 14:43:49
【问题描述】:

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

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

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

【问题讨论】:

    标签: redis lettuce


    【解决方案1】:

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

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

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

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

    线程=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连接池连接数的示例代码?一直找不到好的来源。
    • var client = RedisClient.create(uri); var config = new GenericObjectPoolConfig(); config.setMaxTotal(5); var pool = ConnectionPoolSupport.createGenericObjectPool( client::connect, new GenericObjectPoolConfig());
    猜你喜欢
    • 2012-05-16
    • 2020-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-17
    • 2018-01-03
    • 1970-01-01
    • 2010-11-18
    相关资源
    最近更新 更多