Skip to main content
← CONCEPTS / SYSTEM-DESIGN
An end-to-end domain learning journey

Content-Addressed Storage (CAS) &
Merkle Action Digests.

Mutable branch-named build caches suffer cross-PR cache poisoning and redundant multi-gigabyte uploads; content-addressed SHA-256 Action Digests deduplicate blobs globally and guarantee hermetic cache hits.
Start the journey2 sections· Last modified Dec 3, 2026
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

When operating a CAS build cache, refresh blob lease timestamps inside FindMissingBlobs to prevent GC TOCTOU failures.

TL;DR30-second simplified summary
▾

Delivers >92% cache hit rates and safe petabyte-scale garbage collection.

Before you begin · Building blocksAlready know these? Jump to Stage 01 ↓
SHA-256 Content Hashing
Maps arbitrary byte blobs to collision-resistant 256-bit identifiers.
Merkle Directory Trees
Hashes file trees bottom-up so unchanged subtrees keep identical root digests.
Full walkthrough · 2 stages
01
STAGE 01beginner

Action Cache vs. Content-Addressed Storage.

Why this matters: Separate mutable action mappings from immutable content blobs.

AC maps ActionDigest -> ActionResult (output file digests). CAS stores immutable SHA-256 blobs so two builds producing the same binary share one physical copy.

Before · Mutable Branch Tarballs
After · Action Cache + CAS
ChoiceWhat it buysWhat it costs
Branch Tarball CacheSingle zip uploadMassive redundant transfer; race-prone overwrites
CAS + Action CacheGlobal deduplication; hermeticityRequires GC lease pinning
import hashlib, json

def compute_action_digest(cmd: list, env: dict, input_merkle_root: str) -> str:
    payload = json.dumps({"cmd": cmd, "env": sorted(env.items()), "root": input_merkle_root}, sort_keys=True)
    return hashlib.sha256(payload.encode()).hexdigest()
02
STAGE 02advanced

GC Lease Pinning Against TOCTOU Races.

Why this matters: Prevent garbage collection from deleting blobs mid-build.

If GC deletes a cold blob right after FindMissingBlobs reports it present, the worker fails. Refreshing a 15-minute ref_lease_until_ts on every query closes the TOCTOU window.

Engineering Decision Framework
Problem
How to garbage-collect petabytes of old CAS blobs without deleting inputs still needed by in-flight CI builds.
Naive Solution
Delete any CAS blob older than 7 days based solely on creation timestamp.
Why It Fails
Frequently reused base toolchain blobs created 30 days ago get deleted mid-build right after FindMissingBlobs says they exist (TOCTOU race).
Alternatives
Creation-time TTL, stop-the-world mark-and-sweep, or ref_lease_until_ts pinning refreshed on every AC/CAS read.
Engineering Decision
Attach a 15-minute ref_lease_until_ts to every CAS blob and refresh it atomically on FindMissingBlobs and ActionResult hits.
Trade-Offs
Requires lightweight lease timestamp updates on hot blobs, but eliminates TOCTOU build failures with zero stop-the-world pauses.
Decision Exercise · Make the Call

A 30-day-old compiler toolchain blob is reported present by FindMissingBlobs, but GC deletes it 2 seconds later before the worker reads it. How do you prevent this?

public boolean isBlobPinned(long refLeaseUntilMs, long nowMs) {
    return refLeaseUntilMs > nowMs; // GC skips blobs with active build leases
}
Practice · Explain the consequences

Practice: Make the trade-off yourself.

Question 1 of 3
Score: 0/3

Why does Bazel's Action Digest include environment variables and command flags alongside input file hashes?

Recommended Learning Flow