Dashboard
Courses
Problems
Badges
Theme
Sign inStart free
Courses/Asynchronous and Event-Driven Systems
Lesson 11 of 20

Scheduling and delayed jobs

Run work at a future time, reliably, at scale, and exactly when users expect.

15 min read

Cron on one box is not enough

A crontab on a single server is a single point of failure: if that machine is down at 09:00, the job does not run. Put the same crontab on two servers and every job runs twice. Distributed scheduling needs either one active scheduler chosen by leader election, a lock per job run (a database row lock or a Redis SET NX with a TTL), or a managed scheduler such as EventBridge Scheduler or Cloud Scheduler. Schedulers should only decide what is due and enqueue it; workers do the actual work.

Storing future jobs

User-defined jobs (reminders, trial expiry, scheduled posts) are data, not crontab lines. Store them in a table indexed by run_at. A poller wakes every second, selects a batch of due jobs with SELECT ... FOR UPDATE SKIP LOCKED so several pollers can run without double-claiming, and pushes them to a work queue. Redis sorted sets do the same in memory, with the timestamp as the score. Broker-native delays are useful for short waits but limited: an SQS message can be delayed by at most 15 minutes.

Scale and the top of the hour

Due times are not uniform. Humans pick round numbers, so a reminder service with 100 million jobs may see a large share due at exactly 09:00 local time. That is a spike of millions of jobs in one second. Handle it by pre-loading jobs due in the next few minutes into the queue with a short delay, sharding pollers by job ID range, and sizing workers so the spike drains within your precision target (for example, delivered within 30 seconds of the scheduled time).

Correctness details

Scheduling is full of small traps.

  • Dispatch is at-least-once, so key sends by job ID plus scheduled time to make them idempotent
  • Store absolute times in UTC, but store the user's time zone for recurring jobs so 09:00 stays 09:00 across daylight-saving changes
  • For recurring jobs, compute and store the next occurrence when the current one runs
  • Check that a job is still active when it executes, not only when it was enqueued, so cancellations take effect

Key takeaways

  • One cron box is a single point of failure; two run jobs twice without a lock
  • Store user jobs in a table indexed by due time and let pollers enqueue them
  • Plan for spikes at round times such as the top of the hour
  • Make dispatch idempotent and handle time zones and cancellation at execution time

Sign in to track lessons and earn badges.

Backpressure and flow controlWebhooks

Challenge checkpoint

Course: 0/20 lessons · 0/10 challenges0%

Build it on the canvas, pass the review (70+) and mark it complete to count it toward the course badge.

  1. 4/10Design Scheduled RemindersStore future jobs, dispatch them on time without a single cron box, and survive the top-of-the-hour spike.Easy
  2. 5/10Design a Distributed Message Queue
Build a partitioned, replicated broker with offsets, delivery guarantees, backpressure and delayed delivery.
MAJORHardPRO
All 10 challenges →