Design a Distributed Job Scheduler
Understanding the Problem
🗓️ What is a distributed job scheduler? A job scheduler runs pieces of work for other teams: "run this report every night at 2 AM," "re-index this customer in 30 seconds," "retry this payout in an hour." Users register a job with a schedule and a payload, and the scheduler makes sure a worker somewhere runs it at the right time, retries it when it fails and records what happened. Think of cron, but for a whole company, on hundreds of machines, where a dead machine can't mean a missed job.
You'll also hear this asked as "design a GPU job scheduler" or "design the system ML engineers use to submit training jobs." It's the same problem: work is submitted, waits for its time and for a free worker, runs, and reports back. What changes is the shape of the workers. A cron job needs any free slot; a training job needs eight GPUs on the same machine, possibly for three days. We'll build the general scheduler first and cover the GPU and ML variant as its own deep dive.
Functional Requirements
We'll agree with our interviewer that the scheduler runs work but doesn't know what the work is. A job is a container image or a registered handler plus a payload; the scheduler's job is to run it at the right time on the right machine.
Core Requirements
- Users should be able to submit a job to run immediately, at a specific time, or on a recurring cron schedule.
- The system should run each job on a worker at (or very close to) its scheduled time.
- The system should retry failed jobs according to the job's retry policy.
- Users should be able to see the status and history of their jobs and cancel them.
Below the line (out of scope)
- Multi-step workflows with dependencies between jobs (that's a workflow engine).
- Building and storing the container images themselves.
- Billing and chargeback for compute used.
Non-Functional Requirements
Ask for numbers. We'll design for 10 million job executions per day, from about 2 million registered jobs (most are recurring), across a few hundred tenant teams.
Core Requirements
- A job should start within 2 seconds of its scheduled time under normal load.
- A job that's been accepted must never be lost, even if the scheduler or a worker dies at the wrong moment.
- Each scheduled run should execute at least once, and we should make running it twice rare and detectable (true exactly-once isn't possible when the job itself has side effects, so handlers must be idempotent).
- The scheduler should be highly available. Losing a machine or a zone should delay jobs by seconds, not drop them.
- One tenant's burst of jobs shouldn't starve everyone else.
Below the line (out of scope)
- Sub-second precision. Nobody needs a nightly report started within 10 ms.
- Running jobs across regions. Each region has its own scheduler.
Back-of-the-envelope. 10M executions/day ÷ 86,400 ≈ 115 executions per second on average. That number is a trap. Cron schedules cluster on round times: 0 * * * * (top of every hour) and 0 0 * * * (midnight) are by far the most common. If 30% of a day's executions fire on the hour mark, that's 3M/24 ≈ 125,000 jobs due in the same second, every hour. We need to dispatch that spike quickly even though the average load is tiny.
Storage is easy by comparison. An execution record is a few hundred bytes to 1 KB, so 10M/day is about 10 GB/day, or a few TB a year. We'll keep recent history hot and age the rest out to cheaper storage.
The design is driven by the spike, not the average. Saying "115 QPS, one Postgres box is fine" and then hearing your interviewer ask about midnight is a common way to lose the thread. Do the clustering math up front.