sccache - Shared Compilation Cache Cheatsheet
sccache is a compiler cache (in the spirit of ccache) from Mozilla that supports Rust, C/C++, and NVCC. It wraps your compiler, hashes the inputs, and returns a cached object when it has seen the same compilation before. Its distinguishing feature is shared storage backends — S3, GCS, Redis, Azure — so a whole team and your CI fleet share one cache. That turns “CI recompiles everything from scratch every run” into “CI downloads what it already built.”
Installation
| Method | Command |
|---|
| Cargo | cargo install sccache --locked |
| macOS (Homebrew) | brew install sccache |
| Binary | download from GitHub Releases |
| Verify | sccache --version |
Using It with Rust
# One-off
RUSTC_WRAPPER=sccache cargo build
# Persistent (in ~/.cargo/config.toml)
[build]
rustc-wrapper = "sccache"
| Variable | Purpose |
|---|
RUSTC_WRAPPER=sccache | Route rustc through sccache |
SCCACHE_DIR | Local cache directory |
SCCACHE_CACHE_SIZE | Max local cache size (e.g. 20G) |
Using It with C/C++
# CMake
cmake -DCMAKE_C_COMPILER_LAUNCHER=sccache \
-DCMAKE_CXX_COMPILER_LAUNCHER=sccache ..
# Make
make CC="sccache gcc" CXX="sccache g++"
Checking Effectiveness
| Command | Shows |
|---|
sccache --show-stats | Hits, misses, cache size, errors |
sccache --zero-stats | Reset counters before a test build |
sccache --stop-server | Stop the background server |
sccache --start-server | Start it explicitly |
# Measure a cold vs warm build
sccache --zero-stats
cargo build --release
sccache --show-stats # expect misses
cargo clean && cargo build --release
sccache --show-stats # expect hits
The key number is cache hit rate. A low rate usually means something in the inputs is changing (flags, env vars, absolute paths).
Shared Cloud Backends
# S3
export SCCACHE_BUCKET=my-build-cache
export SCCACHE_REGION=us-east-1
export AWS_ACCESS_KEY_ID=... AWS_SECRET_ACCESS_KEY=...
# Redis
export SCCACHE_REDIS=redis://cache.internal:6379
# GCS
export SCCACHE_GCS_BUCKET=my-build-cache
export SCCACHE_GCS_KEY_PATH=/path/to/key.json
| Backend | Env prefix |
|---|
| Local disk | SCCACHE_DIR |
| S3 / S3-compatible | SCCACHE_BUCKET, SCCACHE_ENDPOINT |
| Redis | SCCACHE_REDIS |
| GCS | SCCACHE_GCS_* |
| Azure | SCCACHE_AZURE_* |
| GitHub Actions | SCCACHE_GHA_ENABLED=true |
CI Pattern
# GitHub Actions
env:
SCCACHE_GHA_ENABLED: "true"
RUSTC_WRAPPER: "sccache"
steps:
- uses: mozilla-actions/sccache-action@v0.0.5
- run: cargo build --release
- run: sccache --show-stats
Things That Break Caching
| Cause | Fix |
|---|
| Absolute paths in output | Use --remap-path-prefix |
Changing RUSTFLAGS | Keep flags stable across runs |
| Incremental compilation | Disable it (CARGO_INCREMENTAL=0) for cache use |
| Proc macros / build scripts | Not always cacheable |
| Different compiler versions | Cache keys include the compiler |
sccache vs Alternatives
| Aspect | sccache | ccache | Bazel/Buck |
|---|
| Languages | Rust, C/C++, CUDA | C/C++ | Many (full build system) |
| Shared cache | Cloud backends built in | Mostly local | Remote cache/execution |
| Setup | Wrapper env var | Wrapper | Adopt a new build system |
| Best for | Speeding up existing builds | C/C++ local caching | Large monorepos |
Pairs well with mold — sccache cuts compile time, mold cuts link time.
Resources