KatInterview

Design a Recurring Task Scheduler with Pause and Resume

System DesignModel answer
MediumModel answerSystem Designschedulingdistributed-systemscron

Last updated

Design a distributed service that registers one-time and cron-based tasks, dispatches them when due, and supports pausing and resuming schedules reliably.

The Problem

Build a task scheduling service. Clients register tasks that should run either once at a specific future time or repeatedly according to a cron expression. Clients can also pause a task at any point and later resume it.

Your service is responsible for deciding when each task is due, dispatching it to an available worker, and maintaining a complete history of every run. Assume you have as many workers as you need — capacity for actually executing tasks is not a constraint. The hard parts are correctness, timing, and reliability.

What to Design

  • A client-facing API to register, pause, resume, and query tasks
  • A data model for tasks, their schedules, their pause state, and individual run records
  • A mechanism to efficiently find tasks that are due without scanning everything
  • A dispatch path from your scheduler to workers
  • Handling for failures, duplicate runs, and bursts of tasks that all fire at the same time

Constraints and Clarifications

The following are deliberately left open. Either ask clarifying questions before you start, or state your assumptions explicitly and justify them:

  • How many tasks are registered in total, and how many become due in the same second (for example, at the top of the hour)?
  • How tight is the start-time requirement — must a task fire within a second of its scheduled time, or is a few seconds acceptable?
  • What is the run guarantee: is it acceptable to run a task twice after a crash, or must each scheduled occurrence execute exactly once?
  • If a cron task is still executing when the next occurrence is due, should the new occurrence start immediately, queue up, or be skipped?
  • When a task is paused, what happens to a run already in progress? On resume, should occurrences that were missed during the pause catch up or be skipped?
  • In which time zone are cron expressions evaluated, and what should happen when a clock change causes an occurrence to be skipped or duplicated?

What a Strong Answer Looks Like

A strong answer settles the open questions first, then covers the API and data model, the indexing strategy for due-task lookup, the dispatch pipeline, pause and resume semantics in detail, at-least-once vs. exactly-once trade-offs, and how the scheduler itself scales and recovers from failures.

Hints

Hint 1

Before sketching any architecture, nail down the numbers and guarantees. Volume and timing precision change the indexing strategy. At-least-once vs. exactly-once changes almost everything downstream — lease design, idempotency, retry logic, and worker contracts.

Hint 2

For due-task lookup, think about what you can index. A time-ordered index on next_run_at lets you query with a simple range scan instead of scanning every task. After dispatch, you need to compute and write the next occurrence atomically with claiming the current one.

Hint 3

Pause semantics have two separate sub-problems: what to do with a run that is already in flight when pause is called, and what to do with occurrences that fall inside the paused window when resume is called. Treat these independently and be explicit about each choice.