NOTE

2.4 Redis Asynchronous Queue

Simple Redis asynchronous-queue patterns with lists, blocking pop, Pub/Sub, and sorted sets.

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 One Production Can Be 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 professional 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 and process data from N seconds ago.

Discussion

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