当前位置:网站首页>Why is redis fast? Message queue, single thread
Why is redis fast? Message queue, single thread
2022-07-29 02:35:00 【Meme_ xp】
Why fast ?
1. Memory based :Redis Is to use memory storage , No disk IO Overhead on . Data is in memory , Fast reading and writing .
2. Single threaded implementation ( Redis 6.0 before ): Redis Use a single thread to process requests , It avoids the overhead of thread switching and lock resource contention among multiple threads .
3.IO Multiplexing model :Redis use IO Multiplexing technology .Redis Polling descriptors using a single thread , Convert all database operations into events , Not in the network IO Waste too much time on .
4. Efficient data structure :Redis Each of the underlying data types has been optimized , The purpose is to pursue faster speed .
redis Why single thread
1、 Single thread implementation can avoid excessive context switching overhead . The program always runs within a single thread in the process , There is no scenario of multithreading switching .
2、 Avoid the overhead of synchronization mechanism . If Redis Choose the multithreading model , The problem of data synchronization needs to be considered , Some synchronization mechanisms will be introduced , It will lead to more overhead in the process of operating data , It increases the complexity of the program and reduces the performance at the same time .
3、 Implement a simple , Convenient maintenance . If Redis Use multithreading mode , Then thread safety must be considered in the design of all underlying data structures , that Redis The implementation of will become more complex .
Application scenarios
Redis What are the application scenarios ?
1、 Caching hot data , Relieve the pressure of database .
2、 utilize Redis Atomic self increasing operation , The function of counter can be realized , For example, count the number of user likes 、 Number of user visits, etc .
3、 As a simple message queue , Implement asynchronous operations .
4、 Speed governor , It can be used to limit the frequency of a user accessing an interface , For example, the second kill scenario is used to prevent unnecessary pressure caused by users' fast clicking .
5、 Friend relationship , Use some of the commands of the collection , For example, intersection 、 Combine 、 Difference set, etc , Realize common friends 、 Common hobbies and other functions .
redis How to implement message queuing
1、 Use list , Let the producer use the task LPUSH Put the command into the list , Consumers keep using RPOP Take the task from the list .
2、 Publish subscribe mode . Be similar to MQ Theme mode . Only messages published after subscription can be consumed , A message can be consumed by multiple subscribers .
3、 Delay queue . Use sortedset, Take the time stamp as score, The content of the message is key, call zadd To produce news , Consumers use zrangebyscore` Instructions for N Second before the data polling for processing .
边栏推荐
- FPGA刷题——存储器(RAM和FIFO的Verilog实现)
- 聊聊接口性能优化的11个小技巧
- 详解JS的四种异步解决方案:回调函数、Promise、Generator、async/await
- Chapter 3 business function development (deletion and modification of clue remarks)
- Workflow of wireless vibrating wire acquisition system
- 密码安全如何保障?安全浏览器如何管理密码?
- On Multithreading
- I was stunned by this question that I browsed 746000 times
- Waiting queue wait_ queue
- 响应式织梦模板装修设计类网站
猜你喜欢

The financing demand of 129 million yuan was released, and the roadshow of the Dake city project continued to irrigate the "good seedlings" of scientific innovation

ES6详解 快速上手!

矿山开采虚拟现实vr安全培训提升员工警惕性和防护意识

密码安全如何保障?安全浏览器如何管理密码?

ES6 event binding (v-on usage)

2022/07/28 learning notes (day18) common APIs

千万不要把Request传递到异步线程里面,有坑

How to use RPA to achieve automatic customer acquisition?

Responsive dream weaving template home decoration website

HTTP缓存
随机推荐
How does the Devops team defend against API attacks?
DevOps 团队如何抵御 API 攻击?
[mqtt from introduction to improvement series | 09] Wireshark packet capturing and analyzing mqtt messages
响应式织梦模板户外露营类网站
How to quickly design a set of cross end components that support rendering rich text content
[quality] code quality evaluation standard
[upload pictures can be cut-1]
Virsh console connection failure
Branch management practice of "two pizza" team
Production scheme and advantages of online 3D digital exhibition hall
ES6 event binding (v-on usage)
MySQL基本操作和基于MySQL基本操作的综合实例项目
我被这个浏览了 746000 次的问题惊住了
响应式织梦模板家装建材类网站
WebView attack
C language improvement (I)
virsh console连接失败问题
2022/07/28 learning notes (day18) common APIs
数据安全与隐私计算峰会-安全求交集在隐私计算中的发展和应用:学习
Data security and privacy computing summit - development and application of security intersection in privacy Computing: Learning