KatInterview

Build a Fault-Tolerant Distributed Task Timer Service

System DesignModel answer
HardModel answerSystem Designdistributed-systemsjob-schedulingleases

Last updated

Design a distributed service that runs one-off and repeating tasks on time at the scale of millions of jobs, survives worker crashes, and limits duplicate runs despite clock skew.

Scenario

You are asked to design a backend service that fires tasks at their scheduled time. It must handle two kinds of tasks:

  • One-time tasks: run once at a given timestamp.
  • Recurring tasks: run again and again at a fixed interval, in seconds.

A fleet of N worker machines executes the tasks. The service must make sure every task runs at least once, while keeping duplicate runs as rare as possible.

Operations to support

  1. ScheduleJob(job_id, payload, run_at_timestamp): durably store a one-time task.
  2. ScheduleRecurringJob(job_id, payload, interval): durably store a task that fires repeatedly.
  3. ExecuteJobs(): the loop workers use to find tasks that are due and run them.

Requirements

  • Scale: millions of active scheduled tasks.
  • Fault tolerance: if a worker dies while running a task, another worker must take that task over.
  • Precision: a task should start within a few seconds of its scheduled time.
  • Latency: keep the gap between "task becomes due" and "task is picked up" small.

Constraints

  • You may choose any database for persistence.
  • Assume N worker nodes running in parallel.
  • Account for clock skew between servers.

What to cover in your answer

  1. Data model: the schema for task definitions, schedules, and run state.
  2. Task acquisition: how workers claim tasks without stepping on each other (locks, leases, sharding, etc.).
  3. Failure handling: how you detect a dead worker and put its task back into circulation.
  4. Architecture: the main components (API layer, storage, the component that finds due tasks, the execution nodes) and how they interact.

State your assumptions about scale (task count, peak rate) before you begin.

Hints

Hint 1

Say early that exactly-once execution cannot be guaranteed when machines crash. Aim for at-least-once delivery from the scheduler plus idempotent task handlers.

Hint 2

Separate the task definition (with its next fire time) from each individual run record, and give each run a deterministic ID such as job_id plus scheduled time, so a unique constraint collapses duplicate triggers.

Hint 3

Split the system into two roles: a small set of trigger nodes that decide what is due (each owning a hash-based slice of the data) and many executors that do the work. Use time-limited leases, and take all time comparisons from the database clock rather than from worker clocks.