SDSystem DesignConcepts to case studies
System DesignDSA
x
0/311
Part 1: Foundations
Part 2: APIs and Communication
Part 3: Caching
Part 4: Data and Storage
Part 5: Architecture
Part 6: Distributed Systems
Part 7: Advanced Data Topics
Part 8: Operations and Security
  • Deployment Strategies Overview
  • CI/CD Pipelines
  • Rolling Deployments
  • Blue-Green Deployments
  • Canary Releases
  • Feature Flags
  • A/B Testing Infrastructure
  • Schema Migrations
  • Rollbacks and Immutable Infrastructure
  • Multi-Region and Active-Active
  • Infrastructure as Code (Terraform)
  • Auto-Scaling Policies
  • Zero-Downtime Deployment
  • Chaos Engineering
  • Shadow Traffic and Dark Launches
Part 9: Estimation and Interview Method
Part 10: Case Studies
System Design/Operations and Security/Deployment Patterns

Blue-Green Deployments

Run two full copies of production, then switch the traffic from one to the other.

⏱ 12 min readIntermediateMust KnowInterview FavouriteHas visualizer
SummaryConceptReal-Life ExampleWhy?Where?How It WorksStepsKey TermsPros & ConsMistakesReal WorldRead NextInterview Q&ACheat-Sheet

01Quick Summary

A blue-green deployment keeps two full copies of production. One (say Blue) serves the users. You put the new version on the other (Green), test it, and then switch the traffic over in one step.

  • Users never see a half-updated system: they are on the old version, then on the new one.
  • The old copy keeps running, so rolling back is just switching back.
  • The price is double the servers for a while, and a careful plan for the database.

02Concept: What is it?

Normally a deployment changes the servers that users are using right now. That is risky: if the new code is bad, users feel it at once. Blue-green avoids this by never touching the live servers.

You have two identical environments, called Blue and Green (the colours are only names). At any time one is live and the other is idle. A release goes like this: deploy the new version to the idle one, test it with no users on it, and then change the router so all traffic goes there. The old environment becomes the idle one, ready for the next release or for a rollback.

One switch moves all traffic
Before
πŸ”€Router
↓100%
πŸ–₯️Blue v1live
πŸ–₯️Green v2idle, tested
After the switch
πŸ”€Router
↓100%
πŸ–₯️Blue v1standby
πŸ–₯️Green v2live
Before the switch, Green is invisible to users. After it, Blue is the safe standby.
🧠The shared parts are the hard part
The two environments usually share the database, queues and other data stores. Switching code is easy and instant. Switching data is not. A good blue-green plan makes every data change work with both versions.

03Real-Life Example

🍡Two Stages at a Theatre
A theatre has two identical stages. While the audience watches the play on Stage 1, the crew builds the next set on Stage 2 and rehearses it in private. When it is ready, the audience simply walks through a door to Stage 2. No one watches the crew working. If the new set has a problem, everyone walks back to Stage 1, which is still set up exactly as before.
The theatre
Blue-green deployment
Stage 1 with the audience
The live environment (Blue)
Stage 2 being built in private
The idle environment (Green) with the new version
Rehearsal with no audience
Smoke tests on Green before the switch
Audience walks through the door
The router sends all traffic to Green
Stage 1 left as it was
Blue stays as a standby for a rollback
Both stages cost money to run
Double servers during the release
Both stages share the same ticket office
Both versions share one database
Life of one release
πŸš€Deployv2 to idle Green
→↓
πŸ§ͺTestno users yet
→↓
πŸ”€Switchrouter flips
→↓
πŸ‘€Watchkeep Blue warm
→↓
🧹RetireBlue becomes next idle

04Why Do We Need It?

πŸ•³οΈNear-zero downtime
Users move to a fully started, already tested environment, so there is no gap while servers restart.
↩️Instant rollback
The old version is still running. Going back is one routing change, not a new deployment.
πŸ§ͺTest in production-like conditions
Green is a real copy of production, so tests there tell you more than tests on a small staging box.
🎯All-or-nothing for users
No user sees a mix of old and new servers in the same session.
😌Calm releases
You can deploy at any time and switch when you are ready, not in a rush at midnight.
🧯Disaster practice
Having a second environment you can switch to is good practice for recovery drills.
Build the new version beside the old one, test it where no user can see, then move everyone in one step. Keep the old one for the way back.

05Where Do We Use It?

🏦Critical services
Payments, banking and booking systems where even a short outage or half-updated state is costly.
☁️Cloud platforms
Features such as AWS CodeDeploy, Elastic Beanstalk swap, Azure deployment slots and Kubernetes service selectors support the switch.
πŸ”€Load balancer or DNS switch
The switch is a change of target group, a service selector, or a DNS record (DNS is slower and cached).
πŸ“¦Immutable infrastructure
New servers or containers are built fresh for Green rather than patched in place.
🧰Big version upgrades
Changing a runtime, a framework or an operating system is easier on a clean copy.
πŸ§‘β€πŸ€β€πŸ§‘Teams that ship often
Safe, repeatable releases let teams deploy many times a day.

06Types / Parts / How It Works

How the switch can be done

Switch methodHow it worksGood and bad
Load balancer target swapThe load balancer points to the Green servers instead of the Blue onesFast and clear. Existing connections may need time to drain.
Kubernetes service selectorA Service changes its label selector from version=blue to version=greenSimple in Kubernetes. Pods for both versions must be running.
DNS changeThe name now points to the Green addressEasy anywhere, but caches mean some users stay on Blue for a while. Rollback is also slow.
Platform slot swapA cloud platform swaps two named slots (staging and production)One click, warms the slot first. Tied to that platform.

The database problem

Expand, then contract
βž•Expandadd the new column, keep the old
→↓
πŸ”€Switchv2 uses the new, v1 still works
→↓
⏳Waituntil v1 is not needed
→↓
βž–Contractdrop the old column
Safe for both versions Only after rollback is no longer needed
Make every database change safe for both versions, and remove the old parts only after the old version is gone.

If you rename a column in one step, version 1 breaks the moment the change runs, so your instant rollback is gone. See Schema Migrations for the full recipe.

VisualizerSwitch traffic from Blue to Green, then roll backWalk through the four steps. Change what is wrong with version 2, then try a breaking database change and roll back to see what that does.
What is wrong with version 2?
The database change

Version 2 is fine. The database change works with both versions (expand first, clean up later).

πŸ‘₯Users
↓
πŸ”€Router / load balancerpoints to Blue
↓100%
Blue: version 1 (live)
πŸ–₯️v1
πŸ–₯️v1
πŸ–₯️v1
⇣0%
Green: empty
no serversnot deployed
πŸ—„οΈShared databaseold layout
Blue gets
100%
of traffic
Green gets
0%
of traffic
Failing requests
0%
illustrative
Servers running
3
normal cost
Smoke tests
not run
Blue (version 1) serves everyone. Green is empty. Press Deploy to put version 2 on Green. Real users are not affected yet.

A simplified model. The failing share (30%) and server counts are illustrative, not measurements. Try each bug type, and try a breaking database change before you roll back.

07How to Use It / Steps / Implementation

Doing a blue-green release

  1. 1
    Make the two environments the same
    Build Green from the same code, scripts and settings as Blue (infrastructure as code helps). Differences cause surprises.
  2. 2
    Deploy the new version to the idle side
    Install version 2 on Green. No user traffic goes there yet.
  3. 3
    Test Green
    Run smoke tests, health checks and a few real requests (or replayed traffic). Check logs and metrics.
  4. 4
    Prepare the data
    Run only database changes that work with both versions (the expand step). Warm caches if needed.
  5. 5
    Switch the traffic
    Change the router, selector or slot. Let in-flight requests on Blue finish (connection draining).
  6. 6
    Watch closely
    Check error rate, latency and business numbers for a while. Keep Blue running and ready.
  7. 7
    Roll back or retire
    If something is wrong, switch back. If all is well, after some time Blue becomes the idle side for the next release.
πŸ’‘Keep long-lived work in mind
Users with open sessions, WebSocket links or jobs in progress may be on Blue at the switch. Use connection draining, share sessions in a store (not in server memory) and make background jobs safe to move.
⚠️Do not run the destructive step too early
Dropping the old column or table right after the switch removes your rollback. Wait until you are sure you will not need version 1 again.

08Key Terms to Know

TermMeaning in easy words
EnvironmentA full copy of the system: servers, settings and connections
Blue / GreenNames for the two environments. They have no meaning beyond telling them apart
Live / idle (standby)The side that serves users, and the side that is waiting
Router switchThe change that sends all traffic to the other environment
Smoke testA quick check that the main functions of the new version work
RollbackGoing back to the previous version
Connection drainingLetting requests already in progress finish before a server stops
Expand and contractAdd new things first, remove old things last, so both versions work
Immutable deploymentNew servers are built for each release instead of changing old ones
Zero downtimeUsers do not notice any gap in service during the release
Warm-upStarting caches and connections on the new side before users arrive

09Pros, Cons and Trade-offs

βœ… Pros
  • Almost no downtime
  • Very fast rollback
  • Test the new version on a real copy of production
  • Users never see a mix of versions
  • Simple idea to explain and to automate
⚠️ Cons
  • Needs double the servers during the release
  • Shared database changes are tricky
  • All users switch at once, so a bug reaches everyone
  • Both environments must be kept identical
  • Long-running sessions and jobs need care

Trade-offs

ChoiceWe gainWe lose
Blue-green instead of rollingInstant rollback and no mixed versionsHigher cost, as there are two full copies
Blue-green instead of canarySimple, clear switchNo gradual exposure: a hidden bug hits 100% of users at once
Keep Blue for a long timeA safe way backPaying for idle servers
Shut Blue down quicklyLower costRollback needs a new deployment
Shared databaseNo data copying and no data lost on switchEvery data change must fit both versions
Separate database per colourFull isolationHard to keep data in sync and to switch without losing writes

10Common Problems and Mistakes

Common mistakes

  1. A breaking database change. The change runs, and Blue cannot work with it any more. Rollback does not help.
  2. Environments that drift apart. Green has a different setting or library from Blue, so tests lie.
  3. Switching without watching. The switch is quick, but you still need to check metrics after it.
  4. Tearing down Blue too early. The rollback option disappears just when you may need it.
  5. Forgetting the cost and capacity. Two copies may exceed your quota or your database connection limit.
  6. Relying on DNS for a fast switch. Caches keep some users on the old side for minutes or longer.
  7. Losing in-flight work. Requests, sessions or jobs on Blue are cut at the switch.

Things to watch

πŸ—„οΈData compatibility
Every change must be readable by both versions until the rollback window closes.
πŸ“¨Queues and jobs
Messages written by v2 may be read by v1 after a rollback. Keep message formats compatible.
πŸ”ŒExternal callbacks
Webhooks and third-party systems may still point at the old side.
πŸ”₯Cold start
Green may be slow right after the switch if caches and connections are empty.

11Real-World Examples

☁️Cloud deployment services
Several cloud platforms offer blue-green style deployments, such as AWS CodeDeploy and Azure App Service slots.
☸️Kubernetes
Teams run two Deployments and flip a Service selector or an Ingress route, or use tools such as Argo Rollouts that support the pattern.
🏦Banks and payment firms
Often favour it for the easy, quick rollback and a clean switch.
πŸ›’Retail launches
A big sale can have a fully tested new version ready on the idle side before the event.
πŸ§ͺPre-switch checks
Many teams run automated tests and replay sample traffic on Green before they flip.
πŸ”Combined with canaries
Some teams first send a small share of traffic to Green, then move the rest.
πŸ“Note
Product names and features change over time. Check the current docs of your platform for how its switch works.
Mini case: a safe release with a bad surprise
πŸš€Deploy v2to Green
→↓
πŸ”€Switch100% to Green
→↓errors rise
🚨Alerterror rate up
→↓
↩️Switch backBlue still running
The bug was missed in tests, but the way back was one switch.

12Related Topics to Read Next

Deployment Strategies Overview→

Compare blue-green with the other ways to release.

Rolling Deployments→

The cheaper option that updates servers a few at a time.

Canary Releases→

Expose a new version to a small share of users first.

Schema Migrations→

How to change the database so both versions keep working.

Rollbacks and Immutable Infrastructure→

Going back fast and building servers fresh each time.

What are Load Balancers?β†’

The usual place where the traffic switch happens.

13Interview Questions with Short Answers

14Quick Revision Cheat-Sheet

  • βœ”What: two identical environments, one live and one idle. Deploy to the idle one, test, then switch all traffic.
  • βœ”Rollback: switch back. The old side is still running.
  • βœ”Cost: about double the servers while both run.
  • βœ”Risk: all users move at once, so a bug the tests missed reaches everyone.
  • βœ”Database: shared. Use expand and contract so both versions work. Never drop old parts too early.
  • βœ”Switch tools: load balancer target, Kubernetes selector, platform slot, DNS (slowest).
  • βœ”Always: smoke test Green, drain connections, watch metrics, keep Blue until safe.
  • βœ”One line: β€œBuild the new version on the empty stage, rehearse in private, then move the audience in one step.”
← Previous
Rolling Deployments
Next β†’
Canary Releases