Skip to content
← Trylle Journal
dockerocicloudflarer2serverlessbenchmarks

A scale-to-zero Docker registry on Cloudflare Workers and R2, with $0 egress

How docker.direct runs Harbor on Workers, Durable Objects and R2: the architecture, the bugs that shaped it, 23.8 TB of benchmarks, and what it costs.

Max16 min read

A container registry is a metadata API in front of a pile of immutable blobs. That should be cheap to run. In practice we kept paying for it in three ways:

  • Egress. Our CI runners and sandboxes live outside any single cloud. Pulling 20 TB a month out of ECR or Artifact Registry costs about $1,800 to $2,000, nearly all of it bandwidth.
  • Rate limits. Boot a hundred VMs that pull the same image and Docker Hub starts answering 429 in about a second, on a paid account.
  • Layer size. We ship VM snapshots and model weights as OCI layers. Docker Hub and GHCR stop at 10 GB per layer.

Self-hosting Harbor fixes the limits and replaces them with eleven containers that run all night whether or not anyone pushes.

So we rebuilt Harbor’s deployment around one rule: image bytes never pass through a machine we pay for by the hour. The result is docker.direct, a full registry (projects, RBAC, robot accounts, OIDC, quotas) on Cloudflare Workers, Durable Objects, Containers and R2. It scales to zero, has no egress fee, and accepts layers up to 80 GiB.

This post covers how it is put together, the five things that broke on the way with the exact fix for each, the benchmark numbers including the ones we lose, and what it costs to run.

The shape of it

Animated diagram of the Cloudflare components in docker.direct: a Docker client talks to an edge Worker, which streams layer bytes to R2, keeps upload state in a per-upload Durable Object, uses Static Assets and the Cache API, and forwards control requests to a pool of Containers. The containers reach a HarborState Durable Object and Workers KV through interceptOutboundHttp, query PlanetScale Postgres and send copy calls to R2. On pull the client downloads directly from R2.
Every Cloudflare component we use. Green is image data, blue is control traffic, amber is state. No green path touches a container.

There are two planes, and they never mix.

The control plane answers small questions: may this robot push to this repo, is the project over quota, which blobs does this manifest reference. That is Harbor Core and CNCF distribution v3, running as one Go container in a pool of eight that goes to sleep after ten idle minutes. Metadata lives in PlanetScale Postgres.

The data plane moves gigabytes, and it is only a Worker and R2:

  • Push. The Worker verifies the push token itself (RS256, under a millisecond), then streams each chunk into an R2 multipart upload while hashing it. When the layer is complete, it checks the SHA-256 against the digest the client claimed and commits.
  • Pull. The registry answers a blob request with an HTTP 307 to a short-lived presigned R2 URL. The client downloads straight from R2. Public manifests addressed by digest are served from the edge cache without waking a container at all.
Push flow: edge token check, pipelined upload to R2 with streaming SHA-256, server-side copy, manifest commit. Pull flow: manifest and auth, HTTP 307 redirect, direct download from R2
What happens on docker push and docker pull. Containers see JSON and S3 headers, never layer data.

What happened to Harbor’s eleven containers

Upstream Harbor expects a long-lived cluster. We went through it service by service and asked what each one was really for.

Mapping of upstream Harbor's eleven containers to their replacements: nginx to a Cloudflare Worker, portal to Workers Static Assets, core, registry and registryctl to one Cloudflare Container, redis to a Durable Object and Workers KV, postgres to PlanetScale, jobservice to a Cron Trigger, and trivy, notary and log removed
Three services merged into one container that talks to itself over localhost. The rest became platform features or went away.

Redis was the interesting one. Harbor uses it for three unrelated jobs: login sessions, upload coordination, and a read-heavy metadata cache. We did not want to run a Redis, and we did not want to fork Harbor’s cache layer either. Cloudflare Containers can intercept a container’s outbound HTTP, so we gave Harbor two made-up hostnames:

await container.interceptOutboundHttp("harbor.state", this.ctx.exports.HarborStateGateway);
await container.interceptOutboundHttp("harbor.cache", this.ctx.exports.HarborCacheGateway);
export _REDIS_URL_CORE="cfdo://harbor.state/session"
export _REDIS_URL_CACHE_LAYER="cfkv://harbor.cache/layer"

Anything that needs strong consistency (sessions, OIDC state, upload offsets) lands in a SQLite-backed Durable Object. The cache goes to Workers KV, with writes flushed after the response so a manifest push does not wait 400 to 600 ms for global propagation. Harbor still believes it is talking to a key-value store.

Five things that broke, and the exact fixes

The first version worked on every image we tried by hand. Then we pushed 64 GB layers and 128 × 1 GB images at it from three regions.

1. Cloudflare Containers reordered bytes in transit

Symptom. Upload chunks went Worker → container → R2. Under multi-gigabyte load, 10 to 15% of layers failed at commit with 400 DIGEST_INVALID. Every PATCH had returned 202 and the byte count matched exactly.

Cause. We pushed a deterministic 1 GiB pattern, read the staged object back from R2 and diffed it byte for byte.

Diagram of the corrupted stream: a 32 KiB block followed by an 8 KiB block was received as the 8 KiB block followed by the 32 KiB block, with no bytes lost or added
Every corrupted upload had the same signature: got_offset - want_offset = +32768 across one 40 KiB window.

Zero bytes lost, zero added. One 8 KiB block was delivered ahead of the 32 KiB block that preceded it. The SHA-256 state inside distribution and the object in R2 agreed on the swapped content, so the bytes were already out of order when they reached the registry process. The reordering happens in port.fetch(), the TCP proxy that carries a request body from a Worker into a Cloudflare Container.

Fix. Layer data no longer goes through port.fetch(). The Worker handles POST, PATCH, PUT, GET and DELETE on /v2/<repo>/blobs/uploads/<uuid> itself:

  • POST verifies the RS256 push token with node:crypto against Harbor’s token public key, mints the upload UUID and calls createMultipartUpload on _uploads/<uuid>/r2data. No container is involved, which also made session init 3.4 to 4.0× faster.
  • PATCH rejects any Content-Range whose start is not the stored offset (416), then cuts the body into 8 MiB parts with four in flight, so a request holds at most 32 MiB.
  • R2 rejects multipart uploads whose non-final parts differ in size (error 10048). Whatever is left over at the end of a PATCH is written to a side object, r2tail, and prepended to the next request.

Two request forms are still forwarded to distribution: cross-repo mounts, which carry no body, and single-request POST ?digest= uploads, which do and still cross that hop. Docker, BuildKit and containerd do not push layers that way.

2. Zero-egress storage came with a $25/TB egress bill

Symptom. After migrating about 506 GB we queried Cloudflare’s GraphQL analytics and found 341.39 GB under containersUsageAdaptiveGroups.txBytes.

Cause. R2 does not charge for egress. Containers do: $0.025 per GB in North America and Europe above the 1 TB monthly allowance. Every layer that distribution wrote to R2 from inside the container was metered as container egress.

Fix. The same move as above, plus one rule for everything that still runs in the container: it may send S3 calls about objects, never the objects. A 64 GB commit from the container is about 30 KiB of UploadPartCopy headers. On a later test day, 4.03 TB moved through R2 and containers transmitted 0.146 GB, or 0.0036% of it.

3. The fast hash function lost

Symptom. A registry must verify the SHA-256 of every layer. Docker sends a 1 GB layer as sixteen 64 MiB PATCH requests, and anycast may route each one to a different Worker isolate. The hash has to stop at the end of one request and resume in another.

Cause. Workers ship BoringSSL with hardware SHA at about 1,637 MB/s. Neither WebCrypto nor node:crypto can export the hash context mid-stream.

We ran four designs in production:

Approach Push, 128 × 1 GB What went wrong
Pure-JS resumable SHA-256 130 MB/s 591 ms of blocking CPU per 64 MiB chunk
BoringSSL kept alive in a Durable Object 132 MB/s Worker → DO body streaming: 3,545 ms median per chunk, 64.6 s worst case
No hash on PATCH, re-read from R2 on PUT 199 MB/s 5.6 to 18.3 s added to every commit
8 KiB WASM hasher in the Worker 222 MB/s —

Fix. A plain C SHA-256 compiled with Zig (-O3 --target=wasm32-freestanding) to an 8,327-byte module. Its state (eight 32-bit words, a bit counter and up to 63 buffered bytes) serializes to 180 bytes of JSON. That state lives in a Durable Object named r2upload:<uuid>, created with no location hint so it sits next to the uploader, which by itself cut median PATCH latency 24 to 44%.

At about 178 MB/s per stream the WASM hasher is nine times slower than BoringSSL. It still wins, because a single upload stream is network-bound far below that and the hash runs while parts are in flight to R2. An upload that arrives in one request never needs to pause, so that path uses BoringSSL.

Left: push throughput before and after the new upload engine in three regions, 142 to 270 MB/s in New York, 136 to 222 in Virginia, 104 to 169 in Frankfurt. Right: the four hashing designs compared
Push throughput before and after the rewrite, and the four hashing designs measured from Virginia.

4. Commits outlived the 100-second edge timeout

Symptom. 64 GB layers uploaded fine and then failed at commit with 524.

Cause. R2 has no rename. A layer is staged under its upload UUID and its final key is its digest, which is only confirmed at the end, so every commit is a server-side copy. A single CopyObject runs at about 48 MB/s: 21 seconds for 1 GB, over 20 minutes for 64 GB. Parallel UploadPartCopy from the Worker is capped by its six simultaneous outbound connections and took 110 to 130 seconds.

Fix. Blobs up to 128 MiB are copied by the Worker with one CopyObject. Anything larger goes to POST /api/internal/blobs/r2copy in the container, where 64 goroutines issue UploadPartCopy in 256 MiB ranges. A 64 GB layer now commits in 50.9 to 71.1 seconds, and a 1 GB layer in 3.85.

Commits also had to become safe to retry, because during one 128-layer run R2 returned internal error 10001 on layer 121:

  • PUT keeps the staged objects until the blob is registered in Postgres, trying up to four container slots.
  • The session records committedDigest for 15 minutes, so a repeated PUT returns 201 without redoing anything.
  • Layers over 2 GiB take a single-flight lock. A concurrent retry polls every 2 seconds for up to 75 seconds, then returns 503 with Retry-After: 3.

On the next run the same R2 error occurred and recovered on the second attempt with no failed layers.

5. A 128-layer manifest took 53 seconds

Symptom. After the last layer finished, PUT /manifests sat for 53 seconds.

Cause. The commit path resolved the manifest’s 128 blobs one at a time.

Fix. One bulk SQL statement for all blobs in the manifest, and blob descriptors pre-warmed 32 at a time. The same manifest now commits in 2.5 to 2.8 seconds, 18 to 21× faster.

One more fix is small but removes a whole class of errors. A container slot that is booting returns 503 for 2 to 4 seconds. For GET, HEAD, DELETE and body-less upload POSTs, the Worker retries on the next slot, up to three, so a deploy or a cold start does not reach the client.

Benchmarks

The harness is a Go program that generates unique, incompressible layers on the fly (so nothing dedups), pushes them over the standard OCI chunked protocol, pulls every layer back, and verifies SHA-256 on every byte. It ran from New York, Ashburn and Frankfurt on 16 to 32 vCPU machines.

In total 23.8 TB went through the registry. All 2,978 layers verified bit for bit.

Against Docker Hub

Same workload on both: 128 layers of 1 GB, 16 in parallel. Throughput in MB/s.

Region Push: docker.direct Push: Docker Hub Pull: docker.direct Pull: Docker Hub
New York 270 583 1,030 947
Ashburn 222 340 932 1,161
Frankfurt 169 148 763 422

Docker Hub pushes 2.2× faster than we do from New York and 1.5× faster from Ashburn, and pulls 25% faster from Ashburn. We win pulls from New York by 8.7%, and both directions from Frankfurt, where pulls are 1.81× faster, pushes are 14% faster, and the p95 for a 64 MiB chunk is 8.0 s against 21.6 s.

Our push speed is bounded per stream, at roughly 17 MB/s (270 MB/s over 16 streams). Throughput comes from Docker pushing layers in parallel. That is fine for a normal image and slow for one giant layer: two 64 GB layers pushed side by side take 54 minutes at best, at 39.2 MB/s combined.

Bar charts of the categories where docker.direct leads Docker Hub: New York pulls, Frankfurt pulls and pushes, Frankfurt tail latency, and manifest commit times for both
The rows where we lead, plus manifest commit times, where Hub is still slightly ahead (1.7 to 2.1 s against our 2.5 to 2.8 s).

101 VMs pulling at once

This is the test we built the thing for. 101 VMs (50 in New York, 51 in Frankfurt) pull a 256 MB, 10-layer image as fast as they can for 60 seconds.

Registry Pulls OK Failed Success HTTP 429s First 429
docker.direct 1,712 0 100% 0 never
Docker Hub, token reused 1,969 8,989 18.0% 13,607 1.25 s
Docker Hub, fresh logins 1,927 10,162 15.9% 12,934 0.74 s

Docker Hub enforces 1,500 requests per minute per account, and a fleet of this size spends that in about a second. It is a policy, not a capacity problem: Hub’s raw throughput in the first second was higher than ours.

We sustained 44.5 Gbps with no errors. Pushing concurrency to 360 raised that to 50.5 Gbps and turned up a bug of our own: AUTH_RATE_LIMIT_PER_MIN=120, keyed on client IP, which a fleet behind shared NAT addresses exhausted at once. It produced 14,904 of our own 429s in that run, with zero container or R2 errors. The fix: no limit at all on /service/token for authenticated identities, and 5,000 per minute per IP for anonymous requests.

Pull success rate across 101 VMs: docker.direct 100 percent, Docker Hub between 15.6 and 18 percent, with throughput and p95 latency for the Frankfurt half of the fleet
Pull success across the fleet, and throughput and p95 latency for the Frankfurt half.

What it costs

The bill for a team storing 1 TB of images and pulling 20 TB a month to machines outside the registry’s cloud:

Storage Egress Compute Per month
docker.direct (our infrastructure cost) $15 $0 ~$25 ~$40
AWS ECR $100 ~$1,750 $0 ~$1,850
Google Artifact Registry $100 ~$1,920 $0 ~$2,020

The first row is what Cloudflare and PlanetScale charge us, not a price list. It is why a registry built this way does not need to bill for bandwidth.

Two caveats. If everything that pulls your images lives in the same AWS or GCP region as the registry, egress is free there too and this comparison does not apply to you. And GHCR is currently free for both storage and transfer; its constraint is the 10 GB layer cap.

We checked the zero against Cloudflare’s own billing data. On one day of testing, 1.25 TB went up and 2.78 TB came down. R2 bandwidth charges: $0. Container egress: $0.0036. Worker invocations: 87,620, with no errors. Across the whole campaign, R2 operations, Durable Object requests and KV reads all stayed inside the included allowances of the $5 Workers plan.

Left: maximum single-layer size, 10 GB for Docker Hub and GHCR, 64 GB tested and 80 GiB maximum for docker.direct. Right: estimated monthly cost from 500 GB to 100 TB stored on a log scale
Layer ceilings, and the same cost model from 500 GB to 100 TB stored at a 20:1 pull ratio.

Access control

Cost was the reason to build it. These are the reasons we trust it with our own images:

  • No passwords in CI. A job trades its short-lived OIDC token for a push credential scoped to one repository. There is nothing long-lived to leak.
  • Every byte is verified. A layer is committed only if the SHA-256 computed at the edge matches the digest the client sent.
  • Pull URLs expire. The presigned R2 link in each 307 is short-lived and issued only after Harbor’s RBAC check passes.
  • Public repos inside private orgs. Harbor makes visibility all-or-nothing per project. We added a per-repository switch, so one base image can be public without exposing its neighbours.
  • A small attack surface. Internal APIs are blocked at the edge, the control plane has no public address of its own, and logins sit behind Cloudflare’s WAF.

Where it falls short

Each of these is a known gap, with the number and what closing it takes.

  • US pushes are slower than Docker Hub’s. 270 against 583 MB/s from New York, 222 against 340 from Ashburn. The cap is per stream: one PATCH is one request to one isolate with four 8 MiB parts in flight, and it tops out near 17 MB/s. Closing the gap means raising that per-request pipeline, and we have not yet found what limits it.
  • One storage region. The bucket, the container pool and the session Durable Object are all pinned to eastern North America. Frankfurt pushes are the slowest of the three regions at 169 MB/s. We tested R2 local uploads from Frankfurt: median PATCH dropped from 6,696 to 3,729 ms and immediate re-pulls rose 36.8% to 1,044 MB/s, but median commit went from 21.5 to 53.1 seconds while data replicated back for the copy. It is off until commit can run where the bytes landed.
  • 80 GiB per layer. R2 allows 10,000 parts and we use 8 MiB parts. With 64 MiB parts the ceiling is 640 GiB, but four of those in flight need 256 MiB, twice a Worker’s memory limit, and streaming one at a time is the design we replaced, at about half the push speed. We have verified 64 GB layers end to end, 15 runs out of 15.
  • Cold starts. After ten idle minutes the first control-plane request waits 2 to 4 seconds for a container. Slot failover does not cover requests with a body, so a manifest PUT that lands on a booting slot gets a 503 and relies on the client’s retry.
  • No vulnerability scanning, no replication. Trivy is gone and Harbor’s job service is disabled (HARBOR_DISABLE_JOBSERVICE=true); garbage collection runs from a cron instead. Scanning needs a job runner that fits the scale-to-zero model, and we have not built one.
  • Not OCI-certified. Pull, discovery and deletion run on upstream distribution v3. The push path is our own implementation of the chunked upload protocol, and cross-repo mounts are delegated back to distribution. Certification needs a full pass of conformance.test from opencontainers/distribution-spec with all four suites enabled (pull, push, content discovery, content management) against docker.direct, then a pull request with the report to opencontainers/oci-conformance. We have not run it yet.

Try it

docker login docker.direct
docker tag alpine:3.20 docker.direct/<project>/alpine:3.20
docker push docker.direct/<project>/alpine:3.20

docker.direct is live and is what Trylle’s own CI and sandboxes pull from. If you run the numbers for your own workload and get a different answer, we would like to hear it.