In a monorepo, as code grows, compile times increase. Developers waste time rebuilding packages that haven't changed. Turborepo solves this by caching the output of your tasks. But caching locally on one machine isn't enough; you need Remote Caching. Here is how to build a dynamic caching system for distributed teams.
How Turborepo Caching Works
Turborepo hashes the inputs of a task (source files, dependencies, environment variables). If the hash matches a previous run, Turborepo skips running the task and restores the output from the cache.
[Developer 1] -> Runs build -> Hashes source -> Saves to Remote Cache (Cloud)
|
[Developer 2] -> Pulls code -> Hashes source -> Cache Hit! -> Restores instantly
To enable remote caching in your project:
# Build with remote caching enabled
turbo run build --api="https://api.venelx.com/cache" --token="$TURBO_TOKEN"
Configuring turbo.json for Absolute Cache Control
A poorly configured turbo.json file leads to cache misses. Here is an optimized layout specifying build and test environments:
{
"$schema": "https://turbo.build/schema.json",
"pipeline": {
"build": {
"dependsOn": ["^build"],
"outputs": [".next/**", "dist/**", "build/**"],
"inputs": ["src/**/*", "public/**/*", "package.json"]
},
"test": {
"dependsOn": ["build"],
"outputs": [],
"inputs": ["src/**/*.test.ts", "jest.config.js"]
},
"lint": {
"outputs": [],
"inputs": ["src/**/*", ".eslintrc.json"]
}
}
}
Optimizing Remote Cache Hit Rates
To ensure high cache hit rates across distributed developer teams:
- Define Strict Inputs: In your
turbo.json, only include files that actually affect the output. Don't include logs, test outputs, or markdown files in build hashes. - Normalize Env Variables: Ensure environment variables (like compile targets) are aligned. If one developer builds with
NODE_ENV=developmentand another withproduction, cache hashes will mismatch. - Cache Pipeline Artifacts: Pre-warm CI runs by downloading dependencies and restoring previous node_modules in under 3 seconds.
For comparison on virtualized vs. bare-metal speed impacts on caching, read M4 Apple Silicon compilation speeds.
References & Citations
- Turborepo Core Documentation: Vercel Turborepo
- Distributed Caching Research: IEEE Software Architecture Journal
- Next.js static vs dynamic performance: Static Export Guide