KatInterview

Build a Fault-Tolerant Future Task Execution Engine

System DesignModel answer
HardModel answerSystem Designdistributed-systemsschedulingfault-tolerance

Last updated

Design a distributed scheduler that durably stores millions of delayed tasks and dispatches them within seconds of their due time, surviving crashes and partitions.

Context

Many product workflows depend on deferred actions: canceling an unpaid order after 10 minutes, sending a follow-up notification 48 hours after a user signs up, or revoking a temporary access token at a precise moment. Rather than building this logic ad hoc in every service, your team wants a shared Future Task Execution Engine — a central service that any client can hand a job to with a target fire time, and trust that it will run.

You are asked to design this system end to end.

What the System Must Do

Core operations:

  • Register a task: Accept a payload, an execution target, and a fire_at timestamp. Return a stable identifier for the task. Acknowledge only after the record is durably stored.
  • Revoke a task: Cancel a registered task before it fires. If the task has already been dispatched, return an appropriate error.
  • Inspect a task: Return the current status, attempt count, and next scheduled time for a given task.
  • Worker feedback: Accept a success or failure callback from the downstream worker so the scheduler can drive retries.

Execution behavior:

  • Tasks must never fire before their fire_at time.
  • At least 99% of tasks must fire within 2 seconds of fire_at.
  • Failed executions must be retried automatically with exponential backoff and jitter, up to a configurable attempt ceiling.
  • Tasks that exhaust all retries go to a dead-letter destination for manual review.

Constraints and Scale

  • Up to 50 million tasks pending at any moment.
  • Peak ingestion: 5,000 new tasks per second.
  • Peak execution: 5,000 tasks firing per second.
  • Scheduled delays range from 5 seconds to 90 days in the future.
  • Zero task loss after the client receives an acknowledgment.
  • At-least-once delivery; downstream consumers must be idempotent, but unnecessary duplicates should be minimized.

What Your Answer Should Cover

  1. API surface and data model — the external interface, the storage schema, and how you index tasks for efficient time-based retrieval.
  2. Discovery mechanism — how the system efficiently finds tasks whose fire_at has arrived among tens of millions of records; compare relevant approaches and justify your choice.
  3. Atomic claiming — how multiple scheduler nodes coordinate to ensure a task is dispatched exactly once per attempt without a global lock.
  4. Failure recovery — what happens when a scheduler node crashes mid-dispatch, when a lease expires, or when clocks drift; how you handle a sudden burst of tasks all due at the same moment.

Hints

Hint 1

Start with capacity math: 50M tasks at ~1 KB each is ~50 GB of state. That rules out keeping everything in RAM cheaply, but it fits easily in a sharded database. The 'hot set' — tasks due in the next few minutes — is much smaller and can live in memory on the owning node.

Hint 2

Think in two tiers: a durable database as the source of truth (so the ack guarantee holds) plus an in-memory structure like a min-heap or timing wheel per shard owner for sub-second polling precision. The two tiers are complementary, not alternatives.

Hint 3

Atomic claiming is the hardest coordination problem. A conditional UPDATE that checks both status = PENDING and due_time <= now() acts as a compare-and-swap. Pair that with a lease epoch as a fencing token so a slow or partitioned node cannot corrupt state after it loses ownership of a shard.