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.
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.
| Choice | What it buys | What it costs |
|---|---|---|
| Branch Tarball Cache | Single zip upload | Massive redundant transfer; race-prone overwrites |
| CAS + Action Cache | Global deduplication; hermeticity | Requires 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()
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.
- 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.
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: Make the trade-off yourself.
Why does Bazel's Action Digest include environment variables and command flags alongside input file hashes?