KatInterview

Build a durable task scheduler with delayed execution

System DesignModel answer
HardModel answerSystem Designlow-level-designschedulingconcurrency

Last updated

Design a scheduler that runs jobs after a specified delay, with APIs, data structures, persistence, concurrency control, and recovery.

You need to design a task scheduler that executes jobs after a configurable delay. The scheduler must survive crashes, handle concurrent operations, and support cancellation.

Your design should include:

  1. API surface: methods for scheduling, cancelling, and querying task status
  2. Internal data structures: how you organize pending tasks for efficient retrieval
  3. Persistence layer: ensuring durability across process restarts
  4. Execution model: how workers claim and run tasks
  5. Concurrency control: handling race conditions and limiting parallel execution
  6. Delivery guarantees: at-least-once vs exactly-once semantics
  7. Time complexity of core operations

Provide class or component diagrams showing the relationships between major pieces. Discuss how you would verify correctness through testing.

Assume you're building for a single-node service handling around 10,000 schedules per second with delays ranging from seconds to weeks, and up to a million pending tasks at any time.

Hints

Hint 1

Start by clarifying functional requirements and scale constraints before proposing structures

Hint 2

Consider min-heap for precise timing or timing wheels for high-throughput cancellation scenarios

Hint 3

Use compare-and-swap or optimistic locking to prevent multiple workers from executing the same task

Hint 4

Design for testability by abstracting time and storage behind interfaces