Design a Job Scheduler
⏰ What is a job scheduler? It is the part of a system that runs work later or again and again: send this email in ten minutes, rebuild the report every hour, clean up old files every night. You hand it a job and a schedule, and it makes sure the job runs at the right time on one of a limited number of workers.
Every backend has one of these somewhere, which is why interviewers like it. It is small enough for 45 minutes, but it mixes three things candidates often blur together: when a job should run (its schedule), whether it can run yet (are workers free?), and what state it is in right now. Keeping those three apart is most of the design.
You may hear this problem as "design a task scheduler" or "design cron". They are the same design. Tasks and jobs are just names, and cron is one more way to say when a job is due: we build the scheduler so a cron schedule plugs in as one new class, and we write it in the Extensibility section.
Requirements
"Design a job scheduler. Users can submit jobs to run now, at a specific time, or on a repeating schedule. A fixed pool of workers runs them."
That sounds complete, but nearly every interesting rule is still open. These questions settle them.
Clarifying Questions
You: What kinds of schedules do we need?
Interviewer: Three. Run now, run once at a given time, and run every N minutes starting at a given time. Cron expressions might come later.
You: How many workers are there, and what happens when they are all busy?
Interviewer: A fixed number, set when the scheduler starts. A job that is due while every worker is busy waits. When a worker frees up, the job that has been due the longest goes first; if two are due at the same moment, the one submitted first.
You: What if a recurring job is still running, or still waiting, when its next run comes due?
Interviewer: A job must never run twice at the same time. Skip the runs it missed and continue on its normal rhythm: a job that runs every 15 minutes from 9:00 should keep running at quarter hours, not drift.
You: What can a user cancel? Can we stop a job in the middle of a run?
Interviewer: Any job that is not finished. We can't kill a run safely, so a running job finishes its current run and then stops. Until then it is still running and still holds its worker.
You: What statuses do users see?
Interviewer: Scheduled (waiting for its time), queued (due but waiting for a worker), running, done (a one-off job that finished) and cancelled. Users also want to know how many runs a job has completed, and which jobs are running right now.
You: How do we deal with time? And what does a job actually do?
Interviewer: Pass the current time into every call, in minutes, and give each job a duration so we know when it finishes. The actual work is someone else's code.
The third answer is the one that separates a good design from a buggy one, so we will spend a whole section on it. The last answer shapes everything else: with time passed in, the scheduler is a simulation that can be tested exactly. We still talk about real threads later, but the rules (who runs when) are the same, and they are much easier to get right when you can test them without a clock.
💡 Interview tip: Say early that you will keep the scheduling decisions separate from the threads that execute jobs. It shows you know the hard bugs live in the rules, lets you code and test those rules first, and gives you a clean place to add threads when the interviewer asks.
Final Requirements
- The scheduler has a fixed number of workers. Each run of a job uses one worker for the job's duration.
- A job can run now, once at a given time, or every
intervalminutes from a first time. A time in the past means now. - A job that is due while all workers are busy waits. Free workers take waiting jobs by earliest due time, then by lowest job id.
- A recurring job never runs twice at once. After a run that was due at
dfinishes atf, the next run is due at the first timed + k * interval(fork >= 1) that is not beforef. Missed runs are skipped. - Cancelling stops a job. A waiting job stops at once. A running job finishes its current run, then stops.
- Users can see a job's status (scheduled, queued, running, done, cancelled), its number of completed runs, and the list of running jobs.
- Every call carries the current time, and the scheduler first catches up on everything that happened before it.
- New kinds of schedules (for example, cron) must not change the scheduler itself.
Out of scope
- Job priorities and retries after failure (both in Extensibility).
- Persisting jobs across restarts and running on many machines (Extensibility points to the distributed version).
- Dependencies between jobs, where one job starts only after another finishes.