Skip to content
Ask for a Budget
TechnologyFebruary 23, 20264 min readUpdated on November 11, 2026

CI/CD caching: how to design keys and avoid stale caches

What goes in the key (lockfile, OS, architecture), what stays out (SHA, timestamp), restore and fallback keys, and what to cache in Node, Python, Java, Ruby.

CI/CD caching: how to design keys and avoid stale caches
Índice do artigo
faltam 4 min de leitura

Caching is one of the biggest performance levers in CI/CD. It is also one of the biggest sources of intermittent bugs.

The goal of this post is practical: to teach you how to design cache keys that speed up the pipeline without leaving you with "haunted" builds.

This post is part of the series:

CI/CD series map

Cache vs artifacts: start by separating what is what

Rule of thumb:

  • Cache = reuse rebuildable directories (dependencies, downloads).
  • Artifacts = carry build results between jobs (dist, binary, report).

When you use the cache to carry build output, you increase the risk of:

  • a job consuming an "old" output
  • inconsistent results per branch/architecture

1) How to design cache keys (what goes in and what stays out)

A good cache key usually has:

  • Operating system
  • Architecture (ARM64/x64)
  • Lockfile hash
  • Runtime version (when needed)

A bad cache key usually has:

  • Commit SHA (invalidates on every commit)
  • timestamp
  • branch name without normalization (explodes the number of caches)

1.1 Example (GitHub Actions)

- name: Cache npm
  uses: actions/cache@v6
  with:
    path: ~/.npm
    key: ${{ runner.os }}-${{ runner.arch }}-npm-${{ hashFiles('**/package-lock.json') }}
    restore-keys: |
      ${{ runner.os }}-${{ runner.arch }}-npm-

Why this works:

  • The lockfile hash only changes when a dependency changes.
  • The prefix in restore-keys lets you reuse a "close" cache when the exact one does not exist.
  • runner.arch prevents mixing ARM64 and x64.

About the action version: in November 2026, the actions/cache README uses @v6 in its examples (GitHub's documentation still shows @v4). Since v5, the action runs on Node.js 24 and requires runner version 2.327.1 or later. If you use a self-hosted runner, update the runner before bumping the version.

1.2 Example (GitLab CI/CD)

In GitLab, the equivalent of hashFiles is cache:key:files: the key becomes the SHA of the content of the listed files. With cache:key:prefix you combine that SHA with a string that keeps the caches apart. GitLab's documentation uses $CI_JOB_NAME as the prefix, and that is what the example below takes advantage of: the job name carries the architecture.

.node-cache:
  cache:
    key:
      files:
        - package-lock.json
      prefix: $CI_JOB_NAME
    paths:
      - .npm/
  before_script:
    - npm ci --cache .npm --prefer-offline

test-x64:
  extends: .node-cache
  tags: [linux-x64]
  script:
    - npm test

test-arm64:
  extends: .node-cache
  tags: [linux-arm64]
  script:
    - npm test

The tag names (linux-x64, linux-arm64) are examples: use the tags that route to your runners.

Why this works:

  • The key only changes when package-lock.json changes, as in the GitHub example. It ends up as something like test-arm64-<lockfile sha>.
  • Each job has its own cache, so ARM64 and x64 never mix. The cost is that two jobs on the same architecture do not share a cache; if that matters, replace the prefix with a fixed string per architecture (for example node-arm64) defined in each job.
  • The cache lives in .npm/ inside the project. GitLab only caches paths inside the project directory, which is why we use npm ci --cache .npm instead of the default ~/.npm.

Two caveats that apply to GitLab:

  • CI_RUNNER_EXECUTABLE_ARCH looks like the natural place for the architecture, but the documentation itself warns that it describes the GitLab Runner executable and may not match the executor environment (a container for another platform, for example). Also, cache:key does not accept the / character. Check the value in your runner's log before using it in the key.
  • fallback_keys does not work like GitHub's restore-keys. GitHub looks for caches whose key starts with the given prefix; GitLab tries the keys in the list by exact name, in order (up to five per cache). With a lockfile-based key there is no "similar" key to look for, so listing fallbacks only makes sense if some job saves a cache with a fixed name.

2) What to cache (Node, Python, Java, Ruby)

General rule: cache the "package manager cache", not necessarily the final folder.

  • Node (npm): ~/.npm on GitHub; on GitLab, .npm/ inside the project with npm ci --cache .npm
  • Node (pnpm): the store (on Linux the current default is ~/.local/share/pnpm/store; on GitLab, point store-dir to a folder inside the project)
  • Python (pip): ~/.cache/pip
  • Java/Gradle: ~/.gradle/caches
  • Ruby/bundler: vendor/bundle (with care)

What to avoid as a first step:

  • caching node_modules indiscriminately (it can be huge and unstable)
  • caching build outputs (that is an artifact)
  • letting a job from an untrusted source (a fork pipeline, for example) write to a cache that trusted jobs restore later. The pnpm documentation treats the store as a trusted cache, and GitLab, by default, already separates caches for protected and unprotected branches.

3) Invalidation: what changes the cache (and how to avoid a "useless cache")

Three classic causes of an "always miss" cache:

  1. The key changes on every commit (e.g. it includes the SHA).
  2. The lockfile is not included in the hash (the cache goes stale and can break things).
  3. You are caching a directory that changes on every run.

Checklist to fix it:

  • the lockfile is part of the key (hashFiles(...) or equivalent).
  • the architecture is part of the key.
  • the cache paths actually exist (do not invent directories).
  • the cache is restored before installing dependencies.

4) Caching in pipelines with ARM64 and x64

When you have ARM64 and x64:

  • caches of native dependencies cannot be shared
  • caches need to be duplicated (one per architecture)

Practical rule:

  • never share a cache across architectures without putting arch in the key

Final checklist

  • The cache key includes the lockfile hash.
  • The cache key includes OS + architecture.
  • Restore keys exist on GitHub; on GitLab, fallbacks only with a fixed-name key (and never for build output).
  • The cache is for dependencies (artifacts are for outputs).
  • Observable metric: time saved per job (before/after).

References

Also read

GitLab CI: runner tags and job routing (without the pain)Git CI/CD guide for small teams: performance, cache and runnersWhat is a web app and what are its benefits?
Previous post
GitLab CI: runner tags and job routing (without the pain)
Next post
How to speed up CI on GitLab Runner: cache, concurrency and tags
Newsletter

One email a month, no noise

What we learn shipping custom software and applied AI.

Related articles

GitLab CI: runner tags and job routing (without the pain)3 min · Technology
Git CI/CD guide for small teams: performance, cache and runners8 min · Technology
What is a web app and what are its benefits?9 min · Technology
Definitive Low-code Guide: What It Is and When to Use It9 min · Technology
How does a software house work?8 min · Technology

Accelerate your business with X-Apps

Ask for a Budget
X-Apps is an IT provider partner and advised by
Gartner
Receive our e-mails
News on software and applied AI.
Follow X-Apps