#1643

[Bug]: managed build cache is evicted after every build when a project's cache exceeds 5 GiB

Openrudik99opened 27d ago

Existing issue

  • I have searched the existing issues and found no duplicate.

Problem description

On a self-hosted SSH server, every deploy of a project whose BuildKit cache is larger than 5 GiB builds completely cold. The project's persistent cache is deleted right after each build, so the next deploy never reuses it. Every build of our two-image compose project since 1 Nov (12 consecutive builds across two environments) shows this pattern.

I expected the cache to survive between deploys while the disk has room: the server's Docker filesystem is 98 GB with 42 GB free.

Why it happens. After each build, cleanupProtectedBuildWorker → pruneManagedBuildState → pruneManagedBuildCaches trims idle project caches to

// packages/adapters/src/runtime/docker-build-cache.ts
export const DEFAULT_BUILD_CACHE_KEEP_BYTES = 5 * 1024 ** 3;
export function buildCacheKeepBytes(storage: BuildStorage): number {
  return Math.min(DEFAULT_BUILD_CACHE_KEEP_BYTES, Math.floor(storage.totalBytes / 10));
}

The same ceiling bounds BuildKit's maxUsedSpace in the project worker, and the daily build-cache:gc sweep passes the fixed AUTOMATIC_BUILD_CACHE_KEEP_BYTES (5 GiB) as its budget, which the managed collector then also clamps to buildCacheKeepBytes.

A full build of this project leaves a ~13 GiB cache (two pnpm stores, two dependency layers and a Next.js build cache). Because it exceeds the 5 GiB ceiling, the cache is removed as soon as the build finishes. There is no setting, environment variable or CLI option to change the ceiling; the only control is the per-project buildCache on/off switch.

#1209 introduced the 5 GiB retention target and #1351 made it scale down with small disks. This report is the other direction: on a large disk the fixed ceiling is too small to keep one project's cache.

Measured impact. Two staging deploys of the same commit on the 98 GB server, with the ceiling raised locally to 24 GiB (a bind-mounted copy of docker-build-cache.ts):

Staging deployPersistent cacheLayer cacheBuildTotal deploy
1st (cold, as every deploy was before)started new, then kept (13 GiB)0 hits / 12 misses12.8 min18.6 min
2nd (warm)restored12 hits / 0 misses2.7 min8.3 min

Before the local change, the staging builds in the same period took 13.2–18.1 min. The proposed default below (20% of the filesystem) is about 18.3 GiB on this disk, which still holds one project's cache. Our two environments are separate projects on the same server, though, so at 18.3 GiB only the most recently built one is kept, and a production deploy straight after a staging deploy still starts cold. On this disk a larger budget would not change that, because keeping both (~26 GiB) would take free space below the 20% target; the per-server setting in item 4 helps where a server has that headroom.

Steps to reproduce

  1. Add a self-hosted SSH server with the protected BuildKit worker (the default), on a disk of 50 GiB or more.
  2. Deploy a project whose build cache grows beyond 5 GiB, for example a monorepo with two images that each install a large dependency tree, with the project's build cache enabled.
  3. Deploy the same commit again.
  4. The second build log shows Starting a new persistent build cache. and Docker layer cache: 0 hit(s), N miss(es). instead of Persistent build cache restored..

Environment

  • OpenShip version/commit: observed on 0.9.10; the same code is in v0.9.11 (d50978cc8)
  • Deployment method: self-hosted control plane, SSH target server
  • Operating system: Ubuntu (ARM64), Docker 29 with the containerd image store
  • Browser (if applicable): n/a

Logs and screenshots

Starting a new persistent build cache.
...
Docker layer cache: 0 hit(s), 12 miss(es).

Additional context

Proposed fix

  1. Derive the default budget from the Docker filesystem instead of a fixed cap: 20% of storage.totalBytes, following the disk in both directions (disks under 50 GiB also move from 10% to 20%).
  2. Keep the disk safe: when free space falls below the existing 20% free-space GC target, shrink the idle budget to what still fits above that target, never below today's min(5 GiB, 10%). Idle caches then give space back before rollback artifacts and running apps do, so the unbounded growth in #815 stays fixed. BuildKit's own 20% minFreeSpace keeps bounding a running worker.
  3. Use that one budget for all three consumers: the post-build prune, BuildKit's maxUsedSpace, and the daily sweep (which today passes the fixed 5 GiB explicitly, so changing only buildCacheKeepBytes would still trim to 5 GiB once a day). Native Docker Engine retention keeps its 5 GiB target.
  4. Optionally let server admins override the budget per remote deployment server, since the right value depends on that server's disk.

Managed Cloud workers share the same collector, so (1) also raises their budget from 5 GiB to 20% of their filesystem; I can keep the old cap for Cloud if you prefer.

I've prepared this as two PRs so it can be reviewed in pieces:

  • PR A (fix): items 1–3. No setting, no migration; it fixes the cold builds on its own.
  • PR B (setting): item 4, an optional servers.build_cache_budget_bytes, exposed through the existing server update API (so also the CLI and MCP tool) and a Build cache card next to the GPU setting, shown to server admins. It builds on PR A.

Happy to change the default share, the Cloud behaviour or the split if you'd prefer something else.

Activity

0

No activity yet.