KatInterview

Build a Reliable Recurring Task Scheduler with Recovery

System DesignModel answer
MediumModel answerSystem Designsystem-designschedulerdistributed-systems

Last updated

Design a service that fires recurring tasks on time-zone-aware schedules, survives crashes, handles missed runs, and avoids duplicate logical runs.

Design a service that lets users register recurring tasks and guarantees they are triggered according to a calendar-style schedule (for example "every weekday at 09:00 in Europe/Berlin"). The service must remain correct when machines crash, restart, or are briefly unreachable.

Your answer should explain:

  1. Schedule interpretation: how a schedule is stored and how the next fire time is computed.
  2. Dispatch: how due runs are discovered and handed to workers.
  3. Worker execution: how a worker takes ownership of a run, reports progress, and finishes.
  4. Failure recovery: what happens when a scheduler node or worker dies mid-way.
  5. Missed runs: what the system does if it was down or backlogged when a run was due.

Things to settle explicitly in your design:

  • State your assumptions about scale (number of schedules, peak fires per second, acceptable lateness) and about delivery guarantees.
  • Separate a logical occurrence (the run that was supposed to happen at a given time) from an attempt (one try at executing it).
  • Every schedule carries its own time zone. Explain how daylight-saving gaps and overlaps are handled, and what happens when a schedule is edited or disabled.
  • Do not claim exactly-once side effects in external systems just because your own database rows are unique.

Useful clarifying questions: Should missed runs be skipped, merged into one, or replayed one by one? May two runs of the same task overlap? Are the tasks idempotent? How are retries and permanent failures surfaced to users?

Hints

Hint 1

Model two entities: a schedule (rule, time zone, version, next fire time) and an occurrence (schedule id + scheduled instant, unique together). Attempts hang off occurrences.

Hint 2

Make 'create the due occurrence' and 'advance the schedule's next fire time' a single transaction, protected by a unique key, so multiple scheduler replicas cannot produce duplicates.

Hint 3

Workers should claim an occurrence with a lease that expires unless renewed by heartbeat; a reaper returns expired claims to the queue. Because a worker may die after the side effect but before reporting, pass a stable idempotency key to the task.

Hint 4

Decide up front on a misfire policy (skip, run once, replay all up to a cap) and on DST rules (gap: run at the next valid time; overlap: run once, on the first occurrence).