TExcelTAD
Home › Monorepo Builds: Bazel (+ Buck2 & Pants) › Field Notes: Caches & CI at Scale

Field Notes: Caches & CI at Scale

A remote cache multiplies whatever goes into it: correct results make every build fast, a wrong result is served to everyone until its key changes. These notes are about keeping the cache trustworthy and CI fast.

1. Keep the remote cache write-protected

Here is why. Two "machines" (two workspaces) build the same target, which has an undeclared input and runs unsandboxed. The first machine uploads its result; the second, whose undeclared file has different content, gets the first machine's output from the cache:

bash · try it yourself
remote-cache start
mkdir -p laptop-a laptop-b
for ws in laptop-a laptop-b; do
  cd ~/$ws
  echo 'module(name = "shop")' > MODULE.bazel
  mkdir -p site
  printf 'genrule(name = "page", outs = ["page.html"], cmd = "text=$(cat site/banner.txt) && echo \\"<h1>$text</h1>\\" > $@", tags = ["no-sandbox"])\n' > site/BUILD.bazel
done
echo "Spring sale" > ~/laptop-a/site/banner.txt
echo "Summer sale" > ~/laptop-b/site/banner.txt
echo "== laptop A builds and uploads"
cd ~/laptop-a && bazel build --remote_cache=http://127.0.0.1:9090 //site:page 2>&1 | grep 'process'
cat bazel-bin/site/page.html
echo "== laptop B, different banner, same action key"
cd ~/laptop-b && bazel build --remote_cache=http://127.0.0.1:9090 //site:page 2>&1 | grep 'process'
cat bazel-bin/site/page.html
remote-cache stop
== laptop A builds and uploads
INFO: 2 processes: 1 internal, 1 local.
<h1>Spring sale</h1>
== laptop B, different banner, same action key
INFO: 2 processes: 1 remote cache hit, 1 internal.
<h1>Spring sale</h1>

Laptop B shows laptop A's banner. Nothing in the action key mentions banner.txt, so both machines computed the same key, and the cache returned what A uploaded. With the sandbox on, the action would have failed on A; with read-only access from developer machines, A's wrong result would never have reached the cache. Both safeguards matter:

Safeguard Setting
only trusted CI writes --remote_upload_local_results=false on developer machines, enforced with read-only credentials on the server
actions are sandboxed no no-sandbox/local without review (Field Notes)
hermetic toolchains different compilers must give different keys; auto-configured host toolchains don't
separate namespaces for untrusted builds --remote_instance_name=pr (or a separate cache) for pull requests from forks
a kill switch know how to flush or rotate the cache (bazel-remote: delete its directory; services: a button or API)

2. Make CI fast without making it wrong

Practice Why
never bazel clean in CI it throws away exactly what makes the next run fast
keep the output base between runs on persistent agents, or rely on the remote cache on ephemeral ones ephemeral agents gain nothing from a local --disk_cache
--remote_download_outputs=minimal (Build without the Bytes) for test-only jobs don't download gigabytes you won't read
--keep_going one broken target shouldn't hide the other failures
share the repository cache (--repository_cache=/cache/repos) on agents downloads of SDKs and archives happen once
test only affected targets on PRs, everything on main affected targets; a full run on main catches what the heuristic missed
shard big test targets (shard_count) and size them parallelism and correct timeouts
watch the cache hit rate per job (BEP/BES dashboards) a sudden drop means a key changed for everyone: a new flag, a non-hermetic tool, a timestamp in a generated file

3. When the hit rate collapses

A common sequence: someone adds --action_env=HOME (or a script writes the date into a generated header), every action key changes, and CI times triple. How to find it:

  1. Compare two execution logs: build the same commit twice with --execution_log_compact_file=a.log / b.log (on two machines, or before and after the change) and diff them with Bazel's execlog parser, or simply run aquery --output=jsonproto for a sample target at both commits and diff the arguments, environment and input digests.
  2. Look for absolute paths, usernames, timestamps and random seeds in command lines and generated files.
  3. Check bazel config diffs between the two builds' configurations.

4. Size and lifetime

Cache Typical size Policy
remote cache (AC + CAS) tens of GB to a few TB LRU with a max size (bazel-remote --max_size), CAS entries kept at least as long as AC entries that reference them
local disk cache 10–50 GB --experimental_disk_cache_gc_max_size, --experimental_disk_cache_gc_max_age
repository cache a few GB rarely needs cleaning; shared by all workspaces on a machine
output bases GBs per workspace bazel clean --expunge in old worktrees; bazel shutdown frees the server's memory

Test yourself

In the session, why did laptop B get laptop A's banner?

  1. A bug in bazel-remote
  2. The undeclared input wasn't part of the action key, so both computed the same key and B received A's uploaded result
  3. B's build was sandboxed
  4. The banner files were identical

Which setting stops developer machines from writing to the shared cache?

  1. --noremote_accept_cached
  2. --remote_upload_local_results=false (enforced server-side with read-only credentials)
  3. --disk_cache=
  4. --keep_going

CI cache hits suddenly drop to near zero for every job. What's a likely cause?

  1. Too many tests
  2. A change that alters every action key, e.g. a new --action_env, an absolute path or a timestamp in a generated file
  3. The remote cache is too big
  4. A new BUILD file