为什么Redis是单线程的(事件驱动) [英] why Redis is single threaded(event driven)

查看:130
本文介绍了为什么Redis是单线程的(事件驱动)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解Redis的基础知识. Redis是无处不在的一种,它是使事情变得原子化的单线程,但是我无法想象它是如何在内部工作的,我对此深信不疑.

I am trying to understanding basics of Redis. One that that keep coming everywhere is, Redis is single threaded that makes things atomic.But I am unable to imagine how this is working internally.I have below doubt.

如果服务器是IO绑定的应用程序(例如Node.js),则我们不设计服务器单线程,该线程在启动IO操作后释放了另一个请求的空间,并在IO操作完成后将数据返回给客户端(提供并发性) .但是在Redis的情况下所有数据都可以在主内存中使用,我们根本就不会做IO操作.那么为什么Redis是单线程的呢?如果第一个请求花费大量时间会发生什么情况,剩下的请求将不得不保留等待?

Don't we design a server Single thread if it is IO bound application(like Node.js),where thread got free for another request after initiating IO operation and return data to client once IO operation is finished(providing concurrency). But in case of redis all data are available in Main Memory,We are not going to do IO operation at all.So then why Redis is single threaded?What will happen if first request is taking to much time,remaining request will have to keep waiting?

推荐答案

TL; DR :单线程使redis更加简单,并且redis仍然受IO约束.

TL;DR: Single thread makes redis simpler, and redis is still IO bound.

内存是I/O. Redis仍受I/O约束.当redis处于高负载下并达到每秒最大请求数时,它通常会饿死于网络带宽或内存带宽,并且通常不会占用大量CPU.对于某些命令,这不是真的,但是在大多数情况下,redis会受到网络或内存的严格I/O约束.

Memory is I/O. Redis is still I/O bound. When redis is under heavy load and reaches maximum requests per second it is usually starved for network bandwidth or memory bandwidth, and is usually not using much of the CPU. There are certain commands for which this won't be true, but for most use cases redis will be severely I/O bound by network or memory.

除非内存和网络速度突然加快几个数量级,否则单线程通常不是问题.如果您需要扩展到一个或几个线程之外(即:master <-> slave <-> slave setup),那么您已经在研究Redis Cluster.在这种情况下,如果您因某种原因CPU饿了并且想最大化线程数,则可以为每个CPU内核设置一个群集实例.

Unless memory and network speeds suddenly get orders of magnitude faster, being single threaded is usually not an issue. If you need to scale beyond one or a few threads (ie: master<->slave<->slave setup) you are already looking at Redis Cluster. In that case you can set up a cluster instance per CPU core if you are somehow CPU starved and want to maximize the number of threads.

我对Redis的源代码或内部信息不是很熟悉,但是我可以看到使用单个线程如何使实现无锁原子操作变得容易.线程会使情况变得更加复杂,并且由于redis不受CPU限制,因此似乎并没有提供很大的优势.在redis实例之上的某个级别上实现并发似乎是一个不错的解决方案,这是Redis Sentinel和Redis Cluster的帮助.

I am not very familiar with redis source or internals, but I can see how using a single thread makes it easy to implement lockless atomic actions. Threads would make this more complex and doesn't appear to offer large advantages since redis is not CPU bound. Implementing concurrency at a level above a redis instance seems like a good solution, and is what Redis Sentinel and Redis Cluster help with.

Redis需要很长时间时,其他请求会如何处理?

What happens to other requests when redis takes a long time?

在redis完成长请求时,其他请求将被阻止.如果需要,您可以使用 client-pause 命令进行测试.

Those other requests will block while redis completes the long request. If needed, you can test this using the client-pause command.

这篇关于为什么Redis是单线程的(事件驱动)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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