KatInterview

Build a Reliable Delayed and Immediate Task Runner

System DesignModel answer
MediumModel answerSystem Designsystem-designjob-schedulerreliability

Last updated

Design a durable service that runs tasks right away or at a future time, and explain how it copes with crashes, retries, duplicates, and the limits of exactly-once execution.

You are asked to design a backend service that runs background tasks on behalf of other teams. Clients submit a task and the platform executes it later, outside the request path.

The service must support two kinds of submissions:

  1. Run-now tasks: these should start as soon as capacity allows.
  2. Deferred tasks: the client gives a delay (for example "in 1 hour") or an absolute timestamp. When that time arrives, the task should start as soon as possible.

Reliability is the main concern. In your answer, cover:

  • What happens when a worker crashes or stalls partway through a task.
  • What happens when the scheduler component itself fails or is replaced.
  • How retries work, including backoff and what to do with tasks that keep failing.
  • How you prevent or tolerate duplicate execution.
  • Whether exactly-once execution is achievable, and what you can honestly promise instead.

State your assumptions about scale (task volume, maximum delay, payload size, runtime), then present the API, data model, architecture, and the failure-handling deep dives. Call out trade-offs and the choices you would make under uncertainty.

Hints

Hint 1

Ask whether the two submission types really need separate pipelines. A run-now task can be treated as a deferred task whose due time is the current moment.

Hint 2

Keep a durable record as the source of truth, and give each running attempt a time-limited lease that the worker must renew. Think about what should happen to a stale worker that wakes up after its lease was taken away.

Hint 3

When a crash can happen between two steps (record state, then publish to a queue), decide which failure you prefer: losing a task or running it twice. Then ask what exactly-once can realistically mean when the task has side effects.

Hint 4

Exactly-once execution cannot be guaranteed in general. Aim for at-least-once delivery combined with idempotent handlers and stable idempotency keys.