NOTE

2.4 Redis Asynchronous Queue

An asynchronous queue orders data first in, first out and buffers it between production and consumption. These notes record an implementation using Redis.

Redis / CacheCreated Updated 1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. What Is an Asynchronous Queue?

  • Queue: first in, first out.
  • Asynchronous: there is a buffer.
  • Put simply, it is a producer/consumer model with a buffer.

2. How to Implement an Asynchronous Queue

2.1. rpush + lpop + sleep

A list is generally used as the queue. rpush produces messages and lpop consumes messages. When lpop gets no message, sleep for a while before retrying.

2.2. What If Someone Asks Whether sleep Can Be Avoided?

The list also has a command called blpop. When there is no message, it blocks until a message arrives.

2.3. What If Someone Asks Whether a Message Can Be Produced Once and Consumed Multiple Times?

Use the pub/sub topic-subscriber model to implement a 1:N message queue.

2.4. What If Someone Asks About the Disadvantages of pub/sub?

When a consumer is offline, produced messages are lost, so a dedicated message queue such as RabbitMQ is needed.

2.5. What If Someone Asks How Redis Implements a Delayed Queue?

Use a sorted set. Use the timestamp as the score and the message content as the key, call zadd to produce messages, and have consumers use zrangebyscore to poll for and process data older than N seconds.

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub