multithreading Redis是单线程的,那么并发I/O是怎么做的呢?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10489298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 01:19:17  来源:igfitidea点击:

Redis is single-threaded, then how does it do concurrent I/O?

multithreadingredis

提问by Przemys?aw Pietrzkiewicz

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

在尝试掌握 Redis 的一些基础知识时,我看到了一篇有趣的博客文章

The author states:

作者指出:

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

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

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?

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

Could anybody please shed some light on the issue?

有人可以对这个问题有所了解吗?

回答by Didier Spezia

Well it depends on how you define concurrency.

好吧,这取决于您如何定义并发。

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.

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

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?

这个问题在这里争论过: 并发和并行有什么区别?

See also this presentationfrom Rob Pike.

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

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).

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

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 这样的高效存储引擎的瓶颈通常是网络,远在 CPU 之前。因此,隔离事件循环(不需要同步)被视为构建高效、可扩展服务器的好设计。

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

回答by Martin James

OK, Redis is single-threaded at user-level, OTOH, all asynchronous I/O is supported by kernel thread pools and/or split-level drivers.

好的,Redis 在用户级别是单线程的,OTOH,内核线程池和/或拆分级别驱动程序支持所有异步 I/O。

'Concurrent', to some, includes distributing network events to socket state-machines. It's single-threaded, runs on one core, (at user level), so I would not refer to this as concurrent. Others differ..

对某些人来说,“并发”包括将网络事件分发到套接字状态机。它是单线程的,在一个核心上运行(在用户级别),所以我不会将其称为并发。其他不一样..

'scale indefinitely in terms of I/O concurrency' is just being economical with the truth. They may get more belief if they said 'can scale better than one-thread-per-client, providing the clients don't ask for much', though they may then feel obliged to add 'blown away on heavy loading by other async solutions that use all cores at user level'.

'在 I/O 并发方面无限扩展'只是经济实惠。如果他们说“可以比每个客户端一个线程更好地扩展,前提是客户端要求不高”,他们可能会更有信心,尽管他们可能会觉得有必要添加“被其他异步解决方案吹走的重负载”在用户级别使用所有内核'。