Skip to content

Lesson 4 of 9 · 25m

Caching That Actually Speeds Up Pipelines

Design cache keys, separate artifacts from cache, and avoid the classic stale-dependency failures that make caches dangerous.

A bad cache is worse than no cache. It saves two minutes on dependency installation, then costs an afternoon because the test suite ran against packages from the wrong branch. A good cache is a performance optimization with explicit invalidation rules. Treat it as infrastructure, not magic.

Know what is cacheable

Cache dependencies, not build outputs. Good candidates: language package directories, downloaded compiler toolchains, dependency indexes, precomputed analysis data. Bad candidates: deployment artifacts, generated secrets, database snapshots, anything whose correctness depends on the current commit.

test:
  image: node:22
  cache:
    key:
      files:
        - package-lock.json
    paths:
      - node_modules/
  script:
    - npm ci
    - npm test

The key includes package-lock.json, so dependency changes produce a different cache. That is the central idea: correctness inputs belong in the key.

Prefer lockfiles over branch names

A branch-scoped cache looks attractive because branches are isolated. It also means every branch starts cold and long-lived branches accumulate stale entries. Lockfile keys reuse cache when dependencies are unchanged, which is what matters.

cache:
  key:
    files:
      - Gemfile.lock
      - package-lock.json
    prefix: deps
  paths:
    - vendor/bundle
    - node_modules

If the tool can install into a project-local path, cache that path. If it insists on a home directory cache, map that path carefully and make sure the runner user is stable.

Cache does not guarantee presence

GitLab may fail to download cache, evict it, or run the job on a different runner or object storage backend. Your scripts must still work from a cold cache. This is why npm ci and bundle install must be correct even when the cache directory is empty.

Speed optimization is acceptable only when the slow path still works. If a cold cache breaks the job, the cache is not a cache; it is hidden state.

Fallback keys and pull-push policy

Use fallback keys cautiously. They help reuse partial matches, but they can also resurrect dependencies that no longer match the lockfile. If your language installer verifies integrity from the lockfile, the risk is lower. If it copies whatever is present, the risk is high.

cache:
  key: deps-$CI_COMMIT_REF_SLUG
  fallback_keys:
    - deps-main

For untrusted merge requests, prefer read-only cache behavior where possible or isolate runners. A poisoned shared cache is a supply-chain attack with a friendly name.

Field notes

  • Put correctness inputs such as lockfiles into cache keys.
  • Cache is a hint, not a contract; cold-cache runs must pass.
  • Do not cache build outputs that artifacts should carry.
  • Shared caches across untrusted branches can be poisoned.
  • Measure pipeline duration before and after cache changes; intuition is unreliable.

Was this lesson helpful?

Sign in to rate