SYSTEM DESIGN SKETCHBOOK / PRACTICE SHEET / NO. 23
Design a distributed job scheduler
Teams across a company register background work with a shared scheduler: "rebuild the search index every night at 02:00 Berlin time", "expire this trial in 14 days", "retry this webhook in 30 seconds". They want to register a job once and trust that it runs on time even when individual machines crash, and they want to see what ran and what failed.
Requirements
- Create, update, pause and cancel jobs with a cron expression and time zone, or a single run time.
- Start each due run on a worker, retrying failures with backoff up to a configured number of attempts.
- Let a job declare that it must not run concurrently with itself, and what to do about runs missed during an outage.
- Show each job's run history: scheduled time, start, end, attempt count and outcome.
Constraints and assumptions
- 10 million scheduled jobs, mostly one-off; 50,000 recurring jobs.
- About 30 million runs a day, but up to 20,000 runs become due in the same second at the top of the minute and hour.
- A run should start within 5 seconds of its scheduled time at p99.
- Runs last from a second to an hour. Execution is at least once, and job handlers are expected to be idempotent.
- A schedule that was accepted must survive the loss of any machine or one availability zone.
Out of scope for the first sketch
Workflows with dependencies between jobs · Placing containers on machines (bin packing) · Per-team billing
Draw your design
Rough math and notes
SYSTEM DESIGN SKETCHBOOK / FOLLOW-UPS
Design a distributed job scheduler
Finish your first sketch before answering these. Cross out freely.
- What do you store for a recurring job, and how does the scheduler find what is due in the next second without scanning 10 million rows?
- A worker picks up a run, then freezes for two minutes in a garbage-collection pause. The run is handed to another worker. Now both think they own it. What prevents a double effect?
- The scheduler is down from 01:55 to 02:20. What happens to the nightly 02:00 job and the every-five-minutes job when it comes back?
- At midnight UTC, 20,000 jobs are due in the same second. Where does it hurt first, and how do you smooth it?
- One team's job fails every time and is configured with generous retries. How do you stop it from hurting everyone else?
Compare afterwards: worked approaches and reading at sketchbook /questions/job-scheduler/review