Build Cache Mechanics & Layer Ordering Strategies
Order instructions so dependency layers cache while code layers rebuild quickly.
In plain words
Cache order converts rebuilds from a full toolchain install into a few source and compile layers.
Think of it like…
Pack the pantry before chopping today’s vegetables: stable ingredients stay on the shelf while the changing ingredients arrive last.
Where you’ve seen it
In practice, COPY package.json package-lock.json ./; RUN npm ci; COPY src ./src; RUN npm run build is the concrete move for this job: order instructions so dependency layers cache while code layers rebuild quickly. The release is not accepted until build twice, change only src/index.ts, and compare BuildKit CACHED versus new step markers in --progress=plain confirms the result.
Words you’ll hear
- Cache Hits
- The implementation control in
COPY package.json package-lock.json ./; RUN npm ci; COPY src ./src; RUN npm run buildfor order instructions so dependency layers cache while code layers rebuild quickly. - Layer Order
- The result to observe with
build twice, change only src/index.ts, and compare BuildKit CACHED versus new step markers in --progress=plain, rather than infer from a successful command alone. - Invalidation
- The production boundary exposed when copying the entire repository before npm ci, or retaining stale dependency layers because the package lock was copied after the changing source.
Visual diagram
Theory & deep dive
Cache Hits control path
The concrete mechanism is COPY package.json package-lock.json ./; RUN npm ci; COPY src ./src; RUN npm run build. The observable result is build twice, change only src/index.ts, and compare BuildKit CACHED versus new step markers in --progress=plain; if those disagree, inspect the runtime evidence before changing unrelated settings.
Order instructions so dependency layers cache while code layers rebuild quickly. Keep Cache Hits, Layer Order and Invalidation in the same deployment record so the intent and the observed state cannot drift apart.
COPY package.json package-lock.json ./; RUN npm ci; COPY src ./src; RUN npm run build
build twice, change only src/index.ts, and compare BuildKit CACHED versus new step markers in --progress=plain, not with command success alone.Invalidation failure boundary
A source copy before package installation invalidates expensive work on every commit. Cache must not be used as a correctness argument, though: mutable package repositories, ARG values and network responses still require pinned manifests and explicit invalidation. The narrow failure to design for is copying the entire repository before npm ci, or retaining stale dependency layers because the package lock was copied after the changing source.
Accept this compromise explicitly: Good layer order speeds clean builds but can delay useful consolidation; BuildKit cache mounts can be better than baking package-manager state into each layer. The controlled check-build twice, change only src/index.ts, and compare BuildKit CACHED versus new step markers in --progress=plain-also identifies the point where a different design is safer.
Exercise this failure safely: copying the entire repository before npm ci, or retaining stale dependency layers because the package lock was copied after the changing source.
How it works, step by step
Step 1 of 41. State the boundary
Write down why the workload needs this: A source copy before package installation invalidates expensive work on every commit. Cache must not be used as a correctness argument, though: mutable package repositories, ARG values and network responses still require pinned manifests and explicit invalidation.
- Intent
- Cache Hits
When to use it & trade-offs
What it is
BuildKit keys an instruction by its command, inputs and relevant parent outputs. Place stable dependency manifests and installation steps before frequently changing source COPY instructions, so a code edit does not re-download dependencies.
Why it matters: A source copy before package installation invalidates expensive work on every commit. Cache must not be used as a correctness argument, though: mutable package repositories, ARG values and network responses still require pinned manifests and explicit invalidation.
When to reach for it
- The fit is strongest for this job: order instructions so dependency layers cache while code layers rebuild quickly. Pair it with
build twice, change only src/index.ts, and compare BuildKit CACHED versus new step markers in --progress=plainin the runbook. - Production Cache Hits changes should make Layer Order observable through
COPY package.json package-lock.json ./; RUN npm ci; COPY src ./src; RUN npm run build, rather than trusting the runtime default. - Keep a rollback example for the case where copying the entire repository before npm ci, or retaining stale dependency layers because the package lock was copied after the changing source.
When to avoid or be careful
- Do not accept command success while the system can still fail by copying the entire repository before npm ci, or retaining stale dependency layers because the package lock was copied after the changing source.
- Avoid erasing Invalidation protection merely to dodge this cost: “Good layer order speeds clean builds but can delay useful consolidation; BuildKit cache mounts can be better than baking package-manager state into each layer”.
- Do not replace the evidence path-
build twice, change only src/index.ts, and compare BuildKit CACHED versus new step markers in --progress=plain-with a screenshot or a mutable tag.
COPY package.json package-lock.json ./; RUN npm ci; COPY src ./src; RUN npm run build: practical choices
Strengths: BuildKit keys an instruction by its command, inputs and relevant parent outputs. Place stable dependency manifests and installation steps before frequently changing source COPY instructions, so a code edit does not re-download dependencies.
Watch out: Good layer order speeds clean builds but can delay useful consolidation; BuildKit cache mounts can be better than baking package-manager state into each layer.
Strengths: It can begin with COPY package.json package-lock.json ./; RUN npm ci; COPY src ./src; RUN npm run build and fewer surrounding controls.
Watch out: It silently accepts copying the entire repository before npm ci, or retaining stale dependency layers because the package lock was copied after the changing source, which makes the result hard to reproduce after promotion.
Strengths: A source copy before package installation invalidates expensive work on every commit. Cache must not be used as a correctness argument, though: mutable package repositories, ARG values and network responses still require pinned manifests and explicit invalidation.
Watch out: The platform adds an ownership and debugging layer; retain build twice, change only src/index.ts, and compare BuildKit CACHED versus new step markers in --progress=plain as the direct check.
Interview signals & numbers to know
When this topic comes up
- COPY package.json package-lock.json ./; RUN npm ci; COPY src ./src; RUN npm run build
- build twice, change only src/index.ts, and compare BuildKit CACHED versus new step markers in --progress=plain
- Failure signature: copying the entire repository before npm ci, or retaining stale dependency layers because the package lock was copied after the changing source
- Trade-off: Good layer order speeds clean builds but can delay useful consolidation; BuildKit cache mounts can be better than baking package-manager state into each layer.
Benchmarks & rules of thumb
- 1 changed input
- invalidates the layer and normally all later filesystem layers
- 2 COPY stages
- manifests first, source second
- 2 builds
- are enough to demonstrate a focused cache hit
Common mistakes & follow-ups
Common interview mistakes
- Treating a successful
COPY package.json package-lock.json ./; RUN npm ci; COPY src ./src; RUN npm run buildas proof and skippingbuild twice, change only src/index.ts, and compare BuildKit CACHED versus new step markers in --progress=plain. - Copying the entire repository before npm ci, or retaining stale dependency layers because the package lock was copied after the changing source
- Optimizing Cache Hits without accepting that good layer order speeds clean builds but can delay useful consolidation; BuildKit cache mounts can be better than baking package-manager state into each layer.
Likely interviewer follow-ups
- Why does
COPY package.json package-lock.json ./; RUN npm ci; COPY src ./src; RUN npm run buildaddress Cache Hits here rather than through application code? - How would you distinguish healthy Layer Order from the failure “copying the entire repository before npm ci, or retaining stale dependency layers because the package lock was copied after the changing source” using
build twice, change only src/index.ts, and compare BuildKit CACHED versus new step markers in --progress=plain? - When would this trade-off-“Good layer order speeds clean builds but can delay useful consolidation; BuildKit cache mounts can be better than baking package-manager state into each layer”-justify a different Invalidation design?
COPY package.json package-lock.json ./; RUN npm ci; COPY src ./src; RUN npm run build-and one proof-build twice, change only src/index.ts, and compare BuildKit CACHED versus new step markers in --progress=plain. Then name the boundary: copying the entire repository before npm ci, or retaining stale dependency layers because the package lock was copied after the changing source.Knowledge check
Put it into practice
Practice related interview Q&A
Apply Build Cache Mechanics & Layer Ordering Strategies with generative AI interview Q&A and hands-on practice.