Skip to content
tezvyn:

Immutable Infrastructure: Treat Servers Like Cattle, Not Pets

Source: docs.aws.amazon.comHardHow cards are made

Immutable infrastructure means you never modify running servers. To deploy, you replace them with new ones built from a golden image. This is key for CI/CD and autoscaling, ensuring consistency. The footgun is configuration drift from manual, one-off fixes.

Why it exists

To solve the problem of 'configuration drift.' In traditional, mutable infrastructure, servers are updated and modified in place over their lifetime. These manual changes accumulate, making each server slightly different and impossible to reproduce reliably. This leads to deployment failures, security holes, and 'it works on my machine' bugs.

The mental model

Treat your servers like cattle, not pets. A pet server is unique, hand-raised, and nursed back to health when it's sick. A server from the 'cattle' herd is identical to others and replaced if it fails. In practice, this means you never SSH into a production instance to apply a patch. Instead, you build a new, patched server image, deploy new instances from it, and terminate the old ones.

How it works

The process starts by defining a server's entire configuration as code using tools like Packer or Docker. This code builds a 'golden image'—a template like an AWS AMI or a container image. When a change is needed (like an application update or OS patch), a CI/CD pipeline automatically builds a new golden image. Deployment consists of launching new servers from this new image and decommissioning the old ones, often orchestrated with tools like Kubernetes or cloud provider auto-scaling groups.

When to use it

This pattern is ideal for stateless applications where instances can be terminated and replaced without data loss. Think web servers, API gateways, or containerized microservices. It dramatically improves deployment reliability, simplifies rollbacks (just deploy the previous image), and makes scaling predictable and safe.

When not to use it

Immutable infrastructure is difficult to apply naively to stateful services like databases. You cannot simply terminate a primary database server without a careful data replication and failover strategy. While the underlying OS of a database server can be immutable, the data itself is, by definition, mutable and persistent. Applying this pattern without accounting for state can lead to data loss.

One canonical example

A web application on an AWS Auto Scaling Group (ASG). The ASG's Launch Template points to a specific Amazon Machine Image (AMI). To deploy an update, the CI/CD pipeline uses Packer to build a new AMI with the new code. It then creates a new Launch Template version pointing to the new AMI. Finally, it updates the ASG to use the new template and initiates a rolling refresh, which gradually replaces old instances with new ones for a zero-downtime deployment.

Interview question

According to the 'cattle, not pets' mental model for immutable infrastructure, which action is characteristic?

  • a.Building a new server image with all necessary updates and replacing existing instances.Correct
  • b.Applying critical security updates by directly accessing live production servers.
  • c.Manually diagnosing and repairing issues on a specific server to restore its functionality.
  • d.Using a configuration management system to update software on running instances.
Why?

The 'cattle, not pets' model dictates that servers are replaced, not modified. This means building a new image with updates and deploying new instances from it, then decommissioning the old ones. Modifying running instances, even with configuration management, is characteristic of mutable infrastructure.

Just read this? Test yourself on what you have been reading.

Read the original → docs.aws.amazon.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on ci/cd — each one lists the topics its interview covers.

See open roles