How-To Guide

How to Implement Monorepo Build Caching

Cache builds in a monorepo: hash the inputs, reuse the output on a hash hit, cache remotely across CI, and invalidate correctly.

On this page

  1. Introduction
  2. Hash all the inputs of a target
  3. Reuse the output on a cache hit
  4. Share the cache across the machines
  5. Determine the affected targets
  6. Test the hit and the invalidation
  7. Common pitfalls
  8. Conclusion

Introduction

A monorepo has many targets, and a change to one package invalidates only the targets that depend on it, but a naive build rebuilds everything. Build caching makes the affected targets only: each target’s inputs are hashed, and if the hash matches a cached entry, the output is restored instead of rebuilt. In a large monorepo this turns a minutes-long build into seconds, because most targets are unchanged.

This tutorial implements monorepo build caching: hash the inputs, reuse the output on a hit, share the cache across the CI, and test the hit and the invalidation. The examples describe a build cache, and the design applies to any build system that supports content-addressed caching.

Hash all the inputs of a target

The hash must cover every input that affects a target’s output: the source files it builds, the resolved dependencies and their versions, the build configuration, the environment variables that change the output, and the tool version. Omitting an input means a changed build hits a stale cache entry and the output is wrong, which is a silent and confusing failure.

The hash is content-addressed: the same inputs produce the same hash, so a cache entry is keyed by the inputs, not by the file’s timestamp. This is what makes the cache reliable: a timestamp-based cache reuses an output when a file was touched but not changed, and misses when a file changed but the timestamp did not. Hash the content.

function targetHash(target, files, deps, config, toolVersion) {
  const h = createHash('sha256');
  for (const f of files.sort()) h.update(f.path).update(readFileSync(f.path));  // source
  for (const d of deps.sort()) h.update(d.name).update(d.version);              // deps
  h.update(JSON.stringify(config));                                            // config
  h.update(toolVersion);                                                       // the tool
  return h.digest('hex');
}

Reuse the output on a cache hit

On a hit, restore the target’s output from the cache and skip the build, so the target is fast. The output includes the built artifacts and any state the next target needs, so a restored target is indistinguishable from a built one. Record the hash so the next run checks it again.

On a miss, build the target and store its output under the hash, so the next run with the same inputs hits. The store is the counterpart of the restore, and the two together make the cache. Keep the store correct: store the output only on a successful build, so a failed build does not poison the cache with a partial output.

Share the cache across the machines

A local cache speeds the repeated builds on one machine, but the CI runs on fresh machines, so a remote cache is what makes the CI fast. The remote cache is a store the CI jobs and the developers share, so a target built by one job is restored by another, and a target built locally is restored in the CI. This is the cache that turns a monorepo CI from rebuilding everything to restoring the unchanged targets.

The remote cache needs a key, so a job restores from the shared store, and a read-through policy, so a miss builds and stores. The restore is a download, which is fast relative to the build, and the store is an upload. Keep the cache scoped so a job only reads the cache for its project, and prune the old entries so the store does not grow without bound.

# a CI job restores the target's output from the remote cache on a hash hit
# on a miss, it builds and stores the output under the hash
ci build --affected --remote-cache=$CACHE_URL
# the affected targets are the ones whose inputs changed, and the rest are restored

Determine the affected targets

The cache makes a target fast, and the affected-target computation makes only the affected targets run. The build graph knows which targets depend on a changed package, so it rebuilds those and restores the rest from the cache. Together they make a change build only what it affects, which is the monorepo’s promise.

The affected computation uses the dependency graph and the changed files, so a change to a shared library marks its dependents as affected, and a change to a leaf marks only the leaf. A wrong graph over- or under-builds, so the graph is derived from the actual dependencies, not a hand-list. The cache and the affected computation are two halves of a fast monorepo build.

Test the hit and the invalidation

Test that a rebuild with no change is a hit and restores the output without building, by running the build twice and asserting the second is a cache hit. Test that a change to a source file, a dependency version, or the config invalidates the target and it rebuilds, by changing each and asserting a miss. Test that a failed build does not store, so the cache is not poisoned.

# first build: miss and store; second: hit and restore
ci build lib-a        # miss
ci build lib-a        # hit
# change a source file: miss again
echo "// change" >> lib-a/src/index.ts
ci build lib-a        # miss (the input changed)

Common pitfalls

A hash that omits an input so a changed build hits a stale entry, a timestamp-based cache that misses a real change or hits a touch, a local-only cache so the CI never reuses the outputs, storing a failed build so the cache is poisoned, and a wrong dependency graph so the affected targets are wrong are the recurring mistakes. A content-addressed hash over all inputs, a remote shared cache, store-on-success only, and a derived graph address them.

Conclusion

Monorepo build caching makes only the affected targets run and restores the rest. Hash all the inputs of a target, reuse the output on a hit, share the cache across the machines with a remote store, and compute the affected targets from the dependency graph. Self-check by asserting a no-change rebuild is a hit and a source change is a miss, and that a failed build does not store. Then a change in a large monorepo builds only what it affects.

Sources: Nx: Computation caching, Turborepo: Caching, Bazel: Remote caching.

FAQ

What goes into the build hash?
Everything that changes the output: the source files, the resolved dependencies and their versions, the build configuration, the environment variables that affect the build, and the tool's own version. A hash that omits an input reuses a stale output for a changed build.
Local or remote cache?
Both. A local cache speeds the repeated builds on one machine; a remote cache shares the outputs across the CI jobs and the developers, so a target built by one is reused by another. The remote cache is what makes a monorepo CI fast, because the outputs are shared across the runners.

Related Examples