Chapter 4 · Lesson 17 of 18
Deploy and operate

Canary releases explained: shipping to a few users first

A canary release routes a small share of live traffic to the new version first, and you promote it to everyone only if error rate and latency stay healthy.

By the end you will

Run a canary with weighted traffic, decide on promote or rollback from metrics, and know when canaries are the wrong tool.

Time
14 min
Level
Intermediate
Check
4 questions
You need
  • An app behind nginx or another reverse proxy
  • A way to see error rate and latency

A canary release sends a small share of real traffic to the new version while everyone else stays on the old one. You watch error rate and latency on both. If the canary looks healthy you raise its share until it takes everything. If not, you set its share to zero and the damage stays small. It is a safer cousin of a normal deploy, at the cost of running two versions side by side.

How it works#

The name comes from the canary in a coal mine: a small early warning. The mechanism has four parts:

  1. Start the new version next to the old one, without removing the old one.
  2. Route a small percentage of requests to the new version at the proxy.
  3. Compare the new version’s error rate and latency with the stable one over a set period.
  4. Promote it to full traffic, or remove it and send everything back to the stable version.

Because the stable version keeps serving most users the whole time, rollback is just a routing change. That is faster than redeploying. See rollbacks for the other rollback methods.

Canary vs blue-green vs rolling#

  • A rolling deploy replaces instances one by one. Users hit a mix of versions during the rollout, but you rarely control the exact share.
  • A blue-green deploy prepares a full second environment and switches all traffic at once. Rollback is a quick switch back, but a bad release hits everyone.
  • A canary keeps both live and moves traffic in small, controlled steps, so you learn from a slice of real users before committing.

The first two are compared in blue-green vs rolling deploys. Neither one replaces the aim of zero-downtime deploys: the canary is about risk, not just downtime.

An nginx weighted upstream#

This is an illustration, not a complete production setup. Run the stable and new versions as two containers on one Docker network, and give the new one a small weight:

nginx
upstream app {
    server app-stable:8080 weight=9;
    server app-canary:8080 weight=1;
}

server {
    listen 80;
    server_name app.example.com;

    location / {
        proxy_pass http://app;
        proxy_set_header Host $host;
    }
}

With weights 9 and 1, about one request in ten reaches the canary. The share is approximate and applies to requests, not users, so one visitor may see both versions. After editing the config, test and reload without dropping connections:

bash
sudo nginx -t && sudo nginx -s reload

If nginx runs in a container, run the same two commands with docker exec into that container. To promote, change the weights step by step, for example 5 and 5, then remove the stable server. To roll back, delete the canary line or set it to down, and reload.

What to measure#

Decide the pass or fail rules before you start, so you are not judging by feel:

  • Error rate: the share of 5xx responses on the canary compared with stable.
  • Latency: median and slow-tail response times from the proxy logs.
  • Business signals: failed logins, failed payments or other app-specific errors.
  • Resource use: memory growth or restarts of the canary container.

Add a label or header to canary traffic, such as a log field with the upstream name ($upstream_addr in nginx), so you can split the numbers. A health check on the canary stops obviously broken containers from receiving traffic, and external uptime monitoring catches wider problems. Keep the threshold values in config or a runbook, not in someone’s head.

Sticky sessions#

Weighted round-robin can send one user to different versions on different requests. That breaks apps that keep session data in memory, and it confuses users if the interface changes between clicks. Options: store sessions in a shared place such as Redis or the database, or make routing sticky. In nginx you can use ip_hash in the upstream, which ties a client address to a server, although it ignores weights in a simple way and clients behind one NAT share a fate. Cookie-based routing needs extra modules or a different proxy. Pick the approach that matches how your app handles state.

Database migrations#

Both versions use the same database at the same time. A migration that drops or renames a column breaks the stable version the moment it runs. Use the expand and contract pattern: first add new columns or tables in a way both versions tolerate, deploy the code that uses them, and only later remove the old structure once no old version is live. Test that the previous version still works against the new schema before you start the canary.

Check it worked#

Send a batch of requests and look at the split in the access log:

bash
for i in $(seq 1 50); do curl -s -o /dev/null https://app.example.com/; done

Then count by upstream in the log, if you added $upstream_addr to the log format. You should see roughly the share you configured. Check the canary container’s own logs for errors:

bash
docker logs --tail 50 app-canary

Finally compare error counts over your chosen window before changing any weights.

Edge cases and gotchas#

  • Low traffic: With few requests, a 5 percent canary may see almost nothing. Raise the share or run longer, or the signal means little.
  • Request vs user share: Weighted rotation splits requests. A single user may bounce between versions unless routing is sticky.
  • Background jobs: Workers and scheduled jobs are not behind the proxy, so a canary of the web tier does not cover them.
  • Caches: Responses cached by a CDN or browser can hide which version served them.
  • Shared state: Cache formats, queue messages and file layouts must work for both versions at once.
  • Cost of two versions: You run extra capacity during the canary, and incident debugging gets harder when two versions are live.

Troubleshooting#

SymptomLikely causeFix
Canary gets no trafficWeight is zero, or the container is downCheck the upstream, container health and nginx -t
Users see flipping behaviorRequests alternate between versionsUse shared sessions or sticky routing
Stable version errors after migrationSchema change not backward compatibleRoll back the migration step, use expand and contract
Cannot tell canary errors apartLogs lack an upstream fieldAdd $upstream_addr to the log format
Reload failsConfig syntax errorRun nginx -t and fix before reloading

Quick reference#

bash
sudo nginx -t && sudo nginx -s reload
docker logs --tail 50 app-canary
docker compose up -d app-canary
docker compose stop app-canary

Next#

Continue with feature flags vs deploys.

Check what you learned4 questions
1What is the main purpose of a canary release?
2What signals do you watch during a canary?
3In an nginx upstream, what does weight=1 versus weight=9 do?
4Why do database migrations complicate canaries?

Frequently asked questions

What is a canary deployment?

It is a rollout where a new version receives a small percentage of production traffic while the old version serves the rest. If metrics look healthy you increase the share, otherwise you roll back.

What is the difference between canary and blue-green?

Blue-green switches all traffic from one full environment to another at once. A canary shifts traffic gradually, so a bad release affects only part of your users.

Can I do a canary release without Kubernetes?

Yes. Run the new version as a second container and give it a small weight in a reverse proxy such as nginx. Watch logs and metrics before shifting more traffic.

How long should a canary run?

Long enough to see a meaningful number of requests and any slow-building problems. The right time depends on your traffic and the kind of change, so decide it before you start.

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios