Deployment strategies: rolling, blue/green, canary and feature flags
Rolling updates, blue/green, canary and feature flags compared on the shorty app: when each one is worth the effort, what a rollback looks like for each, and why database changes make all of them harder. With Kubernetes, Terraform and Go code I checked.
Getting a new version onto servers is the easy part. The hard part is what happens to the requests in flight while you do it, and how fast you can get back when the new version is bad. That's what a deployment strategy is really about.
In this blog we will look at the four you'll hear about most: rolling updates, blue/green, canary and feature flags. For each one I'll show what it looks like for shorty (the URL shortener from the earlier tutorials), when it's worth the effort, and what rollback means. Then the part most articles skip, the database. This is an intermediate post. You should have read CI/CD explained and build once, deploy many, because every strategy here assumes you deploy one immutable image by digest.
I checked the code with Go 1.27.3, Terraform 1.17.1 with the AWS provider 6.73.0, and kubeconform against the Kubernetes 1.35 schemas.
💻 The full code for this tutorial is on GitHub: FenixAra/tutorials/CI-CD/General/deployment-strategies-rolling-blue-green-canary-and-feature-flags. Clone it and run each step yourself.
Recreate, the one nobody names
Before the four, the strategy everyone starts with: stop the old version, start the new one. There is a gap in between where nothing answers.
My own blog runs like this. Ghost runs as one container on a single 2 GB t3.small with Docker Compose, and there isn't room for a second copy of Ghost and MySQL next to the first. So an upgrade is a backup, docker compose up -d with the new image, and a short window where Caddy has nothing to proxy to. For a personal blog that's quite fine. For an API that other services call, it isn't, and that's where the rest of this post comes in.
Rolling updates
A rolling update replaces instances a few at a time. New ones start, pass the health check, get traffic, and then old ones are stopped. It's the default on Kubernetes and on ECS, so you probably already use it.
Here is shorty's Deployment with the two settings that matter:
apiVersion: apps/v1
kind: Deployment
metadata:
name: shorty
spec:
replicas: 4
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1 # at most 5 pods during the rollout
maxUnavailable: 0 # never go below 4 ready pods
minReadySeconds: 10
selector:
matchLabels:
app: shorty
template:
metadata:
labels:
app: shorty
spec:
containers:
- name: shorty
image: ghcr.io/fenixara/shorty:8adcc28bc5e3a8d6166e205123f8d5c7d4bc84f6
ports:
- containerPort: 8080
readinessProbe:
httpGet:
path: /readyz
port: 8080
periodSeconds: 5
envFrom:
- secretRef:
name: shorty-env
maxUnavailable: 0 means capacity never drops. minReadySeconds makes a new pod stay ready for 10 seconds before it counts, which catches the pods that pass one probe and then fall over. The readiness probe has to be /readyz (it checks Postgres), not /healthz, otherwise a pod that can't reach the database still gets traffic.
Two things make rolling updates go smoothly or badly, and neither is in the YAML. The app must shut down gracefully on SIGTERM, which shorty does (more on that in the graceful shutdown post). And for a few minutes old and new versions serve traffic at the same time. Keep that last one in mind, it comes back in the database section.
Rollback is another rolling update in the other direction: kubectl rollout undo deployment/shorty, or redeploy the previous digest. It basically takes as long as the forward rollout did.
When it's enough: most of the time, honestly. If your releases are small and your health checks are real, rolling is a good default and it costs nothing extra.
Blue/green
Blue/green runs two full copies. Blue serves traffic, green gets the new version. You test green while it is idle, then switch all traffic at once. Rollback is switching back, which takes seconds because blue is still running.
On Kubernetes the cheapest version is two Deployments and one Service whose selector you flip:
apiVersion: v1
kind: Service
metadata:
name: shorty
spec:
selector: { app: shorty, color: blue } # flip to green to switch, back to blue to roll back
ports:
- port: 80
targetPort: 8080
The two Deployments are the same as the rolling one, with color: blue and color: green added to the labels and selectors, and different image digests. The switch is one patch:
kubectl patch service shorty --type merge -p '{"spec":{"selector":{"app":"shorty","color":"green"}}}'
When I first tried to check these manifests with kubectl apply --dry-run=client, it failed, because even the client dry run wants to download the OpenAPI schema from a cluster and I had none running. kubeconform -strict checks against the published schemas offline, and it passed all four resources. I tested the patch itself with kubectl patch --local -f bluegreen.yaml ... -o yaml, which prints the changed Service without any cluster.
On AWS you don't have to build this yourself any more. ECS got a built-in blue/green strategy in July 2025, and linear and canary in October 2025, with bake time and automatic rollback. I'll cover that one separately.
The price is double capacity during the switch, and the fact that the switch is all or nothing. If green is bad in a way your tests didn't catch, every user sees it until you revert back to blue.
Canary
A canary sends a small share of real traffic to the new version first: 5%, then 25%, then everything. You watch error rate and latency on the canary, and only continue if it looks like the stable side. A bad release hurts 5% of requests for a few minutes instead of all of them.
With an ALB in front of shorty, a canary is two target groups and a weighted forward. Here is the Terraform, with the percent as a variable:
variable "canary_percent" {
description = "Share of requests sent to the canary target group"
type = number
default = 0
validation {
condition = var.canary_percent >= 0 && var.canary_percent <= 100
error_message = "canary_percent must be between 0 and 100."
}
}
locals {
colors = toset(["stable", "canary"])
}
resource "aws_lb_target_group" "shorty" {
for_each = local.colors
name = "shorty-${each.key}"
port = 8080
protocol = "HTTP"
target_type = "ip"
vpc_id = var.vpc_id
health_check {
path = "/readyz"
matcher = "200"
}
deregistration_delay = 30
}
resource "aws_lb_listener" "https" {
load_balancer_arn = var.alb_arn
port = 443
protocol = "HTTPS"
certificate_arn = var.certificate_arn
default_action {
type = "forward"
forward {
target_group {
arn = aws_lb_target_group.shorty["stable"].arn
weight = 100 - var.canary_percent
}
target_group {
arn = aws_lb_target_group.shorty["canary"].arn
weight = var.canary_percent
}
# keep a client on the same side for 10 minutes once it has been picked
stickiness {
enabled = true
duration = 600
}
}
}
}
The full file has the terraform block and three more variables (vpc_id, alb_arn, certificate_arn), and terraform validate passed on it. Moving the canary is terraform apply -var canary_percent=25. Rollback is -var canary_percent=0.
The weights are the easy bit. A canary is only as good as thing deciding whether to go on. If nobody looks at the canary's 5xx rate, you've built a slow rolling update. Before you try canaries, make sure you can see errors per target group (CloudWatch has HTTPCode_Target_5XX_Count per target group) and decide upfront what number stops the rollout.
Also, shorty has a catch with canaries that I didn't think of at first. Its link cache sits in Redis (shorty:link:<code>), shared by both sides. If the canary writes a cache entry in a new format, the stable side reads it too. Shared state makes a canary less isolated than the percentage suggests.
Feature flags
The first three strategies move code. A feature flag moves behaviour. You deploy the new code switched off, then turn it on for 1% of users, then 10%, without any deploy at all. Rollback is setting the flag to 0.
You don't need a product for this to start. Here is the small package I wrote for shorty. Flags come from one env var, like SHORTY_FLAGS="link_expiry=10":
package flags
import (
"hash/fnv"
"strconv"
"strings"
)
// Flags maps a flag name to a rollout percent (0..100).
type Flags map[string]int
// Parse reads "name=percent,name=percent". Bad entries are skipped, so a typo
// turns a flag off instead of crashing the service.
func Parse(s string) Flags {
f := Flags{}
for _, part := range strings.Split(s, ",") {
name, pct, ok := strings.Cut(strings.TrimSpace(part), "=")
if !ok || name == "" {
continue
}
n, err := strconv.Atoi(pct)
if err != nil {
continue
}
f[name] = min(max(n, 0), 100)
}
return f
}
// Enabled says if the flag is on for this key. The same key always lands in
// the same bucket, so a user doesn't flip between old and new on every request.
func (f Flags) Enabled(name, key string) bool {
pct := f[name]
if pct <= 0 {
return false
}
if pct >= 100 {
return true
}
return bucket(name, key) < pct
}
// bucket hashes name and key into 0..99. The flag name is part of the hash so
// that the same 10% of users don't get every new feature first.
func bucket(name, key string) int {
h := fnv.New32a()
h.Write([]byte(name))
h.Write([]byte{0})
h.Write([]byte(key))
return int(h.Sum32() % 100)
}
The tests check that a key never flips and that the percentages come out right over 100,000 keys:
$ go test -v ./flags/
=== RUN TestRolloutPercent
flags_test.go:45: 0% configured -> 0.00% enabled
flags_test.go:45: 1% configured -> 1.00% enabled
flags_test.go:45: 10% configured -> 9.94% enabled
flags_test.go:45: 50% configured -> 50.06% enabled
flags_test.go:45: 100% configured -> 100.00% enabled
--- PASS: TestRolloutPercent (0.03s)
PASS
In a handler it's one if, with the key being whatever should stay stable (a user ID, or the link code). To see the buckets I wrote a tiny program over shorty's seeded codes:
package main
import (
"fmt"
"os"
"github.com/fenixara/shorty-flags/flags"
)
func main() {
f := flags.Parse(os.Getenv("SHORTY_FLAGS"))
for _, code := range []string{"golang", "pgx", "sqlc", "blog"} {
fmt.Printf("%-7s link_expiry=%v\n", code, f.Enabled("link_expiry", code))
}
}
$ SHORTY_FLAGS="link_expiry=50" go run ./cmd/flagdemo
golang link_expiry=true
pgx link_expiry=true
sqlc link_expiry=true
blog link_expiry=false
Three out of four at 50% looks wrong but it's just four keys. Over 100,000 the test above shows 50.06%. Run it again and you get the same answer, which is the point.
Reading flags from an env var still means a restart to change them, which is fine for shorty. Once you want to change flags without a restart, or per customer, that's when a flag service or a flags table earns its place. Either way, delete flags after the rollout. Old flags piles up fast and nobody remembers which ones are safe to remove.
The database makes all of them harder
Every strategy above has a window where old and new code run at the same time against the same database. Rolling has it for minutes. Blue/green has it while green warms up and during any rollback. A canary has it for the whole canary. Even feature flags have it, the code for both paths is live.
So a schema change that the old code can't handle breaks all four. Rename a column, and the old pods start failing the moment the migration runs itself. Roll back the code after a migration and the old code meets the new schema. The answer is to only make changes both versions understand, the expand and contract approach from zero downtime schema changes, and to run the migration as its own step before the new code goes out, like in the goose one-off ECS task post.
The rule I'd follow: a release either changes the schema or changes the code that depends on it, never both at once. That gives you a rollback that actually works for any of the strategies.
Picking one
For a small team I'd start with rolling updates and real health checks, add feature flags for risky features, and stop there until there's a reason. Blue/green earns its place when rollbacks must be instant. Canaries earn theirs when you have enough traffic for 5% to mean something and the metrics to judge it. With 20 requests a minute, a 5% canary sees one request.
There we have it. Pick the simplest one that lets you undo a bad release quickly, and spend the rest of the effort on health checks and migrations.