KatInterview

Design a 100x Scalable Distributed Task Scheduler

System DesignModel answer
MediumModel answerSystem Designsystem-designdistributed-systemsjob-scheduler

Last updated

Architect a flexible background job system capable of scaling 100-fold using partitioned time indices, hierarchical timing wheels, and resilient worker leasing.

Problem Context

An online platform relies heavily on background processing to support key features such as digest emails, reverse-geocoding coordinates, boundary calculations, and push notifications. The current system relies on a central relational database to manage and dispatch these background jobs.

As global activity scales, the system needs to support a 100-fold increase in job volume without requiring a complete design teardown. You need to design an architecture that satisfies immediate needs while presenting a clear migration strategy to handle the target scale.

Requirements

Functional Requirements

  1. Task Submission: Support one-time delayed tasks and recurring (cron-style) schedules with custom parameters and desired execution times.
  2. State & Control: Expose APIs to fetch job statuses, view execution logs, and cancel pending tasks.
  3. Delivery & Retries: Guarantee at-least-once task execution, configurable exponential backoff retry policies, and priority handling (e.g., real-time notifications over batch reports).

Scale & Non-Functional Requirements

  • Initial Load: ~500 task submissions per minute (~8.3 QPS), with peak execution of 1,000 tasks per minute (~16.7 QPS). Active pending tasks stored: ~1 million.
  • Target Scale (100x): ~50,000 task submissions per minute (~833 QPS average, 2,500 QPS peak), with peak execution reaching 100,000 tasks per minute (~1,666 QPS execution rate). Active pending tasks stored: ~100 million.
  • Execution Timeliness: Tasks should trigger within a 1 to 5-second window of their target timestamp.
  • Fault Tolerance: High availability with no single point of failure; crashes of workers or schedulers must not result in dropped tasks.

Discussion Points

  1. API Contracts: Interface definition for job registration, status checking, and cancellation.
  2. Storage Architecture: Data model and storage engine selections for baseline load versus target scale.
  3. Dispatch & Scheduling: Mechanism for triggering due tasks accurately (e.g., polling, timing wheels, range partitioning).
  4. Execution & Idempotency: Worker lease management, heartbeat mechanisms, handling late-arriving or crashed workers, and preventing execution duplication.
  5. Scaling Strategy: Detailed evolution path from baseline relational storage to a distributed 100x architecture.

Hints

Hint 1

Decouple the time index (finding work that is due) from the durable source of truth (job definitions) and the queue (workers execution line).

Hint 2

Avoid materializing infinite recurring cron instances into database rows; materialize only the immediate next run.

Hint 3

Use optimistic concurrency (fencing tokens/versioning) and heartbeat leases to prevent zombie workers from corrupting state.