Algorithms · Safety nets
Chaos engineering
Why Netflix breaks its own servers on purpose. If failure is going to happen anyway, make it happen on a Tuesday afternoon while everyone is watching.
The problem
Around 2010–2011, Netflix was moving to the cloud. There, a server (an instance) is not a precious machine in your basement. It can simply vanish, at any moment, with no warning.
Picture millions of people pressing play while some of those machines quietly disappear. If one missing machine can break the show, you will get paged on a Friday night.
The obvious way
Build one good server, hope it never dies, and promise to fix it fast when it does. The trouble: you only learn whether your system survives a failure during a real failure, at the worst time. Code that handles failure but has never been tested is just a guess.
The clever idea
Think of a fire drill. You don't wait for a real fire to find out the exit is blocked, you pull the alarm on a Tuesday morning when everyone is awake. Netflix built Chaos Monkey, which randomly shuts down server instances in production, during business hours, when engineers are around to react.
The deal changes. Since any server can die any minute, engineers must build services that survive it, using redundancy (several copies), retries, and fallbacks. The practice is now called chaos engineering.
How the code does it
Both versions face the same attack: the monkey kills a random server about 5% of the time and keeps it down for 30 requests. With one server, that kill is a long outage. A fresh kill lands every ~20 requests, so the server is down much of the time.
In version B, the retry loop acts as a simple load balancer. It tries replicas in random order and settles for the first live one. A request only fails if all three are down at once, which is rare. Compare the two runs in the bars, then scan the grids: A fills with X runs, while B's X marks are scarce.
Trade-offs
Redundancy costs money (three servers instead of one) and complexity (retries, health checks, data that must stay in sync). Chaos testing is risky if you have no safety net yet. Don't unleash a monkey on a system with a single server and no fallback. Build the safety first, then test it, and start small.
Where else you’ll see it
Netflix described this in its 2011 post "The Netflix Simian Army" and open-sourced Chaos Monkey in 2012. You can read the real thing at github.com/Netflix/chaosmonkey. Today, retries, replicas, and failure drills show up in databases, payment systems, and any app that can't afford to go down.
In version B, when does a request finally fail?
- When any one replica is down
- When all three replicas are down at the same moment
- When the monkey strikes even once
Show the answer
When all three replicas are down at the same moment. The retry loop keeps trying other replicas, so only a moment when every replica is dead produces an X.
- Change simulate(3 to simulate(1
- Raise KILL_CHANCE to 0.2
- Set REPAIR_TIME to 5
- Try random.seed(1)
The code
Python, with what each line means beside it. It compares the clever way with hoping nothing ever fails.
import randomBring in the random toolsrandom.seed(7)Fix the randomness so runs repeat REQUESTS, KILL_CHANCE, REPAIR_TIME = 1000, 0.05, 30Set request count, kill chance, repair time def simulate(replicas, retry):Define a simulation of a service down = [0] * replicas # ticks left until each server is backTrack how long each server stays dead wins, log = 0, []Start with zero wins and an empty log for _ in range(REQUESTS):Handle requests one at a time down = [max(0, t - 1) for t in down]Every repair timer ticks down by one if random.random() < KILL_CHANCE:Sometimes the monkey strikes down[random.randrange(replicas)] = REPAIR_TIMEIt kills one random server for a while tries = random.sample(range(replicas), replicas if retry else 1)Choose which servers to try, in random order served = "X"Assume failure until a server answers for r in tries:Go through the servers to try if down[r] == 0:If this server is alive served = rRemember which one answered breakStop looking, we're done if served != "X":If somebody answered wins += 1Count a success log.append(served)Record who handled this request return wins, logReport wins and the log a_wins, a_log = simulate(1, retry=False)Run A: one server, no retryb_wins, b_log = simulate(3, retry=True)Run B: three servers with retry show([a_wins, b_wins], "successes out of 1000: A (one server) vs B (3 replicas + retry)")Draw both success counts as barsshow([a_log[i:i+10] for i in range(0, 40, 10)], "A: first 40 requests (X = failed)")Show A's first 40 requests as a gridshow([b_log[i:i+10] for i in range(0, 40, 10)], "B: first 40 requests (number = replica)")Show B's first 40 requests as a gridprint("A:", a_wins, " B:", b_wins)Print both totals- same storm line 2Seeding makes the random numbers repeat, so A and B face a fair, repeatable test. Try other seeds.
- the monkey lines 11–12Each request has a small chance of killing a random server for 30 ticks. That is chaos, in miniature.
- the retry lines 13–18With retry, we try every replica in random order until one is alive. Without it, we try just one.
- same code, two designs lines 24–25The only differences are replica count and the retry flag. Redundancy needs both to pay off.
Read more
- netflixtechblog.com: Chaos engineering
- The real code: Netflix/chaosmonkey
- Ask Bento: What is a health check?
- Ask Bento: How does exponential backoff work?
- Ask Bento: What else does chaos engineering test?
Run it. Then change it.
The code runs right in the page and draws as it goes. Free, nothing to install.