How to Build a Reliable Delayed Queue with Redis and Other Messaging Tools
This article explains why Redis can be slow when used for delayed processing, shows how to implement a delayed queue with Redis ZSET, compares its advantages, and reviews alternative solutions such as RabbitMQ, RocketMQ, Kafka, Netty, and Java's DelayQueue.
Introduction
Redis is an in‑memory, single‑process, single‑threaded database (multithreading is supported from Redis 6.0). Although it is extremely fast, it can be configured to behave slowly when certain parameters are set, which is useful for implementing delayed processing.
Design and Implementation with Redis
When a user sends a message request, the server first checks whether the message requires delayed handling. If it does, the request is placed into a delayed queue; otherwise it is processed immediately and the result is returned.
Redis implements the delayed queue using the zset (sorted set) data structure. The score of each member is a timestamp indicating when the task should be executed.
Key Commands
zadd myqueue score1 value1– add a task with a future timestamp. zrangebyscore myqueue min max withscores limit 0 1 – fetch the earliest task that is ready for execution.
After fetching, the worker consumes the task and removes it from the set.
Advantages of Using Redis for Delayed Queues
High‑performance score sorting provided by zset.
In‑memory operations give sub‑millisecond latency.
Cluster mode allows horizontal scaling for large message volumes.
Persistence via AOF or RDB ensures data can be recovered after failures.
Alternative Implementations
Message‑Broker Solutions
RabbitMQ can implement delayed queues by setting x‑expires on a queue or x‑message‑ttl on a message. A plugin called rabbitmq‑delayed‑message‑exchange also provides native delayed delivery.
RocketMQ groups messages with the same delay into dedicated queues and uses a timer to poll for expiration. It only supports predefined delay intervals (e.g., 1 s, 5 s, …, 2 h).
Kafka
Kafka adds a custom timer called SystemTimer based on a timing wheel ( TimingWheel) to schedule delayed tasks.
Netty
Netty uses HashedWheelTimer, which internally relies on a DelayedQueue and the time‑wheel algorithm to manage delayed execution.
Java DelayQueue
Java’s built‑in DelayQueue wraps a PriorityQueue. Each element carries a delay value; only elements whose delay has elapsed become eligible for removal. This approach keeps data in memory, so it is not durable and cannot be used in a distributed environment.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
IT Architects Alliance
Discussion and exchange on system, internet, large‑scale distributed, high‑availability, and high‑performance architectures, as well as big data, machine learning, AI, and architecture adjustments with internet technologies. Includes real‑world large‑scale architecture case studies. Open to architects who have ideas and enjoy sharing.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
