KatInterview

Design CI/CD Build Caching System

System DesignModel answer
MediumModel answerSystem Designsystem-designcachingdistributed-systems

Last updated

Design a distributed build caching system for CI/CD pipelines, covering cache key generation, concurrent upload coordination, and tiered storage architecture.

Design a distributed build caching platform for an enterprise CI/CD service executing tens of thousands of pipeline jobs daily across diverse runtimes (Node.js, Rust, Go, Python, and C++).

The system aims to accelerate pipeline execution by storing and retrieving intermediate compilation and dependency artifacts (such as package manager directories, object files, and pre-built container layers) between worker runs.

Core Requirements

Functional Requirements

  1. Artifact Transfer: CI worker agents must download existing cache bundles before executing build steps and upload newly generated bundles after successful steps.
  2. Deterministic Keying: Compute unique, collision-free cache keys based on input states (e.g., source tree hashes, toolchain versions, OS/architecture, lockfiles). Support fallback prefix matching for partial restores.
  3. Lifecycle & Eviction: Define clean-up policies (e.g., TTLs, LRU) to constrain total storage consumption across projects.

Non-Functional & Resilience Requirements

  1. Fail-Open Operational Model: Caching is an optimization. Cache failures, high latency, or total service outages must never fail a CI pipeline; agents must safely fall back to clean builds.
  2. High Read Throughput: Downloading cached archives must be orders of magnitude faster than building from source, even for multi-gigabyte payloads.
  3. Concurrency Control: Gracefully handle duplicate writes when concurrent pipelines finish identical steps at the same time ("thundering herd" / cache stampede).

Requirements for your Design

  1. API Design: Define RESTful endpoints for cache checking, lease reservation, streaming transfers, and committing artifacts.
  2. Storage Layer: Propose a scalable architecture separating control plane metadata from bulk object storage.
  3. Keying & Match Logic: Detail how deterministic keys are generated and how partial prefix fallbacks operate.
  4. Concurrency & Failures: Explain the state machine and leasing mechanisms used to resolve race conditions between concurrent pipeline steps.

Hints

Hint 1

Decouple control plane metadata from data plane bytes: return presigned storage URLs so workers download directly from object storage without bottlenecking API servers.

Hint 2

Make final payload storage immutable and content-addressable (keyed by payload SHA-256) to enable implicit deduplication across different projects.

Hint 3

Coordinate concurrent writes with a short-lived lease state machine (PENDING -> COMMITTED) using atomic conditional updates (CAS).