How to Speed Up CI with a Remote Build Cache
Share build outputs between machines so CI rebuilds only what changed, keyed by content rather than by timestamps.
On this page
Introduction
A CI machine starts empty, builds everything, throws the result away, and the next machine repeats the work. If the same inputs produce the same outputs, those outputs could be reused across machines instead of rebuilt. A remote build cache stores outputs keyed by a hash of the inputs and serves them to any machine with a matching key, turning a full rebuild into a download. This article explains what a build cache stores, how content-addressed keys work, how to configure a remote cache, and the hygiene rules that keep a shared cache correct rather than poisoned.
Why CI Rebuilds Everything
Continuous integration favors isolation: each job gets a fresh environment so it cannot be affected by leftover state. That is good for correctness and bad for speed, because a fresh environment has no compiled artifacts, no dependency cache, and no incremental output. Every run rebuilds the whole project even when only one file changed.
Local incremental builds avoid this on a developer machine by keeping state between runs, but CI discards the state. The fix is not to keep state on one machine but to share it: publish build outputs to a cache that any machine can read, keyed so a machine can tell whether its inputs match the ones that produced the cached output.
What a Build Cache Stores
A build cache stores the artifacts a target produces, addressed by a key derived from the target’s inputs: source files, configuration, dependency versions, environment variables, and the command itself. If the key matches, the artifacts are valid without re-running the target. The key is a hash, so it is content-addressed: the same inputs always map to the same key, and any input change maps to a different key.
This is different from a timestamp-based cache, which invalidates when a file’s modification time changes even if its content did not, and which cannot be shared because timestamps are machine-local. Content addressing is what makes a shared cache possible and correct.
A Content-Addressed Remote Cache
A remote cache is a server that stores key-to-artifact mappings. A build tool computes the key for a target, asks the cache, and either downloads the artifact on a hit or runs the target and uploads on a miss. The cache protocol is small: get by key, put by key. The complexity is in the key, not the transport.
# Turborepo remote cache
npx turbo login
npx turbo link
# CI: authenticate and turn on the remote cache
export TURBO_TOKEN="$CI_REMOTE_CACHE_TOKEN"
export TURBO_TEAM="$CI_TEAM"
npx turbo run build --remote-cache-timeout=30
# Nx / generic: set the cache directory or a self-hosted endpoint
export NX_CACHE_DIRECTORY="$HOME/.cache/nx"
npx nx build my-app --parallel --skip-nx-cache=false
# Bazel remote cache (a basic example with a local endpoint)
bazel build //... --remote_cache=http://cache.internal:8080 \
--remote_timeout=60
Correctness of Cache Keys
A cache is correct only if the key covers every input that affects the output. The classic bug is a key that omits an environment variable or a config file, so a build that should differ produces the same key and serves a stale artifact. Override variables deliberately and declare them as inputs. The second bug is a key that includes an input that does not affect the output, which reduces hit rate without harming correctness.
Some inputs are invisible to the tool and must be added manually: the toolchain version, the compiler flags, and the version of the build tool itself. Pin these so a tool upgrade invalidates the cache, and record them in the key so a mismatch is detectable. When in doubt, include more in the key; a redundant input costs a cache miss, a missing one costs a wrong artifact.
{
"$schema": "https://turbo.build/schema.json",
"globalDependencies": [".env", "tsconfig.base.json"], // files in the key
"globalEnv": ["CI", "NODE_ENV", "NEXT_PUBLIC_API_URL"], // env vars in the key
"tasks": {
"build": {
"env": ["BUILD_TARGET"], // per-task env that changes the output
"inputs": ["src/**", "tsconfig.json", "package.json"],
"outputs": ["dist/**"],
"dependsOn": ["^build"]
}
}
}
Cache Hygiene and Poisoning
A shared cache is shared trust. If an untrusted pull request can push artifacts, it can seed the cache with output built from modified code under a key that a trusted build will later use. The mitigations are to restrict who may write to the cache, keep read and write permissions separate, and never let a fork build write to the main cache. Sign or scope the cache per trust boundary.
Set a retention policy so artifacts do not accumulate forever, and evict on size as well as age. Cache growth is a cost problem, not a correctness one, but an unbounded cache eventually slows the lookups and costs storage. Track the size and prune old keys, keeping the keys your default branch still uses.
Measuring Hit Rate
A remote cache is only worth it if the hit rate is high. Measure the fraction of targets that hit the cache versus miss, and the time saved versus the time spent uploading and downloading. A low hit rate usually means the key includes something that changes every run, such as a timestamp or a random build id, which defeats content addressing; find and remove it.
Also measure the cost: a large artifact uploaded on every miss and downloaded on every hit can cost more bandwidth than rebuilding a small target. Cache the expensive targets and let cheap ones rebuild. The goal is to spend less total time, not to maximize hits for their own sake.
Conclusion
A remote build cache turns CI from rebuild-everything into download-what-changed, but only if the key is content-addressed and complete. Derive the key from every input that affects the output, pin the toolchain version into the key, restrict who can write to the cache so an untrusted build cannot poison it, set a retention and size policy, and measure the hit rate along with the bandwidth cost. Then a fresh CI machine starts warm, and the pipeline spends its time on what actually changed.
Sources: Turborepo: Remote Caching, Nx: computation caching, Bazel: remote caching.
FAQ
- What makes a build cache key correct?
- It must include every input that affects the output, including source, configuration, dependency versions, the toolchain version and relevant environment variables. Omitting an input risks serving a stale artifact; including an input that does not affect the output only lowers the hit rate.
- Can an untrusted pull request poison a shared build cache?
- If it can write to the cache, yes: it can seed artifacts built from modified code under a key a trusted build later reuses. Restrict write access, keep fork builds read-only or on a separate cache, and scope the cache per trust boundary.
Report a problem with this articleOpens a pre-filled issue on GitHub.