Deployment Strategies Explained: Blue-Green, Canary, and Rolling
From Mid-Level to Senior · Part 12 of 18
- #deployment
- #devops
- #kubernetes
- #senior-engineer
- #engineering-practices
The simplest way to deploy is to stop the old version and start the new one. For a few seconds nothing answers, and then every user is on the new code at the same moment. If the new code has a problem, every user has that problem too, and you find out from the error rate or from the support channel.
Plenty of systems run like this for years, and for an internal tool with ten users it's a reasonable choice. It stops being reasonable when the downtime is visible to customers, or when a bad release costs more than an apology.
Rolling, blue-green and canary are three ways of making that bet smaller. They sound like three unrelated techniques, but each one is an answer to the same three questions:
- How many users get the new version before you know it works?
- How do you find out that it doesn't?
- How do you go back, and how long does that take?
Part 8 ended with one artifact per commit and the promise that how traffic moves during a release would get its own post. This is that post. It also leans on Part 11, because load balancers and container platforms are the machinery that does the moving.
The example is the same order-service as last time: a Spring Boot API
with a PostgreSQL database and a queue, running as four instances behind
a load balancer. Version 1 is in production. Version 2 is the one we
want there.
Three ways to move traffic
Rolling
A rolling deployment replaces instances a few at a time. The platform starts one instance of version 2, waits until it reports ready, sends it traffic, and then stops one instance of version 1. It repeats that until no version 1 instances are left.
If you deploy to Kubernetes or ECS and never chose a strategy, this is the one you're using. On Kubernetes the pace is set by two numbers:
spec:
replicas: 4
minReadySeconds: 30
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1 # at most one extra instance during the rollout
maxUnavailable: 0 # never drop below four serving instances
What it costs. Almost nothing in infrastructure: one extra instance for a few minutes. That's the main reason it's the default everywhere.
How you go back. You run another rolling deployment, this time towards the previous version. It's safe, but it takes as long as the rollout did, and during that time some requests still land on the bad version.
What it asks from you. For the whole rollout, version 1 and version 2 serve traffic side by side. A user can get a response from version 2 and send the next request to version 1. Both versions read and write the same database and the same queue. So every release has to be compatible with the one before it, in its API, its schema and its message formats. Most of the rest of this post is about that requirement, because the other two strategies have it too, only less visibly.
Rolling also controls exposure only roughly. With four instances, the first step already puts a quarter of the traffic on the new version.
Blue-green
Blue-green keeps two complete environments. Blue is running version 1 and taking all the traffic. You bring up green with version 2 next to it, at full size, and it takes no traffic yet. You test green through a private address. When you're satisfied, the load balancer switches everything from blue to green in one step. Blue stays up for a while with nobody using it.
What it costs. Twice the capacity while both environments exist. On a container platform that's usually an hour or less of extra compute, which is cheap. With large fixed fleets it's a real line on the bill.
How you go back. You switch the load balancer back to blue. It takes seconds, and you can practise it. This is the reason to choose blue-green: the old version is still running and still warm, so rolling back is a routing change. No deployment is involved.
What it asks from you. Three things that the diagrams leave out.
There's still only one database. Blue and green share it, so the schema has to work for both versions. The instant rollback is only instant if version 1 can still run against whatever version 2 did to the data in the meantime.
Green starts cold. A JVM that just started has an empty connection pool, empty caches and code the JIT hasn't compiled. If it gets 100% of production traffic in one step, the first minute can look like an incident even when the code is fine. Send it some traffic before the switch, or move the weight over a couple of minutes.
The load balancer only controls HTTP. If order-service also consumes
from a queue or runs @Scheduled jobs, the green instances begin doing
that as soon as they start. That's well before the switch and before
anyone has looked at them. For workloads like these, start green with
consumers and schedulers disabled and turn them on as part of the
switch.
Canary
A canary release sends a small share of real traffic to version 2, say 5%, and leaves the rest on version 1. You compare the two for a while. If version 2 looks as healthy as version 1, you raise the share to 25%, then 50%, then all of it. If it looks worse at any step, the share goes back to zero.
The difference from the other two is that real users are the test, and only a few of them at first. Version 2 gets production data, production load and production clients doing things nobody thought to put in a test suite.
What it costs. Little in compute and a lot in everything else. You need a way to split traffic by weight: weighted target groups on an AWS load balancer, revisions with traffic percentages on Cloud Run, a service mesh or a controller like Argo Rollouts on Kubernetes. You need metrics that can be separated by version. And a rollout takes longer, because each step has to wait long enough to mean something.
How you go back. You set the weight to zero. It's as fast as blue-green, and fewer users were affected in the first place.
What it asks from you. Enough traffic. Suppose order-service gets
20 requests a minute. Five percent of that is one request a minute.
After ten minutes the canary has served ten requests, and a single
failure is either a 10% error rate or noise, with no way to tell which.
On low-traffic services a canary gives very little information. A
smoke test after the deploy is the more honest choice there.
It also has the same mixed-version requirement as rolling, and for longer. And you need to decide what "a share of traffic" means. Splitting by request is simple, but a user may bounce between versions from one click to the next. Splitting by user or by tenant gives a consistent experience and makes the comparison cleaner, at the cost of more setup.
Health checks, and stopping without a human
Every strategy above includes a step that says "wait until the new instance is healthy". The quality of the whole rollout depends on what that check actually verifies.
There are two different questions, and platforms ask them separately:
- Liveness: is this process broken in a way that only a restart fixes?
- Readiness: can this instance take traffic right now?
Spring Boot exposes both through Actuator. On Kubernetes they're enabled automatically, and elsewhere you turn them on:
management.endpoint.health.probes.enabled=true
server.shutdown=graceful
spring.lifecycle.timeout-per-shutdown-phase=30s
That gives you /actuator/health/liveness and
/actuator/health/readiness. A few things I'd check in any service:
- Readiness should mean "can serve a request". The application context is up, the connection pool has connections, migrations are applied. A check that returns 200 as soon as the web server is listening lets the rollout proceed over an instance that will fail its first real request.
- Keep external dependencies out of liveness. If liveness checks the database and the database has a slow minute, the platform restarts every instance at once. A database problem then becomes a full outage with a cold start on top of it.
- Shut down gracefully. When an instance is told to stop, it should finish the requests it has and refuse new ones. Recent Spring Boot versions do this by default, and older ones need the property above. Load balancers also take a few seconds to notice that an instance is leaving, so give them that time before the process exits. Without it, every deploy produces a small burst of 502s that everyone learns to ignore.
A ready instance can still be a wrong one. It may start fine, answer the health check, and return a 500 on every checkout. That's why the second half of the job is watching what the new version does once it has traffic, and deciding in advance what "worse" means.
With Argo Rollouts, the steps and the check between them are part of the deployment definition:
strategy:
canary:
steps:
- setWeight: 5
- pause: { duration: 10m }
- analysis:
templates:
- templateName: error-rate-vs-stable
- setWeight: 25
- pause: { duration: 10m }
- setWeight: 50
- pause: { duration: 10m }
The tool matters less than the rules behind it:
- Compare with the version that's running, at the same time. A fixed threshold like "error rate under 1%" fails on a bad network day and passes a version that doubled the errors from 0.2% to 0.4%. Put version 1 and version 2 side by side over the same minutes.
- Use the signals from Part 9. Error rate, latency at the tail, saturation. Add one business number if you have it, such as orders completed per minute. Some of the worst releases return 200 for everything and quietly stop doing the work.
- Wait long enough. Some problems only show up after a while: a memory leak, a connection pool that slowly runs dry, a cache that expires after fifteen minutes.
- Write the thresholds before the deploy. In the middle of a rollout that's going badly, people are very good at explaining why this particular spike doesn't count.
- Let the abort happen without a person. Stopping a rollout and going back is the safe direction, so it shouldn't wait for anyone's approval. Asking a person to confirm the last step to 100% is fine.
None of this requires a canary. A rolling deployment can do the same thing in a simpler form: pause after the first instance, look at the same metrics, and roll back if they got worse. Kubernetes won't do that comparison for you with a plain Deployment, but a pipeline step that queries your metrics after the rollout can.
Feature flags are a different tool
Deployment strategies and feature flags get discussed as alternatives. They solve different problems.
A deployment strategy decides which version of the binary handles a request. A feature flag decides which behaviour a user gets inside one binary. Part 8 called this separating deploy from release: the code is in production and switched off, and turning it on is a configuration change.
| Deployment strategy | Feature flag | |
|---|---|---|
| What it controls | Which build serves traffic | Which code path runs |
| Granularity | A share of requests or instances | A user, a tenant, a percentage, a region |
| Speed of going back | Seconds to minutes | Seconds, no deploy |
| Protects against | Anything in the build: dependencies, config, memory, startup | A problem in the flagged feature |
| What it costs you | Infrastructure and pipeline work | Two code paths to test, and cleanup later |
The fourth row is the one to remember. A flag can't protect you from a Spring Boot upgrade, a new JDK, a changed connection pool setting or a memory leak in a library, because those affect the whole process whether the flag is on or off. And a canary is a blunt way to release a new checkout flow to ten pilot customers.
So use both. The deployment strategy makes it safe to put a new build into production. Flags then control who sees the risky behaviour inside that build.
Flags have a cost that grows quietly. Each one is a branch that needs testing in both positions, and ten flags make more combinations than anyone tests. Give each flag an owner and a removal date when you create it, and delete it once the feature is fully on. A flag that's been "temporarily" on for two years is technical debt of the kind Part 5 was about.
The database doesn't roll back with the code
All three strategies move traffic between versions of the application. None of them does anything about the database, and there's only one of those. This is where a rollback plan that looked good in the diagram turns out not to work.
One rule covers most of it: at any moment, the schema has to work with the version you're deploying and with the one before it. That's a requirement for a rolling deployment to work in the first place, since both versions are running. It's also what makes rollback possible in all three, because rolling back means running the old code against the new schema.
Part 7 covered the mechanics as expand and contract. Seen from the deployment side, it comes down to a few habits:
- Add in one release, remove in a later one. New columns and tables are invisible to old code. Dropping or renaming something is safe only after no running version uses it, which means at least one release later, and later still if you want to keep the option of going back.
- Don't plan on down-migrations in production. A script that undoes the schema change sounds like a rollback plan. In practice it runs for the first time during an incident, and for anything that dropped or transformed data it can't bring the data back. Roll back the application and leave the schema where it is. If the schema itself is wrong, fix it with a new forward migration.
- Run migrations as their own step. If every instance runs migrations on startup, four instances race for the migration lock. A long migration can also outlast the startup probe, and then the platform kills the instance halfway through. One pipeline job, before the rollout, with its own timeout and its own log, avoids both. Database Migrations with Flyway and Liquibase in Spring Boot compares both options in a Spring Boot app.
- Remember that data has versions too. This one gets past careful
teams. Version 2 introduces a new order status,
PARTIALLY_REFUNDED, and starts writing it. Something else goes wrong and you roll back. Version 1 now reads those rows, callsOrderStatus.valueOf("PARTIALLY_REFUNDED")and throws on each one. The schema never changed, and the rollback still broke production.
The fix for the last one is an ordering rule: teach the code to read something one release before any code writes it. The first release understands the new status and never produces it. The next release, or a flag, starts writing it. The same order applies to messages on a queue and to fields in an API: consumers first, producers after.
I've seen how much this is worth. On one platform I worked on, we moved Liquibase execution into the services' CI/CD pipeline as an automated step. Until then, about two deploys a month failed for database reasons. After the change that number went to zero, and total deployment time dropped by around 30%. Migrations that run the same way every time, in one place, before the new code starts, are the part of that work I'd repeat anywhere.
How to choose
You don't have to pick one strategy for the whole company, or even for one service for its whole life. A reasonable way to decide:
Start with rolling, done properly. That means honest readiness checks, graceful shutdown, backward-compatible releases and a look at the metrics afterwards. This costs nearly nothing and covers most services. In my experience, the teams that have painful deploys rarely need a more advanced strategy. They usually have a readiness check that returns 200 too early and migrations that assume one version at a time.
Use blue-green when going back has to be instant, or when two versions can't share traffic. Examples are a large infrequent release, a runtime or framework upgrade you want to test at full size before anyone uses it, or a client contract that doesn't tolerate mixed responses. You pay for the second environment and you get a switch that goes both ways.
Use canary when you have the traffic and failures are expensive. A high-volume service where a bad release costs money every minute is the natural case. Be honest about the prerequisites: per-version metrics, traffic splitting and automated analysis. A canary where someone looks at a dashboard for a while and clicks "promote" isn't giving you much more than a rolling deployment with a pause would.
Add flags for product risk, whichever you chose.
Recreate, the stop-and-start from the introduction, still has a place too. For a batch worker, an internal tool, or a change that really can't run in two versions at once, a short planned window is sometimes the simplest correct answer. It should be something you chose. It shouldn't be what happens because nobody configured anything else.
Whatever you pick, Part 10 applies when it goes wrong: mitigate first. A rollback you've practised is the cheapest mitigation you'll ever have.
What to try this week
- Find out which strategy you're using. Open the deployment definition of one service and read the settings. How many instances can be down at once? How many extra can be created?
- Read your readiness check. What does it verify? Start the service with the database unreachable and see what the endpoint returns.
- Deploy during steady traffic and count the errors. If there are 502s or connection resets during every rollout, look at graceful shutdown and at how long the load balancer takes to deregister an instance.
- Roll back on purpose, in staging, and time it. Then ask whether the previous version would have run against today's schema.
- Check where migrations run. At application startup, or as a pipeline step?
- List your feature flags. How many are fully on and still in the code?
The short version
- Every strategy answers three questions: how many users see the new version first, how you notice a problem, and how you go back.
- Rolling replaces instances a few at a time. It's cheap, it's the default, and it needs every release to be compatible with the previous one.
- Blue-green runs two full environments and switches between them. Rollback is a routing change, as long as the database, queue consumers and cold starts are accounted for.
- Canary exposes a small share of real traffic and compares versions. It needs traffic splitting, per-version metrics and enough volume to mean something.
- Readiness should mean "can serve a request". Keep dependencies out of liveness. Shut down gracefully.
- Decide the abort criteria before the deploy, compare against the running version, and let the abort happen automatically.
- Flags control behaviour inside a build, and deployment strategies control which build runs. You need both.
- The schema, and the data in it, must work with the current version and the previous one. Add first, remove later, read before you write.
So far Module B has been about getting a change into production and seeing what it does there: the pipeline, the signals, the incident, the platform and now the release itself. All of it assumed someone had already decided what the system looks like. In Part 13 that someone is you: system design basics, and how to approach your first architecture decision.
This is Part 12 of 18 in the series. The Senior Engineer Roadmap maps the whole series, with a short self-assessment and four reading orders.