KatInterview

Build a Distributed Pipeline Execution Engine

System DesignModel answer
HardModel answerSystem Designdistributed-systemsschedulingstate-machines

Last updated

Design a fault-tolerant scheduler that executes dependency graphs across a worker fleet with lease-based dispatch and deployment safety.

You're building the core execution engine for a platform that runs automated build, test, and deployment workflows. Engineers submit jobs as dependency graphs—each node is a task, and edges represent "must run after" relationships. Your system must assign tasks to machines in a worker pool, track progress, recover from crashes, and enforce safety rules when deploying to shared environments.

Your Job

Design the scheduler and execution runtime. Explain how tasks move from submitted to complete, how workers claim and execute them, and how the system stays correct under partial failures.

Functional Scope

Dependency orchestration
Jobs arrive as directed acyclic graphs. A task becomes eligible only after all its dependencies finish successfully. If any dependency fails and cannot be retried, cancel the entire job immediately.

Worker assignment
Workers advertise capabilities—operating system, architecture, attached hardware. Tasks declare requirements. The scheduler matches tasks to compatible workers and grants short-lived execution leases.

Fault tolerance
Workers may crash, hang, or become partitioned. The scheduler must detect stalled tasks, reassign them, and prevent duplicate or conflicting executions. Tasks should be retried with exponential backoff up to a configured limit.

Real-time visibility
Clients query job and task status at any time. State transitions—queued, running, succeeded, failed, cancelled—must be observable and durable.

Deployment guards
Some tasks deploy code to shared target environments (staging, production). The system must serialize or gate concurrent access so two tasks never deploy conflicting changes to the same environment simultaneously.

Scale & Constraints

  • 50,000 jobs per day, averaging 10 tasks each → 500,000 tasks daily
  • Peak dispatch rate: 50 tasks/second
  • Up to 5,000 concurrent workers
  • Task duration: 30 seconds to 2 hours
  • Multi-tenant: prevent one large job from blocking others
  • At-least-once task dispatch; tolerate scheduler restarts without state loss

What to Address

  1. State model: Schema for jobs, tasks, dependencies, worker leases, and deployment locks; task and job lifecycle states
  2. Scheduling logic: How the system identifies ready tasks, matches them to workers, and dispatches them safely
  3. Lease protocol: Worker heartbeats, timeout detection, and reassignment of abandoned tasks
  4. Deployment safety: Mechanism to prevent concurrent deploys to the same environment and handle ordering or mutual exclusion
  5. Fairness: Strategy to balance work across tenants when workers are saturated

You may assume artifact storage (logs, binaries) is handled separately. Focus on the control plane.

Hints

Hint 1

Model task readiness with an in-degree counter that decrements transactionally when dependencies complete

Hint 2

Use conditional updates (compare-and-swap on a lease token) to make worker operations idempotent under retries

Hint 3

A separate lock table with fencing tokens can serialize deploys; the token travels to the target and rejects stale operations