DevOps & Containers

Canary Deployment: Release Safely to a Small Slice of Traffic First

By NileVPS Team · Published · 4 min read

اقرأ المقال بالعربي

Every deployment carries risk. Tests catch many problems, but some bugs only appear under real traffic, with real data and real user behavior. A canary deployment reduces that risk by sending a new version to a small fraction of users first, watching how it behaves, and only then rolling it out to everyone. If something goes wrong, only a small group is affected and the rollback is quick. The name comes from the canaries miners once carried underground as an early warning.

How a Canary Deployment Works

The idea is simple: run two versions side by side and control how much traffic each receives.

  1. Deploy the new version next to the current stable one, without removing anything.
  2. Route a small share of traffic to it, for example 5%.
  3. Compare metrics between the canary and the stable version: error rate, latency, resource usage and business signals.
  4. Increase the share in steps (5% to 25% to 50% to 100%) if everything looks healthy.
  5. Roll back instantly by sending all traffic to the stable version if the canary misbehaves.

The key difference from a simple rolling update is that you decide, based on evidence, whether the release continues. Time spent at each step gives problems a chance to surface.

Canary vs Other Release Strategies

StrategyHow it worksRollback speedExtra resources
RecreateStop old, start newSlow (redeploy)None
Rolling updateReplace instances graduallyMediumSmall
Blue-greenSwitch all traffic between two full environmentsVery fastDouble during switch
CanaryShift traffic gradually with metric checksVery fastSmall to medium

Blue-green moves everyone at once; canary moves people in stages. Many teams combine them: a canary phase inside a blue-green setup. If you want to compare approaches, read our guide to blue-green deployment.

A Simple Canary with Nginx on a VPS

You do not need Kubernetes to run canaries. On a single VPS, Nginx can split traffic between two application instances using weights:

upstream app_backend {
    server 127.0.0.1:8001 weight=95;   # stable
    server 127.0.0.1:8002 weight=5;    # canary
}

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://app_backend;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

Change the weights, test the configuration with sudo nginx -t, then apply it with sudo systemctl reload nginx. A reload does not drop existing connections.

Weights are per request, so one user may hit both versions during a session. If that matters (for example, a changed UI or session format), use split_clients to assign users consistently based on a stable value such as the client IP or a cookie:

split_clients "${remote_addr}" $app_upstream {
    5%   127.0.0.1:8002;
    *    127.0.0.1:8001;
}

server {
    location / {
        proxy_pass http://$app_upstream;
    }
}

Canary Deployments on Kubernetes

With the ingress-nginx controller, you create a second Ingress for the canary Service and mark it with annotations:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: app-canary
  annotations:
    nginx.ingress.kubernetes.io/canary: "true"
    nginx.ingress.kubernetes.io/canary-weight: "10"
spec:
  ingressClassName: nginx
  rules:
  - host: example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: app-v2
            port:
              number: 80

The controller also supports routing by header or cookie (canary-by-header, canary-by-cookie), which is useful for letting internal testers reach the canary before any real users do. For fully automated progression, tools such as Argo Rollouts or Flagger can raise the weight step by step and abort automatically when metrics cross a threshold.

What to Measure During a Canary

A canary is only as good as the signals you watch. Compare the canary against the stable version over the same time window:

  • Error rate: HTTP 5xx responses, exceptions in application logs.
  • Latency: median and p95 or p99 response times, not just averages.
  • Saturation: CPU, memory and database connections used by the new version.
  • Business metrics: sign-ups, checkouts or successful API calls per minute.
  • Logs: new warnings that did not exist before.

Tag logs and metrics with the version (for example a version label or an X-App-Version response header) so you can tell the two groups apart.

Practical Checklist

  • Keep database migrations backward compatible, because both versions run against the same database at the same time.
  • Define rollback criteria before you start, such as "error rate above 1% or p95 latency up by 30%".
  • Hold each step long enough to see real traffic patterns, including peak hours if possible.
  • Make rollback a single command or config change, and practice it.
  • Keep the stable version running until the canary has reached 100% and stayed healthy.
  • Take a snapshot of the server before major releases so you have a safety net beyond the application layer.

A small VPS is enough to practice all of this. You can run both versions behind Nginx on one server from our pricing plans and grow later.

FAQ

How much traffic should a canary receive at first?

Many teams start with 1% to 10%. The share should be large enough to produce meaningful metrics in a reasonable time, but small enough that a failure affects few users. Low-traffic sites may need a larger share or a longer observation window.

Do canary deployments work with database schema changes?

Yes, if the changes are backward compatible. Use an expand-and-contract approach: add new columns or tables first, deploy code that works with both schemas, and remove old structures only after the old version is fully retired.

Is a canary deployment the same as A/B testing?

No. Both split traffic, but a canary checks whether a release is technically safe and is meant to reach 100% quickly. A/B testing compares two variants to learn which performs better for users, and both variants may run for weeks.

Related guides