为什么对 Redis 使用异步客户端有意义? [英] Why does it make sense to use asynchronous clients for Redis?

查看:31
本文介绍了为什么对 Redis 使用异步客户端有意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个 页面列出 redis 客户端中,我统计了 8 个异步库.我的理解是,像 node.js 或 tornado 这样的框架只有在异步回调函数没有为 I/O 相互争斗时才有意义,否则你最好同步.

In this page listing the redis clients, I counted 8 asynchronous libraries. My understanding is that frameworks like as node.js or tornado only make sense when the asynchronous callback functions are not fighting with each other for I/O, otherwise you might as well go synchronous.

但是Redis是单线程的.所以他们实际上是在为 I/O 而战.Redis 的单线程特性不是取消了异步回调的所有潜在好处吗?为什么在 Redis 中使用异步客户端有意义?

But Redis is single-threaded. So they are actually fighting for I/O. Doesn't the single-threaded nature of Redis cancel all the potential benefits of asynchronous callbacks? Why does it make sense to use asynchronous clients with Redis?

推荐答案

Redis 的单线程特性与异步客户端的潜在优势无关.尽管有独特的事件循环,Redis 能够同时管理大量客户端连接.我在单个 Redis 实例上看到了高达 30000 个连接的基准测试.

The single-threaded nature of Redis is irrelevant regarding the potential benefits of an asynchronous client. Despite its unique event loop, Redis is able to concurrently manage a good number of client connections. I have seen benchmarks with up to 30000 connections on a single Redis instance.

请考虑使用 Redis 或 memcached 等内存键/值存储,性能和延迟主要由网络往返而不是服务器端 CPU 消耗决定.诚然,当网络链路饱和时,网络往返的延迟会增加,但这并不意味着当网络远未饱和时它变得可以忽略不计.例如,在负载非常轻的 1 GbE 网络上,RTT 延迟接近 200 us 的情况并不少见.

Just consider that with in-memory key/value stores like Redis or memcached, the performance and latencies are dominated by the network roundtrips rather than server-side CPU consumption. Granted, the latency of network roundtrips increases when the network links are saturated, but it does not mean it becomes negligible when the network is far from saturation. For instance, on a very lightly loaded 1 GbE network, it is not uncommon to see the RTT latency close to 200 us.

结果是,除非网络链接接近饱和,客户端连接(或异步回调函数)很少相互竞争 I/O.套接字与缓冲区相关联,缓冲区可以分摊网络上读写操作的成本.大多数时候,等待状态不是因为 I/O 竞争,而是因为网络延迟.

The consequence is, except when the network links are close to saturation, client connections (or asynchronous callback functions) are rarely competing with each other for I/Os. Sockets are associated to buffers which amortize the cost of read and write operations on the network. Most of the time, the wait states are not due to I/O competition, but to the latency of the network.

有多种方法可以减少网络延迟的影响:

There are various ways to decrease the impact of network latency:

  • 流水线:将多个命令组合在一起,以便每组命令支付一次网络往返费用(实际上,这是同步流水线).

  • pipelining: grouping multiple commands together so that network roundtrips are payed once per group of commands (actually, this is synchronous pipelining).

非阻塞 I/O:虽然它不会减少往返次数(或它们的单独成本),但异步客户端可以同时管理它们.结果是网络延迟对应用程序吞吐量的影响较小(或没有).

non blocking I/Os: while it does not reduce the number of roundtrips (or their individual cost), an asynchronous client can manage them concurrently. The consequence is the network latency has less (or no) impact on the application throughput.

多个客户端连接:每个客户端连接都有自己的套接字,因此也有自己的缓冲区.更多的缓冲区通常意味着更好的吞吐量.更多的连接增加了并发和/或异步处理事物的机会,对整体性能产生积极影响.

multiple client connections: each client connection has its own socket, and therefore its own buffer. More buffers often means better throughput. More connections increases the opportunity to process things concurrently and/or asynchronously, with positive impacts on the overall performance.

这些方案都得到Redis生态的支持,可以组合使用以最大化性能.异步客户端通常允许这种组合.异步客户端的用例是什么?以下是一些示例:

These solutions are all supported by the Redis ecosystem, and can be combined to maximize the performance. Asynchronous clients typically allows this kind of combinations. What are the use cases of an asynchronous client? Here are a few examples:

  • 实现异步流水线,以尽量减少单个连接上的等待状态.

  • implementing asynchronous pipelining, to minimize the wait states on a single connection.

将 Redis 连接与现有事件循环(例如 libevent、Node.js、Tornado、Twisted 等)集成,而不依赖于额外的线程池.

integrating Redis connection(s) with an existing event loop (such as libevent, Node.js, Tornado, Twisted, etc ...) without relying on an extra thread pool.

支持多个Redis实例的数据分片.在这种情况下,客户端应用程序可能希望并行化对各种实例的访问.使用异步客户端,可以方便地从一个独特的线程完成.

supporting data sharding with multiple Redis instances. In that case, the client application will probably want to parallelize the accesses to the various instances. With an asynchronous client, it can be conveniently done from a unique thread.

支持基于客户端应用程序到各种主/从实例的预连接的 HA 弹性模型.

supporting the HA resiliency models based on the pre-connection of the client application to the various master/slaves instances.

事件循环、异步库和/或类似协程的机制是大多数高效 NoSQL 引擎的基石之一.

Event loops, asynchronous libraries, and/or coroutine-like mechanisms are one of the corner stones of a majority of the efficient NoSQL engines out there.

这篇关于为什么对 Redis 使用异步客户端有意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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