intermediate

Caching

Cache dependencies and task outputs only when cache keys include every input that affects correctness.

Pipeline caching saves dependency downloads or deterministic task outputs. Correct cache keys include lockfiles, source inputs, config, platform, runtime version, and any environment variable that affects output.

| Cache type | Key inputs | |------------|------------| | Dependencies | Lockfile hash, OS, package manager version | | Build output | Source hash, config, toolchain version | | Test cache | Fixture hash, shard id |

					- uses: actions/cache@v4
  with:
    path: ~/.npm
    key: npm-${{ runner.os }}-${{ hashFiles('package-lock.json') }}
				

Restore keys allow partial hits when the exact key misses. Monorepo tools (Turborepo, Nx) add remote caching with declared task inputs and outputs.

On interviews: dependency versus build caches, restore keys, cache poisoning, when to invalidate aggressively.

Common pitfalls: a fast wrong cache is worse than no cache; caching secrets or mutable generated outputs leaks data or hides failures.

The trade-off is feedback speed versus correctness when inputs are incomplete in the key.

Checklist:

  • Hash all correctness inputs.
  • Avoid caching secrets or unbounded mutable dirs.
  • Measure hit rate and false-green patterns.
  • Invalidate when toolchain or lockfile changes.