问题描述
我目前正在尝试了解 Redis 的一些基本实现内容.我知道 redis 是单线程的,我已经偶然发现了以下问题:Redis是单线程的,那么并发I/O是怎么做的?
I'm currently trying to understand some basic implementation things of Redis. I know that redis is single-threaded and I have already stumbled upon the following Question: Redis is single-threaded, then how does it do concurrent I/O?
但我还是觉得我没理解对.Afaik Redis 使用一个单线程的反应器模式.因此,如果我理解正确,那么有一个观察者(处理 FD/传入/传出连接)将要完成的工作委托给它注册的事件处理程序.他们做实际的工作并设置例如.他们的响应作为事件给观察者,观察者将响应传输回客户端.但是如果客户端的请求 (R1) 花费大约 1 分钟会发生什么.另一个客户端创建另一个(快速)请求 (R2).然后 - 由于 redis 是单线程的 - 在 R1 完成之前,不能将 R2 委托给正确的处理程序,对吗?在多线程环境中,您可以在单个线程中启动每个处理程序,因此主"线程只是接受和响应 io 连接,所有其他工作都在自己的线程中执行.
But I still think I didn't understood it right. Afaik Redis uses the reactor pattern using one single thread. So If I understood this right, there is a watcher (which handles FDs/Incoming/outgoing connections) who delegates the work to be done to it's registered event handlers. They do the actual work and set eg. their responses as event to the watcher, who transfers the response back to the clients. But what happens if a request (R1) of a client takes lets say about 1 minute. Another Client creates another (fast) request (R2). Then - since redis is single threaded - R2 cannot be delegated to the right handler until R1 is finished, right? In a multithreade environment you could just start each handler in a single thread, so the "main" Thread is just accepting and responding to io connections and all other work is carried out in own threads.
如果它真的只是将 io 处理和处理程序逻辑排队,那么它永远不会那么快.我在这里错过了什么?
If it really just queues the io handling and handler logic, it could never be as fast it is. What am I missing here?
推荐答案
除了 Redis 中的大多数操作在不到~毫秒~几微秒内完成这一事实之外,您没有遗漏任何东西.长时间运行的操作确实在执行期间阻塞了服务器.
You're not missing anything, besides perhaps the fact that most operations in Redis complete in less than a ~millisecond~ couple of microseconds. Long running operations indeed block the server during their execution.
这篇关于如果Redis是单线程的,怎么会这么快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!