Task Scheduler
Job Scheduling System
Design an in-process scheduler that runs one-time and recurring tasks at the right moment on a pool of worker threads, with cancellation, failure handling and no busy waiting.
Overview
A scheduler holds tasks that should run later: once at a given time, or again and again at an interval. When a task's time comes it runs on a worker thread, so a slow task never delays the others. Tasks can be canceled before they run. A task that raises must not take the scheduler down. This is what cron, Java's ScheduledExecutorService and Python's sched module do; you are designing the small version.
Interviewers use it to see whether you know the two ideas that make a scheduler correct and cheap: a min-heap keyed by run time, so the next task is always at the top, and a condition variable, so the scheduler thread sleeps exactly until that task is due or a new task arrives. They then push on the details that separate a toy from a real one: what a recurring interval means when a run is slow, how cancellation works when the task is buried in the heap, and what happens on failure.
Scoping the problem
Ask seven questions before you design anything. Each comes with why it matters and the answer interviewers usually give.
- One-time, recurring, or both? Usually both. Recurring tasks need a rule for the next run time.
- Fixed rate or fixed delay for recurring tasks? The two use different arithmetic and behave differently when a run is slow. Support both and explain the difference.
- How many tasks run at once? A small, configurable worker pool runs them; the scheduler thread never runs a task itself.
- Can a task be canceled, and while it runs? Cancellation before the run is easy; cancellation during the run needs the task's cooperation. The usual answer is before only: a running task finishes.
- What if a task raises? Mark it failed and keep the scheduler alive; a recurring task then either continues or stops. The usual answer is to continue and count failures.
- Does the schedule survive a restart? It can live in memory only or be persisted. Here it is in memory, and persistence is a follow-up.
- Which clock? Wall-clock time jumps and a monotonic clock does not, so the answer is monotonic.
Functional requirements
- Schedule a one-time task for a given time
- Schedule a recurring task at a fixed rate or with a fixed delay
- Run due tasks on a pool of worker threads; the scheduler thread never runs a task itself
- Cancel a task by id before it runs; a running task finishes and is not rescheduled
- Notify listeners of task events: due, started, completed, failed, canceled
- Track each task's status, run count and failure count
Non-functional requirements
- No busy waiting: the scheduler thread sleeps until the next due time or until a new task arrives, whichever is first
- Thread-safe: one Condition guards the heap and the task dict; every state change to a task happens under it
- A task that raises is marked failed; the worker, the scheduler and the other tasks keep going
- Tasks due at the same instant run in the order they were scheduled
- Next run time O(1), schedule O(log n), cancel O(1)
- Scheduling uses a monotonic clock; the clock is injectable
Interview questions
- Why a condition variable instead of a loop that sleeps 100 ms and checks?
- What is the difference between fixed rate and fixed delay, and which drifts?
- What happens when a task raises, and when a recurring task keeps raising?
- How would you add dependencies between tasks, so B runs only after A succeeds?
- How would the schedule survive a restart, or run on more than one machine?
- Why is the clock injectable, and why monotonic?
Sign up free to read the full topic
More Low-Level Design topics
Start free on Cariara · Coding problems · System design studio