← Lessons

quiz vs the machine

Gold1360

System Design

Delayed and Scheduled Jobs

Run a job at a future time using ready times and a delay structure.

5 min read · core · beat Gold to climb

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.

Check yourself

Answer to earn rating on the learn ladder.

1. How do brokers typically know a delayed job is ready to run?

2. Why add random jitter to delayed jobs that share a ready time?