当前位置:网站首页>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 .
边栏推荐
- Exploration and practice of network security vulnerability management
- ES6事件绑定(v-on用法)
- Prometheus + AlertManager 消息预警
- Excel uses countif statistics
- 2022/07/28 学习笔记 (day18) 常用API
- 3种过期策略
- 线上3d数字展厅制作方案及优点
- 响应式织梦模板户外露营类网站
- Data security and privacy computing summit - development and application of security intersection in privacy Computing: Learning
- Waiting queue wait_ queue
猜你喜欢
随机推荐
C language improvement (I)
What should I do if excel opens a CSV file containing Chinese characters and there is garbled code?
Rust 列表(Vec)复制
2022/07/28 learning notes (day18) common APIs
数据安全与隐私计算峰会-安全求交集在隐私计算中的发展和应用:学习
[RT learning note 1] RT thread peripheral routine - control LED light flashing
Time pit in MySQL driver
FPGA刷题——存储器(RAM和FIFO的Verilog实现)
[upload pictures can be cut-1]
HTTP缓存
结合Retrofit 改造OKHttp 缓存
会议OA之会议通知
Read the recent trends of okaleido tiger and tap the value and potential behind it
How to guarantee password security? How does the secure browser manage passwords?
WebView attack
密码安全如何保障?安全浏览器如何管理密码?
响应式织梦模板户外露营类网站
time_wait和close_wait产生原因
7/28 Gauss elimination to solve linear equations + Gauss elimination to solve XOR linear equations + find the combination number II
Day 15 (VLAN related knowledge)





![[quality] code quality evaluation standard](/img/33/2c2256fd98b908ddaf5573f644ad7f.png)


