Redis是单线程的,那么并发I/O是怎么做的呢? [英] Redis is single-threaded, then how does it do concurrent I/O?

查看:19
本文介绍了Redis是单线程的,那么并发I/O是怎么做的呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了掌握 Redis 的一些基础知识,我看到了一篇有趣的博客文章 .

Trying to grasp some basics of Redis I came across an interesting blog post .

作者声明:

Redis 是单线程的,带有 epoll/kqueue,在 I/O 并发方面可以无限​​扩展.

Redis is single-threaded with epoll/kqueue and scale indefinitely in terms of I/O concurrency.

我肯定误解了整个线程的事情,因为我觉得这句话令人费解.如果程序是单线程的,它如何并发执行任何操作?如果服务器是单线程的,为什么Redis操作是原子的那么好?

I surely misunderstand the whole threading thing, because I find this statement puzzling. If a program is single-threaded, how does it do anything concurrently? Why it is so great that Redis operations are atomic, if the server is single-threaded anyway?

有人能解释一下这个问题吗?

Could anybody please shed some light on the issue?

推荐答案

这取决于你如何定义并发.

Well it depends on how you define concurrency.

在服务器端软件中,并发性和并行性通常被视为不同的概念.在服务器中,支持并发 I/O 意味着服务器能够通过仅使用一个计算单元执行与这些客户端对应的多个流来为多个客户端提供服务.在这种情况下,并行意味着服务器能够同时执行多项操作(使用多个计算单元),这是不同的.

In server-side software, concurrency and parallelism are often considered as different concepts. In a server, supporting concurrent I/Os means the server is able to serve several clients by executing several flows corresponding to those clients with only one computation unit. In this context, parallelism would mean the server is able to perform several things at the same time (with multiple computation units), which is different.

例如,调酒师可以同时照顾多位顾客,而他一次只能准备一种饮料.所以他可以提供并发而不是并行.

For instance a bartender is able to look after several customers while he can only prepare one beverage at a time. So he can provide concurrency without parallelism.

这里已经讨论过这个问题:并发和并行有什么区别?

This question has been debated here: What is the difference between concurrency and parallelism?

另请参阅 Rob Pike 的此演示文稿.

See also this presentation from Rob Pike.

单线程程序肯定可以通过使用 I/O(解)复用机制和事件循环(Redis 所做的)在 I/O 级别提供并发.

A single-threaded program can definitely provide concurrency at the I/O level by using an I/O (de)multiplexing mechanism and an event loop (which is what Redis does).

并行是有代价的:在现代硬件上可以找到多个套接字/多核,线程之间的同步非常昂贵.另一方面,像 Redis 这样的高效存储引擎的瓶颈通常是网络,远在 CPU 之前.因此,独立的事件循环(不需要同步)被视为构建高效、可扩展的服务器的好设计.

Parallelism has a cost: with the multiple sockets/multiple cores you can find on modern hardware, synchronization between threads is extremely expensive. On the other hand, the bottleneck of an efficient storage engine like Redis is very often the network, well before the CPU. Isolated event loops (which require no synchronization) are therefore seen as a good design to build efficient, scalable, servers.

Redis 操作是原子的这一事实只是单线程事件循环的结果.有趣的一点是原子性是在没有额外成本的情况下提供的(它不需要同步).用户可以利用它来实现乐观锁定和其他模式,而无需支付同步开销.

The fact that Redis operations are atomic is simply a consequence of the single-threaded event loop. The interesting point is atomicity is provided at no extra cost (it does not require synchronization). It can be exploited by the user to implement optimistic locking and other patterns without paying for the synchronization overhead.

这篇关于Redis是单线程的,那么并发I/O是怎么做的呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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