Chapter 4 · Lesson 9 of 18
Deploy and operate

Blue-green vs rolling deploys

Blue-green deploys run old and new versions side by side and switch traffic at once, while rolling deploys replace instances a few at a time.

By the end you will

Choose between blue-green and rolling deploys for your app and check a deploy for gaps.

Time
20 min
Level
Beginner
Check
4 questions

Blue-green deploy

Blue and green are two full copies of the app: you prepare the idle one, test it, then switch the router, and you can switch back instantly if something is wrong.
  1. Users always go through the router.
  2. Blue is live and takes all the traffic.
  3. Green is a full second copy, started with the new version and tested while idle.
  4. When green looks good, the router sends all traffic to it at once.
  5. Blue stays running as an instant way back if something breaks.
Static diagram
  1. UsersrequestRouter
  2. RouterliveBlue
  3. RoutertestGreen
  4. Routerswitch allGreen
  5. RouterstandbyBlue

Both strategies answer one question: how do you ship a new version without users noticing? Blue-green runs two full copies and flips traffic once. Rolling replaces instances one or a few at a time.

A short history#

The blue-green name comes from teams that kept two identical production environments and swapped a router between them. Rolling updates grew out of cluster managers and load-balanced fleets, where replacing every machine at once was never an option.

How it works#

Both strategies rely on something that decides where traffic goes: a reverse proxy or a load balancer. Deploying means changing what that component points at.

Blue-green. Blue is live. You start the new version as green beside it and test it directly, without public traffic. When it passes, you change the proxy to send traffic to green. Blue keeps running for a while. If green misbehaves, you point the proxy back at blue. The blue-green deploy switch is a single change, so the rollback is the same change reversed.

Rolling. You have several instances behind the balancer, say four. You take one out of rotation, replace it with the new version, wait for its health check to pass, and put it back. Then the next. Between the first and last step, both versions serve users. A rolling deploy needs little spare capacity, since you only lose one instance’s worth at a time.

A worked example on one Docker host with Compose and nginx: run app-blue on port 3001 and start app-green on 3002. Check green with curl -fsS http://127.0.0.1:3002/health. Edit the nginx proxy_pass from 127.0.0.1:3001 to 127.0.0.1:3002, run sudo nginx -t, then sudo systemctl reload nginx. Reload keeps open connections on the old worker processes until they finish, so most requests are not cut. Stop blue later, after you are sure.

Comparison#

QuestionBlue-greenRolling
Rollback speedOne switch, if blue is still runningRoll again, which takes as long as the deploy
Spare capacity neededA full second copy for a whileA little
Both versions live at onceBriefly, around the switchFor the whole deploy
Works with one instanceYes, as a swap on one hostNo
Blast radius of a bugEveryone after the switchThe share already replaced
Typical homeSingle host, small teamsClusters, fleets

Common misconceptions#

  • A switch does not make a deploy free of risk. Everything that depends on shared state still applies.
  • Blue-green does not need two servers. Two containers on one host work, if you have the memory.
  • Rolling does not mean slow. The speed depends on how long a health check takes, not on the strategy.
  • Neither strategy handles database changes for you.

When not to use it#

For a hobby app that can be down for ten seconds, a plain docker compose up -d may be the right call. Blue-green on a host with little memory can run out of RAM while two copies run. A rolling deploy of two instances with no spare capacity can drop below what the load needs. Pick the simplest option that meets your uptime need.

Check it worked#

During a deploy, keep requesting your health URL in a loop and watch for failures:

bash
while true; do curl -fsS -o /dev/null https://example.com/health || echo "fail"; sleep 1; done

-fsS makes curl fail on 4xx and 5xx but still print errors, and -o /dev/null discards the body. A clean deploy prints nothing. Any fail line shows a gap to fix. After the switch, docker compose ps should show only the version you intend to keep.

Edge cases and gotchas#

  • Database changes break the overlap. For a time old and new code share one database. Add columns first, ship code that handles both, remove the old column in a later deploy.
  • Sessions can reset. If sessions live in one instance’s memory, a switch logs users out. Use a shared store or signed cookies.
  • Long requests can be cut. A request in flight on the old version may be dropped when you stop it. Let the old version drain, and give it a stop timeout longer than your slowest request.
  • Background jobs run twice. Old and new workers can both pick up the same queue or scheduled task. Pause workers on the old version before the switch, or make jobs safe to repeat.
  • A health check that is too shallow. A check that only returns 200 can send traffic to a version whose database connection is broken. Check what the app needs.
  • Memory on small servers. Two copies of a heavy app can push a small server into swap or an out-of-memory kill. Check free -h first.
  • Cached DNS and sticky connections. If your switch is a DNS change, clients keep using the old address until the TTL ends. Prefer switching at the proxy.

Troubleshooting#

SymptomLikely causeFix
502 right after the switchGreen is not ready or listens on the wrong portCheck green directly first, see Fix a 502 Bad Gateway
Some users get errors, others do notRolling deploy in progress, or one instance is unhealthyCheck each instance’s health and logs
Users logged out after the switchSessions stored in instance memoryMove sessions to a shared store
Old code errors on a missing columnSchema change shipped in the same deployUse a two-step migration
Nginx still sends traffic to blueConfig not reloadedsudo nginx -t then sudo systemctl reload nginx
Green fails to start with a memory errorNot enough RAM for two copiesStop other services or add swap
Duplicate emails or jobs after deployTwo workers ran at oncePause old workers before the switch
Rollback to blue shows old data errorsNew code changed data in a way old code cannot readPlan data changes to be reversible

Real-world scenario#

A solo developer runs an app and Postgres on one VPS. Deploys used to take the site down for about twenty seconds. They switch to a blue-green style swap behind nginx. For one release they need to rename a column. Instead of renaming, they add the new column and keep writing both. They start green on 3002 and check it, then flip nginx:

bash
curl -fsS http://127.0.0.1:3002/health
sudo nginx -t && sudo systemctl reload nginx

The health loop in another terminal prints nothing. Two hours later a bug shows up in a rarely used page. They point nginx back at blue, which is still running, and the page works again. The old column is removed in a later release, once nobody needs the old code.

Quick reference#

GoalCommand or rule
Check the new version before switchingcurl -fsS http://127.0.0.1:3002/health
Switch at the proxyEdit proxy_pass, then sudo nginx -t && sudo systemctl reload nginx
Watch for gapsHealth loop with curl -fsS
Roll back, blue-greenPoint the proxy at the old color
Roll back, rollingDeploy the old version the same way
Schema changesAdd first, remove in a later deploy

Next#

See what a failed switch looks like and how to fix it: Fix 502 Bad Gateway behind a reverse proxy.

Check what you learned4 questions
1Why is a rollback very fast with blue-green?
2Which situation leans toward a rolling deploy?
3Do these strategies solve database schema changes?
4You rename a database column in the same deploy that ships code using the new name. What goes wrong?

Frequently asked questions

Which deploy strategy is better for a single small server?

Often a blue-green style swap, because a rolling deploy needs several instances to work. Start with whichever you can run and understand.

Do blue-green and rolling deploys fix database changes?

No. Both run old and new code against one database for a while, so schema changes must work with both versions.

Why do users get logged out after a blue-green switch?

Sessions stored in the old instance's memory do not move with the traffic. Keep sessions in a shared store, or in signed cookies, so both colors can read them.

Does a health check make a deploy zero-downtime?

It helps, because traffic only goes to a version that answers. It does not help with in-flight requests or schema changes, which need their own care.

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