Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

The Build Cache

Status: 🟢 Implemented for the local cache. ⚪ The remote cache is planned; [cache] keys parse but there is no transport yet.

Kargo caches compiled output by content. When the inputs to a compilation have been seen before, the result is restored instead of recompiled.

Two layers that look similar but are not

Artifact cacheBuild cache
Location~/.kargo/cache/~/.kargo/build-cache/
HoldsDownloaded JARs and POMs from MavenYour own compiled classes
Keyed byMaven coordinateHash of all compilation inputs
Cleared bykargo self cleankargo cache clean

This page is about the second one.

How a cache key is computed

A compilation unit's key hashes everything that could change its output:

  • the content of every source file in the unit
  • the resolved dependency set, by coordinate and checksum
  • the full compiler argument list, including profile compiler-args
  • the compiler version
  • the target
  • generated inputs such as BuildConfig and annotation-processor output

Change any one of them and you get a different key. Change none of them and the cached output is, by construction, the output you would have compiled.

Inspecting it

kargo cache stats
Build cache
  Location      ~/.kargo/build-cache
  Entries       1,284
  Size          2.1 GiB
  Hit rate      87% (last 30 days)
kargo cache clean     # drop everything
kargo cache push      # upload current outputs to the remote cache (planned)

Where it pays off

SituationEffect
Switching branches back and forthNear-total hit rate — the old branch's output is still cached
git bisectEvery revisited commit is a hit
Rebuilding after kargo cleanHits, because cleaning does not clear the cache
Editing one file in a large moduleOnly that unit misses
Bumping a dependencyMisses for everything that depends on it, hits elsewhere

Eviction

The cache is LRU-bounded:

# ~/.kargo/config.toml
[cache]
dir = "~/.kargo/build-cache"
max-size = "10GB"

The default is 10 GB. Lower it on a small disk; raise it on a CI machine that builds many branches. Eviction never considers a currently running build's entries.

dir moves the whole cache, which is the setting for a small system disk. kargo cache stats and kargo cache clean read it too, so they report on and clear the directory the build actually writes to. KARGO_HOME moves the default along with everything else Kargo keeps globally.

Remote cache

Status: ⚪ Planned.

A remote cache shares compiled output across machines — most usefully, CI populates it and developers read from it.

# ~/.kargo/config.toml or Kargo.toml
[cache]
local = true
local-dir = "~/.kargo/build-cache"
local-max-size = "10GB"
remote = "https://cache.example.com/kargo"
remote-auth = "${env:KARGO_CACHE_TOKEN}"
remote-push = false     # developers pull; CI pushes

The protocol is deliberately plain: GET /<key> and PUT /<key> against a content-addressable store, so any object store with an HTTP front end works — S3, GCS, a reverse proxy, or a few lines of your own.

Set remote-push = true only on trusted builders. A cache that untrusted clients can write to is a supply-chain hole: whoever can push an entry can choose what your next build compiles to.

kargo cache push        # explicit upload
kargo build --offline   # never consults the remote

When to distrust the cache

Almost never — a correct key makes a stale hit impossible, and a stale hit is a bug rather than something to work around. If a cached build misbehaves and a clean one does not, that is worth filing with the two builds' -v output attached.

To rule the cache out while diagnosing something else:

kargo cache clean && kargo clean && kargo build

Next