KatInterview

Build a Fault-Tolerant Job Scheduler for Async Workflows

System DesignModel answer
HardModel answerSystem Designdistributed-systemsschedulingfault-tolerance

Last updated

Design a distributed scheduling service that fires one-time and recurring jobs on time, at scale, without losing a single task across node failures or clock drift.

Background

Asynchronous workflows are everywhere in data-intensive platforms: kicking off hourly metric rollups, retrying failed delivery webhooks with exponential backoff, or running nightly ETL pipelines. Rather than baking scheduling logic into each individual service, it makes sense to centralize it behind a dedicated scheduling layer that any internal team can call.

Your task is to design that layer: a durable, multi-tenant service that lets clients register future work, forget about it, and trust it will fire correctly — even when nodes crash or the cluster is mid-election.

What to Design

A scheduling service with the following properties:

Core behavior

  • Accept both one-shot tasks (fire at a specific UTC timestamp) and recurring tasks (defined by a cron expression with an explicit timezone).
  • Be responsible only for triggering execution — dispatching a message or event to downstream workers — not for running the job logic itself.
  • Let callers cancel or modify a pending task before it has fired.
  • Deliver tasks at least once, and give downstream consumers the tools (idempotency keys) to safely handle re-delivery.
  • Retry failed or stalled dispatches with configurable backoff, and route permanently failed tasks to a dead-letter destination.

Scale targets

  • Up to 50 million pending tasks in the system at any time.
  • Peak dispatch throughput of 10,000 tasks per second.
  • Each task must fire within 1 second of its scheduled time.
  • Zero task loss once the API has acknowledged a registration request.
  • Survive node crashes, clock skew, and concurrent dispatch attempts across nodes.

Expected Coverage

Address all four of the following areas:

  1. API and data model — endpoints for creating, updating, cancelling, and inspecting tasks; schema for task definitions, run records, and state transitions.
  2. Due-task discovery and claiming — how the system finds tasks that are ready to fire, partitions responsibility across scheduler nodes, and prevents duplicate dispatches without a centralized lock.
  3. Dispatch and execution handoff — how the scheduler passes work to executors (push vs. pull, message queues, execution leases), and how execution outcomes flow back.
  4. Failure handling and edge cases — thundering herds (many tasks scheduled for the same instant), missed runs during downtime, recurring-task catch-up policies, and stale claims from crashed nodes.

Discuss trade-offs where relevant. Numbers and concrete examples carry more weight than vague generalities.

Hints

Hint 1

Start with scale math: 50M tasks at ~1 KB each is ~50 GB of hot state, and 10k dispatches/s means roughly 30–40k DB writes/s when you count state transitions and run history. These numbers should drive your storage and partitioning choices before you sketch any architecture.

Hint 2

A naive 'poll every second' loop against a single table will not hit the 1-second accuracy target at this scale. Think about combining a short lookahead window from the database with an in-memory timer structure (a timing wheel) so the hot path never touches the DB at fire time.

Hint 3

The trickiest correctness problem is: what happens if a scheduler node claims a task and then crashes before it finishes dispatching? Your design needs fencing tokens or versioned leases so a recovered or replacement node can safely reclaim the work without producing a duplicate dispatch.