Remote Caching Setup
1. Architecture & Prerequisites
Define cache topology, network boundaries, and storage backends before integrating with your Monorepo Architecture & Orchestration strategy. Remote caching requires strict network isolation and deterministic key generation to prevent artifact corruption.
Pre-Deployment Checklist:
- Validate TLS 1.2+ termination at the load balancer or edge proxy.
- Configure IP allow-listing for CI runners and developer workstations.
- Implement automated token rotation (max 90-day expiry) via your secrets manager.
# Verify TLS handshake and certificate chain
openssl s_client -connect cache.yourdomain.com:443 -servername cache.yourdomain.com </dev/null 2>/dev/null | openssl x509 -noout -dates
# Validate network latency to cache endpoint (target <50ms)
curl -o /dev/null -s -w "time_connect: %{time_connect}\ntime_starttransfer: %{time_starttransfer}\n" https://cache.yourdomain.com/health
2. Tool-Specific Configuration
Map pipeline outputs to deterministic cache keys. Explicitly declare cacheable tasks, output directories, and environment hash dependencies. Avoid implicit caching of non-deterministic artifacts.
Turborepo (v2.0+)
Align task definitions with Turborepo Pipeline Configuration to enforce strict output boundaries.
// turbo.json
{
"$schema": "https://turbo.build/schema.json",
"remoteCache": {
"enabled": true,
"signature": true,
"timeout": 30000
},
"tasks": {
"build": {
"outputs": ["dist/**", ".next/**"],
"inputs": ["src/**", "package.json", "tsconfig.json"]
},
"test": {
"outputs": ["coverage/**"],
"inputs": ["src/**", "tests/**"]
}
}
}
Critical Fields: signature: true enforces cryptographic verification. timeout prevents hanging on degraded cache endpoints. Explicit outputs arrays prevent partial artifact restoration.
Nx (v17+)
Configure workspace runners to match your Nx Workspace Architecture.
// nx.json
{
"tasksRunnerOptions": {
"default": {
"runner": "nx-cloud",
"options": {
"cacheableOperations": ["build", "test", "lint"],
"accessToken": "${NX_CLOUD_ACCESS_TOKEN}"
}
}
}
}
Critical Fields: cacheableOperations must exclude e2e or network-dependent tasks. accessToken resolves at runtime; never commit plaintext values.
3. CI/CD Integration & Secret Management
Inject cache credentials securely via CI environment variables. Enforce read/write scopes per branch and configure fallback behavior for cache misses.
# .github/workflows/ci.yml
jobs:
build:
runs-on: ubuntu-latest
env:
TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
TURBO_TEAM: ${{ secrets.TURBO_TEAM }}
TURBO_REMOTE_CACHE_SIGNATURE_KEY: ${{ secrets.CACHE_SIGNATURE_KEY }}
NX_CLOUD_ACCESS_TOKEN: ${{ secrets.NX_CLOUD_TOKEN }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'pnpm'
- run: pnpm install --frozen-lockfile
- name: Build with Remote Cache
run: pnpm turbo run build --cache-dir=.turbo --continue --concurrency=4
timeout-minutes: 15
Security & Scope Enforcement:
- Branch Scoping: Restrict
writeaccess tomain/release/*. Feature branches should operate inread-onlymode to prevent cache poisoning. - Fallback Logic: Use
--continue(Turborepo) or--skip-nx-cache=false(Nx) to gracefully degrade to local execution on transient network failures. - Secret Rotation: Automate credential rotation via GitHub Actions OIDC or Vault. Never store static tokens in repository settings.
4. Validation & Performance Tuning
Verify cache hit rates, monitor artifact integrity via cryptographic signatures, and implement automated eviction policies. Reference Optimizing Turborepo Remote Cache for CI for advanced throughput adjustments, network compression, and parallel upload strategies.
Diagnostic Commands:
# Inspect cache hit/miss ratios locally
turbo telemetry status
# Validate artifact integrity against signature key
openssl dgst -sha256 -verify cache-signature.pub -signature artifact.sig artifact.tar.gz
# Force cache purge for a specific project (Nx)
npx nx reset
Performance Tuning Parameters:
- Concurrency: Set
--concurrencyto$(nproc)or CI runner vCPU count. Over-provisioning causes I/O bottlenecks. - Compression: Enable
zstdorgzipat the proxy layer. Turborepo v2+ natively supports compressed artifact streaming. - Eviction Policy: Implement tiered retention:
main(90 days),feature(14 days),stale/failed(0 days).
Common Pitfalls & Mitigation
| Mistake | Impact | Remediation |
|---|---|---|
| Hardcoding cache tokens in config files | Credential leakage, supply chain compromise | Inject via CI secrets manager or OIDC federation |
Missing explicit outputs arrays |
Corrupted/partial artifact restoration | Define exact output globs per task in turbo.json/nx.json |
Unrestricted write access on PRs |
Cache poisoning, malicious artifact injection | Enforce read-only for non-protected branches |
| Ignoring network timeouts/retries | Silent CI hangs on degraded endpoints | Set timeout: 30000 and implement exponential backoff |
| Caching non-deterministic tasks | Flaky builds, invalid cache hits | Exclude network-dependent tests; seed RNGs explicitly |
Frequently Asked Questions
Q: How do I prevent cache poisoning in a shared monorepo? A: Enforce branch-scoped read/write permissions, require cryptographic signatures for all cached artifacts, and implement strict CI pipeline validation before promoting cache entries to the main branch.
Q: What is the recommended cache retention policy for production builds? A: Implement a tiered eviction strategy: retain main branch artifacts for 90 days, feature branch artifacts for 14 days, and purge unverified or failed builds immediately.
Q: Can I use a self-hosted remote cache instead of a SaaS provider? A: Yes. Deploy a compliant cache server with TLS termination, IP allow-listing, and automated token rotation. Ensure the server implements the standard remote cache HTTP API for artifact upload/download.
Q: How do I handle cache misses during initial CI runs? A: Configure fallback to local execution with explicit retry limits. Use scheduled cache-warming pipelines to pre-populate artifacts for critical dependency graphs.