Work That Waits
Sometimes a job should not run now. Send a reminder in 24 hours, retry a payment in 5 minutes, or release a hold at a specific timestamp. A delayed job carries a ready time and stays invisible until then.
How Brokers Implement Delay
- Sorted set by ready time where a scheduler thread moves jobs whose time has arrived into the live queue.
- Per message delay supported natively by some brokers, hiding the message until the delay expires.
- Timer wheel that buckets jobs by expiry for efficient large scale scheduling.
The common idea is a due index: the system repeatedly asks for jobs whose ready time is less than or equal to now and promotes them.
Precision Versus Cost
Polling the due index every second gives roughly one second precision. Tighter precision costs more polling. Most background work tolerates a few seconds of slack, so do not over engineer.
Pitfalls
- Clock skew across nodes can fire jobs early or late. Use one authoritative clock source.
- Thundering herd when many jobs share the same ready time. Add small random jitter.
Key idea
Delayed jobs sit in a due index keyed by ready time, and a scheduler promotes them when their time arrives.