TExcelTAD
Home › Monorepo Builds: Bazel (+ Buck2 & Pants) › Remote Caching with bazel-remote

Remote Caching with bazel-remote

A remote cache is the disk cache on a server: CI uploads the results of every action it runs, and every developer and CI agent downloads results instead of rebuilding. In a large repository this is the single biggest speed-up Bazel offers, usually turning most of a CI build into downloads. It works because action keys are deterministic and machine-independent (previous lessons).

The protocol is the Remote Execution API (REAPI), a set of gRPC services; its caching half is two stores:

Store Key Value
Action Cache (AC) the action digest (hash of command, inputs root, platform) an ActionResult: exit code, output file digests, stdout/stderr digests
Content Addressable Storage (CAS) a file's SHA-256 digest the file's bytes

Bazel also speaks a simpler HTTP protocol (GET/PUT /ac/<hash> and /cas/<hash>), which many caches (and plain nginx or S3 setups) support.

bazel-remote

bazel-remote is a small open-source cache server: one Go binary, HTTP and gRPC, an on-disk LRU store with optional S3/GCS/Azure backends. The course image has bazel-remote 2.6.4 and a helper script: remote-cache start runs it on 127.0.0.1:9090 (HTTP) and 9092 (gRPC) with an empty store.

Upload once, hit everywhere

bash · try it yourself
mkdir ws && cd ws
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", "cc_test")\ncc_library(name = "math", srcs = ["math.cc"], hdrs = ["math.h"], visibility = ["//visibility:public"])\ncc_test(name = "math_test", size = "small", srcs = ["math_test.cc"], deps = [":math"])\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 '#include "lib/math.h"\nint main() { return triple(2) == 6 ? 0 : 1; }\n' > lib/math_test.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
cat > .bazelrc <<'EOF'
build:remote --remote_cache=http://127.0.0.1:9090
build:remote --remote_upload_local_results=true
EOF
remote-cache start
echo "== CI: build and test, uploading results"
bazel test --config=remote //... 2>&1 | grep -E 'process|PASSED'
echo "== a fresh machine: clean --expunge, then the same build"
bazel clean --expunge 2>/dev/null
bazel test --config=remote //... 2>&1 | grep -E 'Invocation ID|process|PASSED'
./bazel-bin/app/app
curl -s http://127.0.0.1:9090/status | jq -r '"cache entries: \(.NumFiles)"'
remote-cache stop
== CI: build and test, uploading results
INFO: 18 processes: 10 internal, 9 processwrapper-sandbox.
//lib:math_test                                                          PASSED in 0.0s
== a fresh machine: clean --expunge, then the same build
INFO: Invocation ID: 4440784f-6449-49c5-a006-b3f701421b7a
INFO: 18 processes: 9 remote cache hit, 10 internal.
//lib:math_test                                                 (cached) PASSED in 0.0s
42
cache entries: 40

The second run started from nothing: clean --expunge deleted the output base, the action cache, even the Bazel server (hence the new Invocation ID line that remote builds print). Every compile and link was a remote cache hit, and the test result itself came from the cache: (cached) PASSED without running the test. That's how a CI pipeline with a warm cache "builds" a large repository in a minute.

The flags that matter

Flag Purpose
--remote_cache=grpc://host:9092 or http://…, grpcs://… where the cache is
--remote_upload_local_results (default true) upload results of actions run locally; set false on developer machines
--noremote_accept_cached don't use hits (to check whether a failure is cache-related)
--remote_header=Authorization=Bearer …, --credential_helper=…, --tls_client_certificate authentication
--remote_download_outputs=toplevel (default) / minimal / all "Build without the Bytes": download only the outputs you asked for, not every intermediate file
--remote_timeout=60s, --remote_retries=5 network robustness
--remote_local_fallback build locally if the cache is unreachable
--disk_cache together with --remote_cache a local layer in front of the remote one

Who may write?

A cache entry is trusted by everyone who reads it, so a poisoned entry (from a machine with a modified compiler, an uncommitted change in an undeclared input, or malware) would spread to every build. The standard setup:

Client Reads Writes
CI on the main branch (trusted, hermetic agents) yes yes
CI on pull requests yes optionally, to a separate cache or namespace (--remote_instance_name=pr)
Developer laptops yes no: --remote_upload_local_results=false, enforced by read-only credentials on the server

bazel-remote supports this with HTTP basic auth or mTLS and an "unauthenticated reads" mode; commercial services (BuildBuddy, EngFlow, NativeLink) offer per-key permissions.

text
# .bazelrc
build:ci  --remote_cache=grpcs://cache.example.com
build:ci  --remote_upload_local_results=true
build:dev --remote_cache=grpcs://cache.example.com
build:dev --remote_upload_local_results=false

Test yourself

What are the two stores of a REAPI cache?

  1. Git and S3
  2. The Action Cache (action digest → result) and the CAS (content digest → bytes)
  3. A lockfile and a registry
  4. Logs and metrics

Why should developer machines usually not upload to the shared remote cache?

  1. Bandwidth only
  2. Their results could be non-hermetic or tampered with and would then be served to everyone
  3. Uploads are slower than builds
  4. Bazel forbids it

What does --remote_download_outputs=minimal do?

  1. Disables the cache
  2. Doesn't download intermediate outputs, only what is needed (Build without the Bytes)
  3. Compresses outputs
  4. Downloads only test logs