当前位置:网站首页>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 .
边栏推荐
猜你喜欢
会议OA之会议通知
Day 15 (VLAN related knowledge)
一文搞懂 Redis 架构演化之路
How to quickly design a set of cross end components that support rendering rich text content
Awvs cannot start problem
Data security and privacy computing summit - development and application of security intersection in privacy Computing: Learning
Jmeter之BeanShell生成MD5加密数据写入数据库
Three implementation methods of Servlet
响应式织梦模板家装装饰类网站
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
随机推荐
Workflow of wireless vibrating wire acquisition system
会议OA之会议通知
[upload pictures can be cut-1]
What happens if you have to use ArrayList in multithreading?
DevOps 团队如何抵御 API 攻击?
发布融资需求1.29亿元,大科城项目路演持续浇灌科创“好苗子”
Thermistor temperature calculation formula program
Prometheus + alertmanager message alert
Transform okhttp cache with retrofit
C语言实现三子棋游戏
ES6详解 快速上手!
Installation guide for proftpd Secure FTP server with TLS encryption enabled
一文读懂Okaleido Tiger近期动态,挖掘背后价值与潜力
多边形点测试
NPM install reports an error: eperm: operation not permitted, rename
快速掌握Nodejs安装以及入门
3种过期策略
In depth analysis - Pretreatment
即时通讯场景下安全合规的实践和经验
2022/07/28 学习笔记 (day18) 常用API