Skip to content

Failure injection and chaos experiments

Every lab of this module did the same thing: it caused a failure on purpose and watched what the system did. Chaos engineering does that to a real system. The Principles of Chaos Engineering define it as the discipline of experimenting on a system to build confidence in its ability to withstand turbulent conditions in production.

The word to notice is experiment. The Principles describe its shape.

  1. Define a steady state. Pick measurable outputs of the system, such as throughput, error rate and latency, that say it is behaving normally. They look at what the system does, not at how it does it.
  2. State a hypothesis. The steady state will continue while the failure is in place.
  3. Vary a real-world event. Hardware failures, software failures, traffic spikes, scaling events: choose by how likely and how serious.
  4. Run it where the traffic is. Systems behave differently under production traffic and in other environments, so production gives the real answer.
  5. Automate it so that it can run continuously.
  6. Minimise the blast radius. Some harm to users may be acceptable, but the experiment must be contained.

Netflix’s Chaos Monkey is the best-known tool. It randomly terminates instances in production, to make sure that engineers build services that survive the loss of an instance. The point of such a tool is a habit: if instances die every day, nobody builds a service that needs one to stay alive.

Mental model. An experiment is a bet that the system will hold, placed where you can afford to lose. If it holds, you have learned something small. If it does not, you have learned something large, cheaply.

Two controls make an experiment safe to run.

The blast radius is how much of the system and of its users the experiment can touch. Start small: one instance, one zone, a sliver of the traffic, then widen. The lab you used in the first lesson shows why. Killing one of three instances at 900 requests a second took 71% of requests down over 30 seconds, and the failures reached 100% by 8.25 s. Run that on all of production without a limit and you have caused the outage you were testing for.

A stop condition ends the experiment automatically when the steady state is lost. AWS Fault Injection Service defines it as a threshold, expressed as a CloudWatch alarm, at which the service stops the experiment; the steady state, such as latency, CPU load or retries, is what the alarm is built on. A stopped experiment cannot be resumed. The condition is only as good as the measure behind it, and that brings us to the lab.

Try it: an experiment whose measure is blind

Section titled “Try it: an experiment whose measure is blind”

You are about to cut the network between the primary and its two replicas of the shop for 30 seconds. Replication is asynchronous. Write the hypothesis first: while the link is cut, users notice nothing. Choose the steady state: requests that fail.

Press Network partition.

Network partition

The shop sends its writes to one primary and its reads to two replicas, and replication is asynchronous: the primary acknowledges a write as soon as it has it. You cut the network between the primary and the replicas. Both sides are well. Clients still reach the API, writes still succeed at the primary, and the replicas still answer reads, but nothing reaches them from the primary. The link comes back 30 seconds after the cut. Watch what the clients see, then count what the replicas missed. Then decide what you would have built differently and run the same failure again.

The failure lab needs JavaScript. The healthy system is shown below.

1. Break it

The healthy system is shown first. When you press the button the failure strikes, and the run follows the 60 seconds after it.

What the failure does: The network between the primary and the replicas breaks. Both sides keep running, and neither can reach the other. Clients can still reach the API.

Replica servers0.3 ms0.5 ms0.5 ms250 msClientsHealthy<1%APIBusy75%PrimaryHealthy9%ReplicasHealthy36%ReplicationHealthy45%
Replica servers0.3 ms0.5 ms0.5 ms250 msClientsHealthy<1%APIBusy75%PrimaryHealthy9%ReplicasHealthy36%ReplicationHealthy45%

Each dot stands for many requests. The numbers show the real rate.

A dashed arrow is a call the sender does not wait for: the primary sends each write on and carries on.

No bottleneck: every component has headroom.

0 ms
What happens to the data

Figures from the model at the moment shown. They change as the run plays.

Then, with no one pressing a button: The link comes back after 30 s.

Acknowledged writes the replica has missed, so far
—
Writes the primary told clients succeeded while the link to the replica was cut. A primary that waits for the replica does not acknowledge them.
Reads that could be stale, so far
—
Reads answered by a replica that was more than 1 s behind, or that was cut off from a primary that went on acknowledging writes. Each could return data that has changed since.
Writes refused so far
—
Writes the primary could not take. The client was told that each one failed.
Writes have been failing for
—
The seconds in which more than half of the writes were refused.
Time a write takes at the database
—
From the API to the primary, and to the replica too when the primary waits for it.

Cause and effect

Each step leads to the next. A step that is changing right now is marked.

Metrics
ComponentStateUtilisationOffered (req/s)Served (req/s)Rejected (req/s)p50p95p99
ClientsHealthy<1%90090000 ms0 ms0 ms
APIBusy75%900900014 ms60 ms93 ms
PrimaryHealthy9%18018002.8 ms12 ms18 ms
ReplicasHealthy36%72072005.5 ms24 ms37 ms
ReplicationHealthy45%18018003.2 ms14 ms21 ms
Traffic
900 req/s
Successful
900 req/s
Error rate
0%
Mean latency
28 ms

Illustrative values, not benchmarks of any real product.

Model notes

Assumptions

  • Requests arrive at random, as a Poisson process, at a constant average rate.
  • Each node is one queue, served first-come first-served by several identical slots (instances × concurrency).
  • Service times are random with the stated mean; by default they are exponentially distributed.
  • Each node is analysed separately, with the traffic it receives treated as Poisson.
  • The system starts healthy at 900 requests a second. One request in five writes, 180 writes a second, and four in five read, 720 reads a second. Three API instances can serve 1,200 requests a second, the primary 2,000 writes a second, and the two replicas together 2,000 reads a second.
  • Replication is asynchronous: the primary acknowledges a write as soon as it has it, and sends it to the replicas without waiting for them. Each replica applies the stream at up to 400 writes a second. The diagram shows one replica's apply stage, since the replicas are alike and fall behind together.
  • The link to the replicas takes 250 ms there and back, so a write reaches a replica 125 ms after the primary acknowledged it, and is applied a few milliseconds after that, longer if the replica has a backlog.
  • A failure strikes at one instant. Nothing repairs it except the steps the lab lists, which happen at fixed times: no restart, no operator, no autoscaling.
  • The link is cut at the instant of the failure and comes back 30 seconds after it. The figure is illustrative. A fix can bring it forward.

Simplifications

  • The writes that the replicas missed are counted, not queued. After the link is back the model shows the replicas receiving new writes again, not the earlier ones, so it does not show how long catching up takes. A real primary keeps its log and sends what was missed.
  • Only the link between the primary and the replicas is cut. The clients and the API stay on the primary’s side and can reach both. A partition that separates the clients from the primary, or splits the replicas, or leaves two nodes that each think they are the primary, is not simulated.
  • Every read that a replica answers while the link is cut counts as one that could be stale, and only where the primary keeps acknowledging writes. A read is stale only if the data it asks for changed meanwhile, which the model does not know.
  • Sending a write to a replica and applying it are one stage with one rate here. Real databases report them separately (PostgreSQL has write, flush and replay lag): what a primary loses when it dies is what never reached a replica, while what a read returns depends on what has been applied.
  • A replica applies the stream and answers reads in two separate components, so reads stay fast while it lags. A real replica does both with the same machine.
  • Nothing is retried: a write that fails is refused to the client, and the client does not send it again.
  • Every replica is the same and falls behind by the same amount. A real replica can be slower than its siblings.
  • Above capacity there is no steady state. The model switches to a fluid approximation: the node serves at capacity, its queue grows until it reaches its limit, and further requests are rejected.
  • Percentiles assume an exponential tail. A service-time variability other than exponential changes the mean wait but not the shape of the tail.
  • A node frees its slot as soon as it has done its own work, before waiting on the services it calls. Real thread-per-request servers hold the slot, so a slow dependency exhausts them faster than this model shows.
  • A request calls its dependencies one after another. Parallel fan-out is not modelled.
  • When a caller times out and gives up, the callee still finishes the work, so abandoned requests keep consuming capacity.
  • Retries add load. Their backoff and jitter decide when that load arrives, which only the per-request simulation shows.
  • A caller’s timeout counts the mean time its dependencies take, not their full distribution.
  • Latency percentiles for a whole request are estimated, over every request, including those that are rejected and fail quickly. The mean is exact, and the percentiles come from a gamma distribution that has that mean and the variance of the time spent in each component.

Parameter values are illustrative. They are not benchmarks of any real product.

The hypothesis holds on its own measure. No write is refused, no time is lost to writes failing, and a write still takes 4.5 ms. Clients reach the API, writes succeed at the primary, and the replicas answer reads. A dashboard of errors and latency would stay green.

Now read the measures that were not in the steady state. The primary kept acknowledging writes, 180 a second, and none reached the replicas, so 5,400 acknowledged writes are missing from them. The replicas kept answering reads, 720 a second, and 21,600 of those reads could return data that has already changed. The system chose to stay available and to let the two sides drift apart, which is the situation Brewer’s “Managing Partitions” section is about. He advises detecting the partition, limiting some operations while it lasts, and having a recovery process for what was done on each side. The lab detects nothing and limits nothing, and this is what that looks like.

An experiment that measures only errors would report success here. A better steady state would include the age of the data replicas return, or the writes that the replicas have not received. The lesson of the lab is about the experiment, not about the partition: the hypothesis can be true and the system still be losing something nobody was watching.

Try the fixes, as before applied from the start of the run.

Change Writes the replicas missed Reads that could be stale Writes refused A write takes
None 5,400 21,600 0 4.5 ms
Wait for the replica before acknowledging a write 0 0 5,400 259 ms
Repair the link sooner (10 s) 1,800 7,200 0 4.5 ms

Waiting for the replica removes the drift and turns it into refusals: while the link is cut, every write fails, 180 a second for 30 seconds, and the clients are told. Now the error rate does see the partition. Repairing the link after 10 seconds leaves a third of the damage, and needs something to detect the cut quickly, which can mistake a slow or flapping link for a cut. Neither is free, and the experiment is what showed the price.

Each failure lab is a small experiment with a hypothesis that you might have written down before pressing the button.

Experiment A hypothesis you might hold What the lab found
Kill an API instance Three instances survive losing one 71% of requests fail on average
Add 10x traffic The API copes with a campaign 83% fail, and thirty API instances move the problem to the database
Kill the database The cache keeps the shop up 24% fail, quickly
Slow dependency (not dead) Slow is milder than dead The same 24%, with a worst p99 of 1 s instead of 92 ms
Introduce 500 ms latency A 400 ms timeout is generous 96% fail with nothing broken
Kill the primary (failover) Failover loses nothing About 23 acknowledged writes lost and 30 s of failed writes
Network partition Users notice nothing No request fails, and 5,400 writes are missing from the replicas

Almost every hypothesis fails, which is the usual finding of the first experiment on any system, and the reason to run them on a day you chose.

What happens if the experiment has no way out?

Section titled “What happens if the experiment has no way out?”

Before a real experiment, write down five things: the steady-state measures, the hypothesis, the failure and where it is injected, the blast radius, and the stop condition with who can press it. If you cannot name the stop condition, you are not running an experiment, you are causing an incident. Start with the smallest injection that could show a result, widen it only when the smaller ones hold, and keep what you learn: each finding is a fix to schedule or a number to write into the recovery plan of the last lesson.

  • Chaos engineering is experimenting on a system to build confidence that it withstands turbulent conditions. An experiment has a steady state, a hypothesis, a real-world failure, and a result.
  • Contain it with a small blast radius and a stop condition that ends the experiment when the steady state is lost.
  • In the network partition lab no request fails and a write takes 4.5 ms, so the error rate says the hypothesis holds, while 5,400 acknowledged writes are missing from the replicas and 21,600 reads could be stale.
  • Waiting for the replica turns the drift into 5,400 refused writes and 259 ms per write. Repairing the link after 10 s leaves 1,800 missed writes and 7,200 reads.
  • The measures you choose decide what an experiment can find. Add the ones that would show the loss you fear.
  • Each lab of this module is an experiment, and in six of the seven the first hypothesis fails.

Next: the recap and quiz for the module.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.