WarpBuild LogoWarpBuild

Scaling Docker Builders

By Darshan Chaudhary · Published · Updated

How we made remote Docker builders horizontally scalable

Scaling Docker Builders cover

Building Docker images in CI jobs is a pretty common use case for organizations. The naive way is to build them natively in the CI job, but there are more efficient ways to go about it; WarpBuild offers Remote Docker builders, which are much faster and cheaper than building inside the CI job. In this post, we will share how we made our Docker builders scale horizontally.

Our customers run thousands of builds a day. Each customer builds on one or more builder profiles; a profile sets the machine size and architecture, and has its own build cache, up to 2 TB. The busiest profiles do up to 900 builds a day, and in a burst, a single profile can have close to 100 concurrent builds.

Until recently, all of a profile's builds ran on one machine. This kept the cache warm, but bursty workloads made everything slow. On a 16-vCPU builder, a build which took 41 s on its own took 345 s with 25 concurrent builds, and at 50 concurrent builds, the machine ran out of memory and every build on it failed.

The one-machine model is enough for a team that runs a few builds at a time, but not for those whose CI fans out into dozens of builds. Some of our customers run close to 100 concurrent builds, so the one-machine approach needed to be changed.

After we made our Docker builders horizontally scalable, builds are not limited to a single machine and can now overflow to extra machines as required. The new design's gains are at the tail, where builds used to pile onto one machine. Our two busiest profiles got 7.5× and 1.7× faster at the median, the p90 build time fell from 441 s to 287 s, and the maximum builds on a single machine at once fell from 102 to 22.

Build times before and after: the busiest profile's median build time fell from 331 s to 44 s (7.5× faster), the second busiest profile's from 307 s to 184 s (1.7× faster), and the p90 build time across all builds from 441 s to 287 s (35% lower)
Fig 1: Build times before and after

Anatomy of a Docker build

Docker builds are driven by a Dockerfile, which is just an ordered list of steps. Each step that changes files produces a layer, which is a directory holding the changed files. There is also a lookup table which maps each step's fingerprint to its layer, so the output of unchanged steps can be reused without re-executing them. Both live in the builder's cache dir, /var/lib/buildkit, so that directory is critical for performance.

A step's fingerprint is a function of:

  • The instruction, with the surrounding context (ENV and ARG values, WORKDIR, USER etc.)
  • A checksum of the files it copies in from the build context
  • The previous step's fingerprint

Before executing the step, BuildKit, the engine behind docker build, looks up the fingerprint in the lookup table:

  • If the lookup table has it, the step's layer is reused.
  • If not, the step executes, and BuildKit writes a new layer and adds a row to the lookup table.
InteractiveOne Dockerfile, built twice
Dockerfile
  1. 1COPY busybox /bin/busybox
  2. 2COPY site/ /www/
  3. 3RUN ["/bin/busybox", "gzip", "-k", "/www/index.html"]
Fingerprinta hash of the step’s three inputs
instruction + settings—
files copied in—
previous fingerprint—
hash →fingerprint
Is it in the lookup table?
yes: hitReuse its layerskip the step; the log prints CACHED
no: missRun the stepwrite a new layer, add a row
/var/lib/buildkit
Lookup table fingerprint → layer
empty
Layers one directory each
empty

Build 1 starts with an empty cache. No rows, no layers. Press Next to send step 1 through the lookup.

0/7
Fig 2: One Dockerfile, built twice

Why a shared builder

To see why building Docker images natively in the CI job is inefficient, consider matrix jobs: one workflow fans out into dozens of jobs, and each job runs its Docker build on its own ephemeral VM. Every build starts with an empty BuildKit cache dir and redoes every step.

An alternative to this is to use WarpBuild's Remote Docker builders. They are long-lived machines running BuildKit for the whole team, with every job sending its build there. The cache stays warm between jobs, so a build only redoes the steps whose inputs changed. A CI job on GitHub Actions asks the WarpBuild backend to assign a machine to it via POST /api/v1/builders/assign, and points Buildx's remote driver at it, communicating with it over mTLS.

Matrix builds: on the left, four jobs each build in their own CI VM with an empty BuildKit cache, so every build starts cold; on the right, the same four jobs send their builds to one shared builder whose cache stays warm, so later builds reuse earlier layers
Fig 3: Matrix builds

Scaling the builders

How do we scale the builder machine to support a higher number of concurrent builds? Vertical scaling can only get you so far, and has diminishing returns. We cannot simply add more machines to operate on top of the shared cache dir (/var/lib/buildkit); that would break the single-writer assumption of BuildKit.

How about performant builder machines on local NVMe, which restore the cache dir from a storage layer at the start of each session?

This could work but needs careful thought, since restoring takes time proportional to the size of the cache dir, which can grow to TBs. Cloud disk clones are rate-limited (GCP allows one clone per disk every 30 s), and EBS snapshot restores are lazy underneath.

Other solutions we evaluated, like shared network disks and object storage as a filesystem, were either too expensive or too slow.

We wanted to find a way where we could keep what works with the current setup: a single machine that maximizes reuse of the cache dir, but which can also horizontally scale if required, while honoring BuildKit's assumption of a single writer per cache dir.

The cache dir as chunks

We decided to combine the above solutions and model the entire cache dir as a series of chunks, with a single owner having write access and helper machines having read-only access to them.

Three views of the same bytes: BuildKit sees a lookup table and layers, ext4 stores them as inodes pointing into an array of blocks, and we publish that array as 4 MiB pieces, each stored once by hash
Fig 4: Three views of the same bytes

Concretely, a builder's /var/lib/buildkit is ordinary ext4, and that volume is treated as an array of 4 MiB pieces:

  • Each changed piece is hashed (SHA-256 of the raw bytes), compressed with zstd, and stored once as chunks/<sha256>.
  • There is an index: a 64-byte header (generation, parent, device size, created-at), then one 48-byte row per 4 MiB piece, in piece order, holding its offset, SHA-256, compressed size and present/zero flags. Finding the chunk for any byte in the cache dir is a single array lookup.
  • HEAD is a small JSON object naming the current generation, the latest published version of the index.
The index is an array: a 64-byte header, then one 48-byte row per 4 MiB piece, in piece order. Piece 0 covers bytes 0 to 4 MiB, piece 1 covers 4 to 8 MiB, and so on, so a read at 9 MiB falls in piece 2 (9 ÷ 4 = 2.25, rounded down) and its entry is row 2. Row 2 holds the piece's offset (8 MiB), the SHA-256 of its bytes, its compressed size and its flags, and that SHA-256 is the key of its chunk in object storage, chunks/<sha256>, which holds piece 2's bytes, zstd-compressed
Fig 5: The index

When the builds run, new bytes will be written at arbitrary offsets, which changes some pieces. At appropriate periodic intervals, we can identify the changed pieces, save them under their new SHA, write a new index and publish it as a new consistent generation. A generation, once published, is an immutable snapshot of the cache dir: an index plus the chunks it names.

We can then store these chunks on object storage:

The object storage layout under <profile_id>/<arch>/: HEAD records the current generation, 41, and points to gen/41/index, the current index, which is written once and never changed; gen/40/index is an older generation kept for a while; chunks/ holds one object per unique 4 MiB piece, zstd-compressed and named by the SHA-256 of its bytes, shared by every generation
Fig 6: The object storage layout

Primary and helpers

Now that we have the chunked cache dir in object storage, we can add more builders. We keep the original single machine, and call it the primary; it has write access to the cache dir, publishes generations, and takes as many builds as it can. Extra machines, called helpers, take the overflow.

A helper mounts the latest published generation as a read-only base, keeps everything its builds write on its own local NVMe, and never publishes anything back. Many helpers can run, but since the cache dir has exactly one writer, the single-writer assumption still holds.

How a helper reads the cache

The helper machine has an agent that helps orchestrate its read-only access to the cache dir. This is done via NBD, the Linux network block device protocol, which lets a user-space program serve a block device to the kernel: reads and writes on it turn into requests on a socket which the agent can answer.

The agent runs the NBD server in-process, mounts ext4 and starts buildkitd. BuildKit sees an ordinary disk with the agent answering read calls by fetching the bytes from object storage.

On the helper machine, each piece is in one of three states:

  • Absent - Not on this machine yet. The first read fetches the chunk, decompresses it, checks its SHA-256, writes it to local NVMe and marks the piece present.
  • Present - Served straight from local NVMe.
  • Zero - Reads return zeros without a fetch.

Booting a helper

When booting the helper, we need it to be ready to take builds as soon as possible; a burst of matrix jobs should not have to wait for a TB-sized cache dir to be pulled down from object storage. The agent never waits for the whole cache dir before it starts serving; once the instance has booted, the agent reads HEAD, fetches the index of the latest generation, and then loads the cache dir in three stages:

  • Startup map.
    • Fetch the bare minimum chunks required to start BuildKit and report the helper as serving right away. Builds can land from here on, and anything they need that isn't local yet is fetched on first read.
  • Read history.
    • In the background, prefetch the pieces that earlier helpers' builds had to fetch on demand. The agent does bookkeeping to remember this information from each run.
  • Backfill.
    • Slowly fetch the rest of the cache dir with 2 workers, so it doesn't compete with the pieces builds need.
A helper's first minute, schematic: instance launch and boot take about 91% of the wait; the agent then fetches HEAD and the index, prefetches the startup map, mounts ext4, starts buildkitd and reports serving, while history and backfill continue in the background
Fig 7: A helper's first minute

Dropping a helper's writes is safe

Writes go to local NVMe and never leave the machine. This is safe, and only causes some efficiency loss.

  • Since the cache dir only changes when the primary publishes, and every generation is a consistent snapshot of the primary's disk, the helpers get a consistent snapshot of the cache dir and cannot corrupt it; we never merge writes from two machines.
  • If a future build needs a layer that only a helper built, that build makes the layer again, on the primary or on another helper. Once the primary has built it, the next generation carries it.
A helper's layers are thrown away: a helper builds layer X, which lives only on its local NVMe; when the helper terminates, X is deleted without ever being published; when the primary later needs X, it misses and builds X again, and its next generation includes X
Fig 8: A helper's layers are thrown away

In our testing across diverse workloads, builds on a running helper took a median 1.21× as long as builds on the primary; that includes the lazy loading as well as layers built twice.

Routing a burst

Which machine does a build go to? When the CI job's docker-configure, build-push or bake step calls POST /api/v1/builders/assign, the backend picks the machine which will serve that request based on the following logic:

  • The primary, while its running builds are below the profile's limit of max(1, vCPU ÷ 4).
    • If the primary isn't healthy (its agent has been silent for 2 minutes, it has been sitting on unpublished data for 15 minutes etc.), it is skipped.
  • Else, the least-loaded helper with room.
  • Else, a new helper.

Over two weeks post rollout, 88.8% of builds ran on the primary, 7.8% on a running helper and 3.4% on a newly launched one. Most builds still run on the primary with its warm cache, and helpers absorb the bursts that used to pile onto it.

On this page