KatInterview

Build a Scalable Task Scheduling Service

System DesignModel answer
MediumModel answerSystem Designdistributed-systemsschedulingfault-tolerance

Last updated

Architect a fault-tolerant distributed system that executes millions of recurring jobs with precise timing, at-least-once guarantees, and resilience to failures.

Problem

You're building a cloud-native task scheduling platform that customers use to automate recurring operations—sending invoices, generating reports, purging stale data, and triggering batch pipelines. Your service must reliably fire jobs at their configured intervals, deliver execution requests to external systems, and remain available despite infrastructure failures.

What You Need to Design

Core Capabilities

  • Schedule CRUD: Customers define repeating tasks using cron syntax with explicit time zones. They can create, modify, pause, resume, delete, and manually invoke schedules on demand.
  • Trigger Dispatch: When a task's time arrives, the system sends its payload to a designated target—an HTTP endpoint, message queue, or worker fleet.
  • Execution Tracking: Record every attempt, enforce timeouts, retry failed deliveries with backoff, and allow in-flight cancellations.
  • Missed-Run Handling: Define behavior when a job misses its window—fire once immediately, replay all missed occurrences in sequence, or skip entirely. Support explicit backfill requests for historical windows.

Scale & Performance Targets

  • Capacity: 50 million active schedules; peak load of 50,000 triggers per second.
  • Precision: 99% of jobs must dispatch within one second of their scheduled time.
  • Reliability: Guarantee at-least-once execution through node crashes, network splits, and database failovers. Minimize (but tolerate) duplicate dispatches using deterministic identifiers.
  • Fairness: Prevent heavy or bursty schedules from monopolizing resources at the expense of others.

What to Provide

  1. Component Architecture: Identify the key services (API layer, scheduling coordinators, storage, dispatch queues, execution workers) and explain how they interact.
  2. Storage Schema: Define tables or collections for job definitions, materialized run instances, and execution logs.
  3. Partition & Dispatch Logic: Describe how you scan upcoming work, distribute it across scheduler instances, and ensure exactly one coordinator fires each job without duplication or omission.
  4. Failure Handling: Address coordinator crashes, worker timeouts, lease expiration, duplicate dispatch scenarios, daylight saving transitions, and orphaned runs that need reconciliation.

Hints

Hint 1

Think about how to avoid scanning all 50 million jobs every second—what indexing or bucketing strategy keeps lookups fast?

Hint 2

Deterministic run identifiers (derived from job ID and scheduled time) allow multiple components to safely attempt the same insert without creating duplicates.

Hint 3

Consider a two-phase approach: pre-materialize runs shortly before their trigger time to absorb write spikes, then flip their state and enqueue at the exact moment.