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

Rollbacks and Immutable Infrastructure

Go back fast when a release fails, and never patch servers in place.

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

01Quick Summary

A rollback means going back to the last version that worked, fast, when a release goes wrong. Immutable infrastructure makes this easy: you never change a running server. You build a new one from a versioned image and replace the old one.

  • Mutable servers are changed in place (patches, hand edits), so over time they drift apart and nobody knows their exact state.
  • Immutable servers are replaced, never edited. Every server from one image is identical.
  • A rollback of code is simple. A rollback of data is hard, so plan database changes with care.

02Concept: What is it?

Every release can fail. What matters is how quickly and how safely you can return to a good state. A fast rollback turns a scary incident into a small hiccup.

In the old style, called mutable infrastructure, you log in to a server and update it: install a package, change a setting, copy new code. After a year each server has a different history. Two servers that should be equal are not. When a release fails, an "undo" script runs on servers it was never tested on.

With immutable infrastructure, a server is built once from a recipe (a container image, a VM image) with a version number, such as app:1.4.2. To release, you start servers from app:1.4.3 and stop the old ones. To roll back, you start app:1.4.2 again. Nothing is repaired in place. The old image still exists, exactly as it was.

Patching in place vs replacing from an image
Mutable: change in place
πŸ–₯️Serverv1 + patch A + hand edit + patch B
↓roll back = undo script
❓Unknown statenot the same as v1
Immutable: replace
πŸ“¦Image v1stored, unchanged
πŸ“¦Image v2new, bad
↓roll back = start v1 again
πŸ–₯️Server from v1exactly as before
With images, the previous version is not a memory or a script. It is a real, ready artifact.
🧠Roll back or roll forward?
Sometimes going back is not possible (data changed) or too slow. Then you roll forward: ship a small fix as a new version. Good teams are ready for both.

03Real-Life Example

🍡Spare Phones vs Repairing Your Own
A company gives its staff phones. In the first style, each person tweaks their own phone: new apps, changed settings, a cracked screen fixed with tape. When an update breaks a phone, the help desk tries to undo it, but every phone is different, so each repair is a mystery. In the second style, every phone is set up from the same factory image. If an update breaks a phone, the help desk hands out a fresh phone with the previous image, and the staff member is working again in minutes. The broken phone is thrown away, not repaired.
Phones
Servers
A phone with personal tweaks
A server that drifted from the rest
The factory image
A versioned container or VM image
Handing out a fresh phone
Replacing a server instead of fixing it
The previous factory image, kept on a shelf
The old image, ready for a roll-back
Throwing away the broken phone
Terminating the bad instance
Photos stored on the phone
Data on the server (it must live somewhere else)
Cloud backup of photos
A database or object store outside the server
One release and one rollback, with images
πŸ”¨Buildapp:1.4.3
→↓
πŸ“¦Registrystores every version
→↓
πŸš€Releasestart 1.4.3, stop 1.4.2
→↓bad!
↩️Roll backstart 1.4.2 again

04Why Do We Need It?

⚑Fast recovery
Starting an old image takes minutes. Debugging a half-patched server can take hours.
🧬Identical servers
Every server from the same image is the same, so a bug on one is a bug on all, and a fix is the same everywhere.
πŸ”No drift
Nobody can log in and tweak a server and then forget. What runs is exactly what was built.
πŸ§ͺTest what you ship
The image you tested in staging is the same one that runs in production.
πŸ›‘οΈSafer security
Instead of patching a live server, you build a patched image and replace everything. Old, unknown servers do not hang around.
πŸ“ˆEasy scaling
New servers come from the same image, so adding capacity is just starting more.
If you can rebuild any server from scratch in minutes, you do not have to fear changing or losing one.

05Where Do We Use It?

🐳Containers
A Docker or OCI image with a version tag or a digest. Kubernetes keeps the history of a Deployment and can undo it.
☁️Cloud VMs
Machine images (AMIs and similar) baked with tools such as Packer, then started in an auto-scaling group.
🧱Infrastructure as code
Terraform, Pulumi and CloudFormation describe infrastructure in files, so you can recreate it from version control.
🚚CI/CD pipelines
The pipeline builds the image once, tags it and promotes the same artifact through each stage.
πŸ“±Serverless and apps
Function versions and aliases, and app-store releases, follow the same idea: a numbered artifact you can point back to.
πŸ› οΈGitOps
The desired state lives in Git. Rolling back means reverting a commit, and the system follows.

06Types / Parts / How It Works

Ways to go back

WayHow it worksGood and bad
Redeploy the previous versionStart the old image again (or point the pipeline at the old tag)The simplest and most common. Needs the old artifact to be kept.
Switch traffic backIn blue-green, point the load balancer back to the old environmentAlmost instant. Needs two environments running.
Flip a feature flag offTurn the new code path off without any deployTakes seconds. The code must be behind a flag.
Stop a canarySend 0% of traffic to the new versionLimits the damage to the small share who saw it.
Revert the commit and redeployUndo the change in Git, run the pipeline againSlower, but leaves a clear history.
Roll forwardShip a quick fix as a new versionNeeded when going back is unsafe, for example after a data change.
Restore from backupRecover the data to a point in timeThe last resort for data problems. Some recent data is lost.

Mutable vs immutable

MutableImmutable
How a change is madeEdit the running serverBuild a new image, replace the server
Servers over timeDrift apartStay identical
Roll backRun an undo script, hope it fitsStart the old image
Debuggingβ€œIt works on my server”Same image everywhere, so reproducible
Logging in to fix thingsCommonDiscouraged. Fix the image instead.
Local state on the serverOften kept thereMust live outside (database, object store)

Why the database makes it harder

Code is stateless, data is not
πŸ“¦App v1 ↔ v2swap at will
↔same database
πŸ—„οΈDatabaseonly moves forward
Easy to roll back Hard to roll back
You can swap code versions freely. The database keeps moving forward, and some changes cannot be undone by starting old code.
VisualizerA bad release: roll back servers and dataPress Roll back in each mode. Then choose 'Code + dropped column' and see that going back to the old code does not bring the data back.
How are the servers managed?

Servers are patched in place with scripts. Over time, people also make hand changes, so some servers drift away from the rest.

What did the bad release v2 contain?

Release v2 only changes code. The database is untouched.

Production right now: each box is one server. Red = bad release, green = healthy v1, amber = unknown state, pink = old code but its data is gone.
πŸ–₯️
server 1
v2 (bad)
πŸ–₯️
server 2
v2 (bad)
πŸ–₯️
server 3
v2 (bad)
hand-changed
πŸ–₯️
server 4
v2 (bad)
πŸ–₯️
server 5
v2 (bad)
πŸ–₯️
server 6
v2 (bad)
hand-changed
πŸ–₯️
server 7
v2 (bad)
πŸ–₯️
server 8
v2 (bad)
Healthy servers
0 / 8
running a working version
Users still failing
100%
share of servers not healthy
Roll-back time
-
illustrative
Unknown state
0
servers to inspect
Database change
None
can the code roll back?
Release v2 is bad and every server runs it (8 of 8). Release v2 only changes code. The database is untouched. Press "Roll back" to return to v1.

A simplified model with no random numbers. In the patched model every third server has hand-made changes (drift). All times are illustrative and not from a real system: undoing a patch takes half the start time per server, and each drifted server needs 15 minutes of manual repair. Press Roll back to compare.

07How to Use It / Steps / Implementation

Making rollbacks boring

  1. 1
    Build once, deploy many
    Build the artifact one time, give it a unique version (a tag or a digest), and promote that same artifact to staging and production. Never rebuild for production.
  2. 2
    Keep old versions
    Keep the last several images in the registry. Do not let a clean-up job delete the one you may need tonight.
  3. 3
    Pin exact versions
    Deploy app:1.4.3 or an image digest, not β€œlatest”. With β€œlatest” you cannot say what a roll-back should return to.
  4. 4
    Keep servers stateless
    Store data in a database or object store, not on the server disk. A server must be safe to delete at any time.
  5. 5
    Make database changes backward compatible
    Use expand and contract so the previous app version still works on the new schema. Then a code roll-back is safe.
  6. 6
    Use flags for risky behaviour
    Ship new behaviour dark, turn it on for a few users, and turn it off in seconds if it misbehaves.
  7. 7
    Automate the roll-back
    One command or one button, tested often. Trigger it automatically when health checks or error rates cross a limit.
  8. 8
    Practise it
    Roll back on purpose in a quiet period. A step you have never run is a step that will fail at 3 am.
⚠️Do not fix production by hand
A quick manual change on one server feels fast. But now that server is different from the image, and the change will be lost the next time the server is replaced. If you must act in an emergency, write the fix into the image or the code afterwards.
πŸ’‘Know your rollback time
Measure how long a roll-back really takes, from the decision to healthy traffic. If it is longer than your error budget allows, work on that before the next big release.

08Key Terms to Know

TermMeaning in easy words
RollbackReturning to the previous working version after a bad release
Roll forwardFixing a problem by releasing a new version instead of going back
Immutable infrastructureServers are never changed after they start. They are replaced from a new image
Mutable infrastructureServers are updated in place, one change at a time
Configuration driftServers slowly becoming different from each other because of manual or partial changes
ImageA packaged, versioned copy of an app and its environment (a container or VM image)
ArtifactAny build output you can store and deploy, such as an image
RegistryA store for images, where each version is kept
Image digestA hash that identifies one exact image. It can never point to something else
Infrastructure as codeDescribing servers and networks in files that are kept in version control
Snowflake serverA hand-tuned server that nobody can recreate

09Pros, Cons and Trade-offs

βœ… Pros
  • Roll-backs are fast and predictable
  • All servers from one image are identical
  • No configuration drift
  • Test what you ship
  • Easy to scale and to rebuild after a failure
⚠️ Cons
  • Needs a build pipeline and an image registry
  • Slower for tiny fixes, since you rebuild and replace
  • State must live outside the servers
  • Images need rebuilding to get security patches
  • Does not solve data roll-backs by itself

Trade-offs

ChoiceWe gainWe lose
Replace servers on every changeKnown state, easy undoEach change needs a build and a roll-out
Patch in placeA quick small fixDrift, and a roll-back nobody has tested
Keep many old imagesA choice of versions to go back toStorage cost and a longer clean-up policy
Always roll back firstFast recoveryMay hide a deeper problem and repeat the failure
Roll forwardKeeps the good part of the releaseNeeds a fix to be written quickly under pressure
Keep the old environment running (blue-green)An instant switch backDouble the infrastructure cost for a while

10Common Problems and Mistakes

Common mistakes

  1. Deploying "latest". You cannot know what to go back to.
  2. Deleting old images too early. The roll-back target is gone when you need it.
  3. Hand-editing production servers. The change is invisible, untested and lost on the next replacement.
  4. Dropping data in the same release. The code can go back, but the data cannot.
  5. Local state on the server. Uploads or sessions on the disk vanish when the server is replaced.
  6. An untested roll-back. The script was written once and never run, so it fails when it matters.
  7. Rolling back without understanding why. The same bad release comes back tomorrow.

Things to watch

πŸ—„οΈSchema and data
Make every database change compatible with the previous app version.
πŸ“¨Messages in flight
A new version may have put messages on a queue in a format the old version cannot read.
πŸ”‘Config and secrets
A roll-back of code does not roll back config. Version the configuration too.
🧊Caches
Cached data from the bad version may still be served after you go back.

11Real-World Examples

☸️Kubernetes
A Deployment keeps a history of revisions, and a roll-back returns to an earlier one with a single command.
🐳Container platforms
Services deploy a tagged image. A roll-back is the same deploy with the earlier tag.
☁️Cloud auto-scaling
Instances are started from a machine image and replaced when they fail or when a new image is rolled out.
πŸ’Chaos testing
Some teams terminate servers on purpose, to prove that replacing a server is routine and safe.
🚩Flags as a first step
Many teams try a flag flip before a full roll-back, since it is faster and does not touch servers.
🧯Postmortems
Incident reviews often ask: how long did the roll-back take, and could it be automatic?
πŸ“Note
Commands and features differ between platforms and versions. Check the documentation of the tools you use.
Mini case: a bad release at 14:00
πŸš€14:00release 1.4.3
→↓
🚨14:04error alert fires
→↓
↩️14:06start roll-back to 1.4.2
→↓
βœ…14:12healthy again
Illustrative timeline.

12Related Topics to Read Next

Deployment Strategies Overview→

Where rollbacks fit among all the ways to release.

Blue-Green Deployments→

An instant switch back to the old environment.

Canary Releases→

Limit the damage, and roll back by sending zero traffic.

Feature Flags→

Turn new behaviour off in seconds without a deploy.

Schema Migrations→

Make database changes safe for both old and new code.

CI/CD Pipelines→

Build once, version the artifact, and deploy or roll back with one pipeline.

13Interview Questions with Short Answers

14Quick Revision Cheat-Sheet

  • βœ”Idea: never edit a running server. Build a new image and replace.
  • βœ”Roll back: start the previous image. The old version is a real artifact.
  • βœ”Pin versions: exact tag or digest, never "latest". Keep old images.
  • βœ”Stateless servers: data lives in a database or object store.
  • βœ”Database: keep schema changes backward compatible, destructive ones last.
  • βœ”Fastest undo: flip a flag, stop a canary, switch blue-green traffic.
  • βœ”Roll forward: when going back is unsafe. Practise both.
  • βœ”One line: β€œReplace, don't repair, and always keep the last good version.”
← Previous
Schema Migrations
Next β†’
Multi-Region and Active-Active