How does Redis implement message queuing
1. Use List
List is the simplest and most straightforward, storing and reading messages queue (fifO) via Lpush and RPOP
FirstMsg (integer) 1 127.0.0.1:6379> lpush mq "secondMsg" # (integer) 2 127.0.0.1:6379> rPOP MQ # receive message "firstMsg"Copy the code
2. Use Zset
ZSet implements message queues in a similar way to List, and Zadd and ZRangeByScore store and read messages
How does Redis implement delayed message queuing
ZSet has a score attribute, which is used to store time stamps, and multiple threads poll ZSet to obtain expired tasks for processing, so as to realize delayed message queuing, etc., the steps are as follows:
- using
zadd
Inserts elements into the collection with the element’s timestamp (timeout) as score - using
zrangebyscore
以0 < score <= Current timestamp
To get the elements to be processed - When an element meets a condition, delete it first
zrem
This element (guaranteed not to be fetched by other processes) is then processed by business logic;
Problem a: When the elements meeting the conditions of the delay queue are empty (or the set is empty), the process will frequently obtain the elements meeting the conditions from redis service, which will cause resource occupation and waste of Redis service -> The program can be blocked for a period of time when the conditions are not met. This method is actually to exchange time for resources, pay attention to control the blocking time, not too short, not too long (affect the immediacy)
Zrangebyscore and ZREM may not be the same client, i.e. atomic problem -> use lua script to solve
Third, Redis implementation of queue defects
- The message is not persisted and will be lost if the server goes down or restarts
- There is no ACK mechanism, and if the consumption fails, the message is lost
- There is no queue monitoring and the performance of inbound and outbound pairs is poor