Back to explorer
Infrastructure & Messaging• 45 Min

Design a Distributed Job Scheduler

HARD

Design a Distributed Job Scheduler

Distributed job schedulers coordinate execution of millions of recurring (cron) and ad-hoc delayed tasks across worker clusters with at-least-once execution guarantees, dependency graphs (DAGs), and automatic retry semantics.


1. System Requirements

Functional Requirements

  1. Submit Jobs: Users/services can submit one-time delayed jobs (run_at: timestamp) or recurring cron jobs (cron: "*/5 * * * *").
  2. Execution Guarantees: Tasks must trigger within a few seconds of scheduled execution time.
  3. Task Dependencies (DAGs): Support execution pipelines where Task B only runs after Task A completes successfully.
  4. Retry & Backoff: Automatic retries with exponential backoff and jitter on worker failure.
  5. Job Monitoring & Status: Real-time dashboard to inspect job execution state (PENDING, RUNNING, SUCCESS, FAILED).

Non-Functional Requirements

  1. Scalability: Support scheduling 100M+ tasks per day with peak trigger rates of 50,000 tasks/second.
  2. High Availability: Master/Coordinator nodes must failover seamlessly without dropping scheduled tasks.
  3. Durability: Zero task loss if a node crashes right before trigger time.

2. Core Architecture: Hierarchical Time Wheel & Database Partitioning

The Hierarchical Hashed Timing Wheel

Instead of polling the database with SELECT * FROM jobs WHERE trigger_time <= NOW() (which locks tables and scales poorly), the scheduler uses an in-memory Hierarchical Timing Wheel:

  • Divided into Second, Minute, Hour, and Day circular ring buffers.
  • Tasks are placed into buckets corresponding to execution offsets.
  • As the clock advances each second, the current bucket's tasks are immediately drained into a distributed task queue (Kafka / RabbitMQ).

3. High-Level Architecture Blueprint

code
                                  +-----------------------+
                                  |    Client Services    |
                                  +-----------+-----------+
                                              |
                                              | POST /jobs
                                              v
                                  +-----------------------+
                                  |     Scheduler API     |
                                  +-----------+-----------+
                                              |
                                              v
                                  +-----------------------+
                                  | Primary Storage DB    |
                                  | (PostgreSQL / TiDB)   |
                                  +-----+-----------+-----+
                                        |           |
             +--------------------------+           +--------------------------+
             |                                                                 |
             v                                                                 v
+------------------------+                                         +------------------------+
| Scheduler Coordinator  |                                         |  Distributed Message   |
| (Leader Election via   |                                         |  Queue (Kafka / SQS)   |
| ZooKeeper / Raft)      |                                         +-----------+------------+
+-----------+------------+                                                     |
            |                                                                  v
            +==== Dispatches Tasks Due Now ====================================>+
                                                                               |
                                                                               v
                                                                   +------------------------+
                                                                   | Worker Nodes Cluster   |
                                                                   | (Task Execution Pods)  |
                                                                   +------------------------+

4. Staff-Level Deep Dives

Deep Dive 1: Preventing Duplicate Executions

In distributed systems, networks partition and workers experience garbage collection pauses.

  • Distributed Lock with Fencing Tokens: Before executing, the worker acquires a Redis / ZooKeeper lock. The lock issues a monotonically incrementing fencing token. If a paused worker wakes up, its stale token is rejected by the database.
  • Idempotency Keys: Workers pass the task_execution_id to downstream APIs to ensure side effects are safely deduplicated.

Key Architecture Trade-Offs & Decision Matrix

DimensionOption A: Asynchronous / DecoupledOption B: Synchronous RPCTrade-off & Recommendation
Communication PatternMessage Queues (Kafka / SQS)Direct gRPC / REST CallsMessage queues prevent cascading failures and provide backpressure buffering.
Failure HandlingDead Letter Queue (DLQ) with Exponential BackoffImmediate Circuit Breaker tripDLQ preserves unparseable messages for manual operational inspection.
Ordering GuaranteesPartition-level Strict FIFOUnordered Parallel ExecutionPartition-key routing guarantees chronological order per entity without global lock contention.