那么它如何执行并发I

那么它如何执行并发I

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

问题描述

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

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

作者指出:

我肯定会误解整个线程问题,因为我发现此语句令人费解.如果程序是单线程的,它如何并发执行任何操作?如果服务器仍然是单线程的,为什么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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 03:43