Deploying fast, the hard way.
Our learnings from 3x-ing deploy speed.
3B is a sophisticated application. It contains a lot of systems code, primarily in Rust, to power our custom compute, storage, and networking runtime that millions of executions every day rely upon. It has an ambitious frontend, powered by Zero and dedicated backend services. It deploys to many global regions, from the US to Europe to Australasia, and a variety of multi-tenant and dedicated stacks in each of those regions.
Our target for a median deploy is 5 minutes. The clock starts with a workflow triggered by pressing merge, running right through to the last service being healthy on the last host in the last region. During that time we need to compile binaries, build Docker images, run many tests, migrate databases, and roll services. It is a challenge!
Work is ongoing – we haven’t quite achieved our target, yet. But here are a few things we’ve learned and discovered so far.
Avoid the easy way
Skipping unnecessary work is the key to deploying quickly. A tempting tactic, the easy way, is to introduce logic like “don’t run this Docker build unless the PR touches the following directories”. But this is input caching: guessing what production needs from a diff.
It is brittle, and dangerous, and must be avoided:
- what if the last deploy failed, so we’re actually deploying multiple PRs?
- what about something shared or imported from beyond the directory?
- what if the last rollout was interrupted halfway, and one region is behind others?
Instead, use output caching. Build an output (say an image or a frontend bundle) then compare that with the version running in production, and no-op if they match. You prove that there is nothing to deploy. We do this everywhere:
- we synthesize CDK and compare with the previous synth
- we render the Helm chart and compare with what’s deployed
- we build all Docker images and roll services if changed
Optimize for the best case
A substantial fraction of our PRs don’t affect production services. These include deleting unused code, improving tests, tweaking skills or documentation, adding development tooling, etc.
But, as explained previously, we can’t just skip these. We still need to build and compare, just in case.
At the start of our journey, our best case deployments took 15 minutes, including a change to a totally inert file like a README. We found that iteratively focusing on this metric, which contains the lowest hanging fruit, led to rapid and surprising improvements to overall latency. This is because
- it’s a very easy signal to measure. you look at your fastest deploys, and you ask, how could this be faster?
- it directs your attention to the universal stages of the process (building and testing). These are always on the hot path, so wins accumulate
- when the low-hanging fruit is eliminated, more magically appears
Now, such best-case deployments are 2.5 minutes in total: around a minute of building images and running many unit tests with high parallelism, then another minute of running full stack integration test scenarios, and finally 30 seconds to render and compare Helm charts to be sure nothing needs to roll, and quickly activating UI.
As your best case numbers start to get good, more signal in other hard/slow deployments will emerge in the noise. You’ll spot new low-hanging fruit there, too.
Inhabit your data
The process, as before, is to pay lots of attention, find the low-hanging fruit, solve it, repeat.
To sustain this focus, we constructed a robust definition and dashboard of deployment latency (in our case this involved controlling for deploy queue depth and defining the true moment of actual completion), and then posted it constantly in a place the team spends time in. For us this is Slack: we added a #deployments channel, logging each deployment, its commits, which services rolled, and how many so far that day:

What deployed? How long did it take? Did anything fail? Should those services have rolled?
For some reason, this stream of deployment notifications doesn’t feel like other Slack noise. Each post is in fact joyful – another delivered improvement that benefits our customers. But now and then, something catches our eye that looks off, and we investigate it.
During our move to Kubernetes, deploy performance expectedly regressed owing to the hugeness of the change. Yet without any official planned project work on making deploys fast again, our exposure to the regression nerd-sniped us, and after a few weeks, we had restored the levels previously reached.
Some things that worked for us
Almost every improvement we made was, in retrospect, obvious. What led to them was caring. Letting the data reach us, paying attention to it, allowing ourselves to get distracted from other things to investigate a piece of slowness here and there. Once you isolate a problem, a tactical solution usually reveals itself.
Nonetheless, here are some specific tactics that made a difference for us.
Avoid metadata within the build
Problem: Output caching needs an unchanged system to produce identical output. A detail like a timestamp or embedded commit SHA can compromise this. We wrote a git SHA into every stack’s CloudFormation template for a telemetry purpose, but this made every deploy an update to every stack, whatever the commit contained.
Solution: Publish the SHA to an SSM parameter before deploying, and read it in tasks on startup. On Kubernetes, the Jobs that need it read it from a ConfigMap.
After that the comparison is small. We hash every file CDK synthesizes for a stack, and compare with the hash stored after that stack’s last successful deploy.
find . -type f ! -name tree.json -print0 |
LC_ALL=C sort -z |
xargs -0 -r sha256sum --zero |
sha256sumIf the hashes differ, we overwrite the stored one with unavailable before changing anything, and only write a real value back after a clean deploy. An interrupted rollout can’t make the next deploy skip. The rendered Kubernetes manifests get the same treatment.
Make image builds reproducible
Problem: We roll a service when its image digest changes, so an unchanged service has to build to the same digest every time, including from a cold cache. Ours didn’t. One day when clearing our build cache, we noticed a test-only change rolled many production services whose code hadn’t changed.
Solution: Build example commits warm and cold, and diff the images until every digest matches. What we had to fix:
- layer timestamps, which we now pin and rewrite on export
- base images and Debian package snapshots, some of which hadn’t been pinned
/etc/shadow, which stores the day each system account was created, so a build after midnight UTC produced a different image/var/cache/ldconfig/aux-cache, whichapkleaves behind with different bytes on every run
Give each image only the inputs it needs
Problem: A service rolls whenever its image changes, and its image changes whenever anything copied into it changes, whether or not the service uses it. One heavier service referred to some shared cross-service definition files, and edits to those files (which often only affected UI) could cause it to roll.
Solution: Narrow each image’s inputs to what it reads at runtime. That service’s build now gets a projection of the definition files with just the fields it uses, so its image changes only when one of them does. The frontend used to be part of a backend service’s task definition, so UI changes rolled that service. It’s now static files behind one index document, which we publish as an immutable candidate and promote once, after every stack has finished.
The same applies to layers within an image. Changing any file in a layer replaces the whole layer. One image had a 0.9 MB binary in the same layer as a 778 MB filesystem, so each change to the binary pushed 778 MB to every regional registry and every host. In two weeks that layer was replaced 51 times. We moved the binary into its own layer with COPY --link, and the same change now ships 0.45 MB.
Run test databases without durability
Problem: For isolation and parallelism, we give individual test files that need a database their own Postgres database. By default Postgres makes each write crash-safe, which is slow, and wasted on a throwaway db.
Solution: Turn durability off for test databases. Disabling fsync and full-page writes took one database test job from 25 seconds to 16. Moving the data directory to tmpfs took another suite from 31 seconds to 25, and removed flakiness. Clone each database from a migrated template, which is 17 MB and takes 26 ms to copy.
Define hard time limits for tests and checks
Problem: Test suites degrade and get slower over time. Stalls can block the deploy pipeline.
Solution: Cap every check. Once we made ours fast and parallel, anything over 3 minutes failed, with many jobs capped at 1 or 2. If the cap is exceeded, the run fails, and we need to fix it like any other bug.
This has frequently revealed waits or excessive work hiding inside tests. One suite sat through real timers; switching to fake ones, and running its files in 4 isolated processes, took it from 1 minute to 10 seconds. Another was compiling Rust inside every test case.
Pre-warm image registries in regions
Problem: Our builds push to a centralized image registry, but we deploy to a variety of regions. Our deployment metric measures the slowest rollout, which is sensitive to distance.
Solution: Give each region its own ECR registry, with a pull-through cache pointing at the centralized build registry. Once images are built, one job per region brings in the new image. This work can happen while tests are still completing. It works the same on ECS and EKS: tasks and pods both pull from the registry in their own region.
Pre-warm capacity for rollouts
Problem: A rolling deploy keeps every old instance of a service healthy while it starts the replacements, so it needs spare hosts. Without them, the rollout waits for new hosts to boot, pull images, and become healthy.
Solution: Have the hosts ready before the rollout starts.
On ECS: Without spare hosts, ECS would wait until CloudFormation updated the service before requesting new ones. So we request them ahead of time. Once images are built and we know which services need to roll, we raise each one’s auto scaling group minimum by one host per task the rollout is allowed to start, capped at the group’s maximum. The hosts join the cluster while the tests are still running, and we pull the new images onto them over SSM, so the first tasks don’t wait on a pull either. After the deploy we put the minimum back and let autoscaling remove the spares. If a run dies before cleanup, the minimum stays raised until the next successful deploy, which costs a few idle hosts. We proved this out on one service initially, where its rollout median decreased from 296 seconds to 144, and then applied everywhere.
On EKS: Each rollout surges a full set of replacement pods, and Karpenter adds nodes when they don’t fit. We don’t request those ahead of time. Instead, nodes emptied by one rollout stay for 10 minutes before Karpenter removes them, so a deploy that follows soon after reuses them. A cold rollout still waits for nodes to boot.
Keep code rollouts out of CloudFormation
Problem: By default, a rollout isn’t finished until the old instances have drained. Ours drain gracefully, so that tail was long. And when CloudFormation drives the rollout, it adds its own wait for each service to stabilize.
Solution: Finish the deploy once the new instances are healthy, let the old ones drain in the background, and have the workflow roll services itself. CloudFormation only manages infrastructure.
On ECS: With early success criteria, HealthyPercent at 100, and SourceServiceRevisionCleanup set to DEFERRED, a deployment succeeds once every replacement task is healthy, and the old revision drains in the background. That wasn’t quite enough on its own, because CloudFormation still waited for each service to stabilize, so we stopped using it for code rollouts. During a stack update, each service would stay on the task definition from its last successful deploy. Once CloudFormation finished, the workflow imperatively rolled services forward itself, in parallel, waited on ECS directly, and recorded the revisions that succeeded in SSM. A failed rollout leaves that record alone, and a retry carries on toward the same revision. For one service, the median rollout went from 4m 24s to 1m 41s thanks to this approach.
On EKS: The split is built in. CloudFormation owns the cluster and never sees application code. The workflow applies the rendered manifests and runs kubectl rollout status for every service in parallel, which returns once the new pods are ready, while the old ones are still terminating. When the infrastructure hasn’t changed, CloudFormation doesn’t run at all.
Pin images by digest
Problem: An image tag has to be resolved to a digest before anything can pull it, and a tag can be moved to a different image.
Solution: Reference every image by digest. The build already knows it.
On ECS: Given a tag, ECS resolves it to a digest for the first task of a deployment before it schedules the rest. For one service, that serialized step took between 10 and 32 seconds of every rollout. With the digest in the task definition, ECS scheduled all 42 of that service’s replacement tasks in under a second. AWS recommends this in a note on its rolling update page.
On EKS: A Deployment rolls only when its pod template changes. With images named by digest, an unchanged image leaves the template unchanged, so applying the manifests rolls only the services whose images changed. Our renderer rejects any container that isn’t pinned.
Recommended: Depot
Problem: GitHub Actions has suffered from reliability issues, and its runners have disappointing resource limitations, e.g. disk IO and CPU.
Solution: We recommend Depot for builds and CI, and specifically Depot CI, which consistently gives you bare-metal runners with low startup latency. Additionally, they have repeatedly responded with advice and determination to solve some BuildKit issues we detected, like slow builder starts under certain conditions, fixing them in their fork.
The hard way gets easier
The rigor of rebuilding every deploy does cost time. Will we ever make it to 5 minutes?
Our hunch is yes. By deploying more quickly, we also deploy much more frequently. This yields more data, more opportunities to spot the low-hanging fruit, more moments for problems like flaky tests or unreliable services to reveal themselves.
We’ll see what we can do. The next data point should be along in just a few minutes…