TExcelTAD

The Disk Cache

The action cache in the output base belongs to one workspace and disappears with bazel clean. The disk cache is a separate, content-addressed directory that stores action results and output files keyed by action key and digest. Any workspace on the machine that points at it can reuse them: a second clone of the repository, a worktree for a code review, or the same repository after bazel clean.

text
--disk_cache=~/.cache/bazel-disk
  ac/   action key  → action result (exit code, output digests, stdout/stderr digests)
  cas/  file digest → file contents

It's the same data model as a remote cache (next lesson), just on local disk, which is why the flags and the summary lines look alike.

Clean, rebuild, and a second clone

bash · try it yourself
mkdir -p repo && cd repo
echo 'bazel_dep(name = "rules_cc", version = "0.2.27")' > MODULE.bazel
mkdir -p lib app
printf 'load("@rules_cc//cc:defs.bzl", "cc_library")\ncc_library(name = "math", srcs = ["math.cc"], hdrs = ["math.h"], visibility = ["//visibility:public"])\n' > lib/BUILD.bazel
printf '#pragma once\nint triple(int x);\n' > lib/math.h
printf '#include "lib/math.h"\nint triple(int x) { return 3 * x; }\n' > lib/math.cc
printf 'load("@rules_cc//cc:defs.bzl", "cc_binary")\ncc_binary(name = "app", srcs = ["main.cc"], deps = ["//lib:math"])\n' > app/BUILD.bazel
printf '#include <cstdio>\n#include "lib/math.h"\nint main() { std::printf("%%d\\n", triple(14)); }\n' > app/main.cc
echo 'build --disk_cache=~/.cache/bazel-disk' > .bazelrc
echo "== first build: everything executes"
bazel build //app 2>&1 | grep -E 'process'
echo "== after bazel clean"
bazel clean 2>/dev/null
bazel build //app 2>&1 | grep -E 'process'
echo "== a second clone in another directory"
cp -r ../repo ../review && cd ../review && rm -f bazel-*
bazel build //app 2>&1 | grep -E 'process'
./bazel-bin/app/app
ls ~/.cache/bazel-disk
== first build: everything executes
INFO: 8 processes: 5 internal, 3 processwrapper-sandbox.
== after bazel clean
INFO: 8 processes: 3 disk cache hit, 5 internal.
== a second clone in another directory
INFO: 8 processes: 3 disk cache hit, 5 internal.
42
ac
cas
tmp

After bazel clean, the three real actions (two compiles and a link) were disk cache hits instead of executions. The second clone has a different output base (its path hash differs), so its own action cache is empty, and its build was served from the disk cache too: a branch switch, a worktree or a fresh clone costs seconds instead of a full rebuild.

What changes the key

Disk cache hits require identical action keys, so anything that changes command lines or inputs between workspaces breaks sharing:

Breaks sharing Why
different compiler, flags or --action_env values part of the key
absolute paths in command lines different checkout paths give different keys (Bazel's own rules use relative paths)
--noincompatible_strict_action_env the user's PATH enters every key
generated files with timestamps different input digests downstream
stamping (--stamp) stamped actions see volatile data

The aquery lesson shows how to compare two keys when hits are missing.

Keeping it small

The disk cache grows without bound by default. Bazel 7.4+ can collect garbage itself:

text
# .bazelrc (or ~/.bazelrc)
build --disk_cache=~/.cache/bazel-disk
build --experimental_disk_cache_gc_max_size=20G      # delete least recently used entries above this size
build --experimental_disk_cache_gc_max_age=14d       # and entries unused for 14 days

The collection runs when the server has been idle for a while (--experimental_disk_cache_gc_idle_delay, default 5 minutes). Before those flags existed, teams ran a cron job with find ~/.cache/bazel-disk -atime +14 -delete; that still works.

Tip: Put --disk_cache in ~/.bazelrc (personal) or user.bazelrc rather than the shared .bazelrc: CI machines with a remote cache usually don't want a local disk cache that grows on every agent.

Test yourself

What does the disk cache add on top of Bazel's action cache?

  1. Nothing
  2. A shared, content-addressed store that survives bazel clean and is shared by all workspaces using it
  3. Faster analysis
  4. Remote execution

A second clone of the repository builds with the same --disk_cache. Why does it get cache hits even though its output base is new?

  1. Bazel copies the output base
  2. Action keys depend on relative paths and content, not on the checkout location
  3. The disk cache stores whole workspaces
  4. It doesn't

How do you stop the disk cache from growing forever with modern Bazel?

  1. bazel clean
  2. --experimental_disk_cache_gc_max_size / --experimental_disk_cache_gc_max_age
  3. --disk_cache=
  4. Delete MODULE.bazel.lock