The Physics of Build Caching: kache, Reflinks, and a Zombie Daemon
Field notes from an ~800-crate Tauri 2 + Rust workspace: content-addressed keys, APFS reflink zero-copy restores, and a 4-day silent cache outage caused by a brew upgrade.
- kache = compiler wrapper + blake3 content-addressed store + APFS reflink zero-copy restore + optional daemon. It caches deterministic mappings from *normalized compilation inputs* to *output artifacts*, rather than raw command strings.
- The primary disk win comes from `reflink`, not hit rate alone: restored artifacts share physical extents with store blobs (`du` reports logical size; `df` reports physical blocks)—a 20 GB `target/` can occupy less than 1 GB of real disk.
- Cargo never garbage-collects `target/` by design. Every feature combination and source edit leaves behind an immortal unit directory. Our workspace accumulated 4,055 unit directories / 19 GB, with fewer than one-third still live.
- The sharpest failure mode is daemon lifecycle drift: `brew upgrade` replaces the binary on disk without restarting the running `kache-engine`. A zombie daemon holds `daemon.run.lock` and silently drops cache hit rates to 1% for days with zero build errors.
kache is not "just another sccache." It is a local-first build artifact manager built around content-addressed storage and copy-on-write filesystems. These field notes are grounded in measurements from our Tauri 2 + Rust workspace (~800 crates in the dependency graph).
Why Rust Workspaces Need Artifact Governance
Cargo's Append-Only Cache Philosophy
Cargo's target/ directory behaves like an append-only log, not a managed database. Its freshness check (fingerprint + dep-info) accurately decides what can be reused, but it never deletes artifacts that used to be useful.
In our workspace (~100 tests/*_contract.rs integration binaries, multiple feature flags, and --all-targets runs), target/debug/build/ grew to 19 GB across 570+ package directories, with 371 historical unit hashes for a single crate (dijiang-studio), each dragging an ~400 MB libapp_lib.rlib copy.
kache's first job is not raw compilation speed—it is making an append-only target/ tree governable.
How kache Differs from sccache / ccache
| Dimension | sccache / ccache | kache |
|---|---|---|
| Philosophy | Transparent compiler proxy storing .o files locally or in S3 |
Compiler wrapper + filesystem-level artifact governance |
| Granularity | Individual crate compilation outputs | Unit artifacts + build script execution + incremental graphs + target/ lifecycle |
| Local Disk Footprint | Copies artifacts into target/ |
APFS reflink sharing (20 GB logical can be < 1 GB physical) |
| Governance | None (cache hits only) | clean --units / --stale / --orphans / disk-recovery |
| Daemon | Stateless client | Stateful engine: periodic GC, quota eviction, target registry, liveness receipts |
Four-Layer Architecture
- Access Layer (User-Level Wrapper): Configured in
~/.cargo/config.toml(rustc-wrapper = "kache",HOST_CC = "kache cc"), intentionally kept out of project-level.cargo/config.tomlso CI, sandboxes, and contributors withoutkachedegrade gracefully to uncached builds. - Key Layer (Content Addressing): Hashes normalized inputs with
blake3—source content, dependency metadata,--cfg, feature sets, profiles,rustcfingerprints, whitelisted env vars, and linker/SDK paths (with-ffile-prefix-mappath normalization). - Storage Layer (Store + APFS
FICLONE): Blobs live in~/Library/Caches/kache/store/alongside an SQLite WALindex.db. Restores useclonefile(FICLONE), sharing physical extents until written. Always checkdfandkache clean -n(ON DISKcolumn) instead ofdu. - Control Plane (
daemon): Handles periodic GC (gc_max_age_hours), quota eviction (local_max_size),disk-recovery(emergency eviction below a free-space floor), worktreetarget/registry, and liveness receipts (KACHE_TARGET_LIVENESS=1).
If the daemon dies, builds keep succeeding (the wrapper reads and writes the store directly), but all governance halts.
Practical Configuration & Pitfalls
# ~/.cargo/config.toml (user-level only)
[build]
rustc-wrapper = "kache"
[env]
HOST_CC = "kache cc"
HOST_CXX = "kache c++"
CC_KNOWN_WRAPPER_CUSTOM = "kache"
KACHE_TARGET_LIVENESS = "1"
# ~/.config/kache/config.toml
[cache]
local_max_size = "8 GiB"
target_liveness = true
auto_clean_unused_units_days = 7
auto_clean_idle_targets_days = 30
auto_clean_orphaned_targets = true
auto_recover_min_free_bytes = 10737418240 # 10 GiB safety floor
gc_max_age_hours = 168
Key operational rules we learned the hard way:
- Read time hit rate, not just count hit rate: Small crates inflate count hits, while a single
aws-lc-syshit outweighs a hundredserdehits. - Disable build-script execution caching when side effects matter: Caching stdout without running the script broke our embedded git commit hash (
DIJIANG_GIT_COMMIT) and skipped anffmpegcheck. SetKACHE_BUILD_SCRIPT_CACHE=0. - Keep free disk well above the recovery floor: When free space hovers near the 10 GiB floor,
disk-recoveryevicts entries every 5 minutes (we watched our store shrink from 789 entries down to 20).
Incident Postmortem: How a "Working" Cache Died Silently
On 2026-11-26, kache 2.3.1 was installed, builds were running daily, and zero errors appeared—yet kache stats reported a 1% hit rate (27 / 2,621 crates) and only 10.6 seconds saved over 24 hours.
Tracing the chain:
brew upgrade kacheswapped the Cellar binary from2.3.0to2.3.1, butlaunchdKeepAliveonly restarts exited processes.- The old
kache-engine2.3.0stayed alive for 4+ days, holdingdaemon.run.lockwhileipc/d.sockvanished. - Every
kacheCLI invocation failed to start the new daemon (did not start after recovery), leaving GC, quotas, and liveness receipts paralyzed. - Meanwhile,
disk-recoverykept evicting the store down to ~20 entries (~945 MB / 8 GiB), collapsing the hit rate to 1%.
Immediate fix: kill the zombie kache-engine process, run kache daemon start, and verify Daemon v2.3.1.
Permanent fix: We wired a lightweight version guard (kache-daemon-guard.mjs) into our build entrypoints (_build-env.js and cargo-mem.sh) with version normalization, an exclusive wx lock, readiness polling, and 60-second throttling.
Caching systems rarely fail loudly—they fail silently by missing slowly. The final step of adopting any build cache is wiring a self-check guard at the entrypoint.