---
url: /en/build/pipeline-cache.md
description: >-
  Explains how Cloud Native Build caches dependencies and build artifacts on
  runner nodes to reduce build time, covering runner node caching, file caching,
  and cross-node caching strategies.
---
During pipeline execution, many dependencies and intermediate artifacts are generated.
Caching these for reuse in subsequent pipelines can effectively reduce build time.

## Runner Node Caching

To balance single-point-of-failure risks with cache hit rates,
pipelines are assigned to 3 runner nodes by default (the number adjusts dynamically with resource usage).

:::tip
Node assignments are not permanent — they may change as the platform scales.
:::

### Code Caching

When a pipeline executes,
it automatically pulls code to the runner node and caches it for reuse by subsequent pipelines.

### File Caching

During builds,
files like project dependencies and build caches can be cached on the runner node for reuse by subsequent pipelines.

Declare the cache directories through
[`docker.volumes`](./grammar.md#pipeline-volumes).
The following example caches the npm dependency directory on the runner node,
so subsequent builds can reuse it without downloading dependencies again:

```yaml title=".cnb.yml"
main:
  push:
    - docker:
        image: node:20
        volumes:
          - /root/.npm:copy-on-write # Cache npm dependencies for reuse
      stages:
        - name: install
          script: npm install
        - name: test
          script: npm test
```

* `copy-on-write` leverages `OverlayFS` instant-copy technology,
  allowing caches to be efficiently reused under high concurrency.
* See [grammar](./grammar.md#pipeline-volumes) for volume types and formats.

## Cross-Node Caching

Node caching takes effect on the current runner node only. To share the same cache
across all nodes, you can build the content to be cached into a Docker image for cross-node sharing.

For specific methods, reference the built-in task: [docker:cache](./internal-steps/docker/cache.md)
