Design and Implementation of a Redis-Based Delayed Queue Service
The article explains the need for delayed processing in business scenarios, compares various delayed queue solutions such as Java DelayQueue, RocketMQ, and RabbitMQ, and details a Redis-based implementation with message storage, ZSET queues, pull jobs, workers, and Zookeeper coordination for high availability.
Background: In business processes, scenarios such as unpaid orders after 30 minutes, missing user reviews, or undelivered orders require delayed handling; polling tables works for small data but becomes resource‑intensive at scale.
Types of delayed queues: Java's java.util.concurrent.DelayQueue (simple but in‑JVM only), RocketMQ delayed queue (persistent and distributed but limited granularity), RabbitMQ delayed queue using TTL+DLX (persistent and distributed but same‑delay messages share a queue).
Key considerations for building a custom delayed‑queue service include message storage, real‑time retrieval of expired messages, and high availability.
Redis‑based implementation (version 1.0) stores messages in a hash (Message Pool), uses 16 ZSETs as delayed queues (score = expiration time), and a timed task scans queues. Message fields include tags, keys, body, delayTime, and expectDate.
Version 2.0 improves latency by replacing the 1‑minute polling task with a Java Lock await/signal mechanism, enabling real‑time delivery of expired messages.
Multi‑node deployment adds pull‑job threads per queue, worker threads for processing, and Zookeeper coordination for queue rebalancing; pull jobs track offsets to avoid message loss on failures, and services register with Zookeeper on startup.
The main workflow: on service start, register with Zookeeper, obtain assigned queues, launch pull‑job threads, query for expired messages, hand them to workers, and remove them after successful processing; offsets are adjusted to handle retries.
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.
Architect's Guide
Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.
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.
