当前位置:网站首页>redis为什么快,消息队列,单线程
redis为什么快,消息队列,单线程
2022-07-29 01:58:00 【Meme_xp】
为什么快?
1.基于内存:Redis是使用内存存储,没有磁盘IO上的开销。数据存在内存中,读写速度快。
2.单线程实现( Redis 6.0以前): Redis使用单个线程处理请求,避免了多个线程之间线程切换和锁资源争用的开销。
3.IO多路复用模型:Redis采用IO多路复用技术。Redis 使用单线程来轮询描述符,将数据库的操作都转换成了事件,不在网络IO上浪费过多的时间。
4.高效的数据结构:Redis每种数据类型底层都做了优化,目的就是为了追求更快的速度。
redis为什么选择单线程
1、单线程实现可以避免过多的上下文切换开销。程序始终运行在进程中单个线程内,没有多线程切换的场景。
2、避免同步机制的开销。如果Redis选择多线程模型,需要考虑数据同步的问题,则必然会引入某些同步机制,会导致在操作数据过程中带来更多的开销,增加程序复杂度的同时还会降低性能。
3、实现简单,方便维护。如果Redis使用多线程模式,那么所有的底层数据结构的设计都必须考虑线程安全问题,那么Redis的实现将会变得更加复杂。
应用场景
Redis应用场景有哪些?
1、缓存热点数据,缓解数据库的压力。
2、利用Redis 原子性的自增操作,可以实现计数器的功能,比如统计用户点赞数、用户访问数等。
3、作为简单的消息队列,实现异步操作。
4、限速器,可用于限制某个用户访问某个接口的频率,比如秒杀场景用于防止用户快速点击带来不必要的压力。
5、好友关系,利用集合的一些命令,比如交集、并集、差集等,实现共同好友、共同爱好之类的功能。
redis怎么实现消息队列
1、使用列表,让生产者将任务使用LPUSH命令放进列表,消费者不断用RPOP从列表取出任务。
2、发布订阅模式。类似于MQ的主题模式。只能消费订阅之后发布的消息,一个消息可以被多个订阅者消费。
3、延时队列。使用sortedset,拿时间戳作为score,消息内容作为key,调用zadd来生产消息,消费者用zrangebyscore`指令获取N秒之前的数据轮询进行处理。
边栏推荐
- 第3章业务功能开发(线索备注的删除和修改)
- How to guarantee password security? How does the secure browser manage passwords?
- 连PostgreSQL问题:expected authentication request from server, but received v
- 多边形点测试
- Day 14: continued day 13 label related knowledge
- Prometheus + alertmanager message alert
- Character flow comprehensive exercise problem solving process
- Internet of things development -- mqtt message server emqx
- 一文理解分布式开发中的服务治理
- 如果时间不够,无法进行充分的测试怎么办?
猜你喜欢
随机推荐
Explain the four asynchronous solutions of JS in detail: callback function, promise, generator, async/await
Thermistor temperature calculation formula program
Data security and privacy computing summit - development and application of security intersection in privacy Computing: Learning
Explain asynchronous tasks in detail: task status and lifecycle management
结合Retrofit 改造OKHttp 缓存
Talk about the implementation principle of feign
我被这个浏览了 746000 次的问题惊住了
如何在多御安全浏览器中自定义新标签页?
How to guarantee password security? How does the secure browser manage passwords?
【质量】代码质量评价标准
NPM install reports an error: eperm: operation not permitted, rename
开启TLS加密的Proftpd安全FTP服务器安装指南
Day 14: continued day 13 label related knowledge
裂开了,一次连接池参数导致的雪崩问题
记一次 ERROR scheduler.AsyncEventQueue: Dropping event from queue shared导致OOM
QT learning notes -37.qregex and regular expressions
Responsive Zhimeng template decoration design website
手把手教你安装VSCode(附带图解步骤)
Data query of MySQL (multi table query)
Waiting queue wait_ queue









