Pidoku

Archive

This learning material is in the archive. It is not currently maintained and has not been updated with the latest information, so tool versions, prices and dates may be out of date.

Cascading and Metastable Failures

Module 6 Lesson 6.e 55 min Difficulty 4/5

Prerequisites 6.b, 6.d

The idea in one minute#

A cascading failure spreads: one component fails, its load or its slowness moves to others, and they fail too. Lose one of four replicas and the other three take 33% more load; if that pushes them past capacity, they fail one after another. Slowness spreads backwards up the call chain the same way (6.a).

A metastable failure is worse: the system stays broken after the trigger has gone. Something — usually retries, sometimes a cold cache or a backlog — keeps the load above capacity, and the overload sustains itself. The service had two stable states, healthy and overloaded, and the trigger knocked it from one to the other. Rolling back the change that started it does not help; you have to remove the sustaining load.

An analogy#

A traffic jam that forms when one car brakes hard on a busy motorway. The car drives on, but the jam stays for hours, because cars keep arriving at the back as fast as they leave the front. The trigger is long gone; the jam sustains itself until the inflow falls.

A picture#

flowchart LR
  T[":i-zap: <b>Trigger</b><br/><small>deploy, cache flush, lost replica, spike</small>"] --> O["Load > capacity"]
  O --> F["Timeouts and errors"]
  F --> R["Retries,<br/>reconnects,<br/>cache misses"]
  R -->|"sustaining<br/>feedback loop"| O
  O --> W["Work wasted on<br/>abandoned requests"]
  W -->|"effective capacity falls"| O
  X["Trigger removed"] -.->|"loop keeps going"| O
  class T warn
  class O,F warn
  class R,W queue
  class X neutral

How it really works#

How failures cascade#

MechanismWhat happens
Load redistributionOne replica dies; its share moves to the rest; they cross capacity; they die
Resource exhaustion spreading upwardsA slow dependency holds callers’ threads, connections and memory (6.a)
Retry amplificationEach layer retries; the bottom sees many times the load (6.b)
Health-check death spiralOverloaded instances fail health checks, are removed, the rest get more load, fail checks
Crash loops under loadRestarted instances get full traffic before they are warm, crash again
Shared dependencyMany services share one database, DNS server or auth service; it falls over; everything does

Protection is the whole of this module: timeouts and deadlines, retry budgets, breakers and bulkheads, load shedding, plus capacity headroom (N+2, 7.d) so losing a replica does not push the rest past capacity, and slow-start so new instances ramp up traffic gradually.

What makes a failure metastable#

Bronson, Aghayev, Charapko and Zhu (HotOS 2021) described metastable failures as systems with a sustaining effect — a feedback loop that keeps them in a bad state:

Sustaining effectExample
RetriesErrors cause retries; retries add load; load causes errors
CacheA cache flush sends every request to the database; the database is too slow to refill the cache before entries expire
Work amplification under loadOverloaded servers waste work on requests that time out (goodput falls as load rises, 6.d)
Connection stormsFailed connections reconnect with TLS handshakes, which cost far more CPU than requests
GC / memoryQueued requests fill memory; GC takes CPU; requests slow; more queue
BacklogsA queue that built up during the trigger takes longer to drain than new work keeps arriving

The characteristic signs: the trigger is fixed or gone, yet the service stays at 100% CPU with near-zero goodput; scaling up slowly or rolling back does nothing; it recovers suddenly when load drops (night-time, a client disabled, traffic blocked).

How to recover#

You must break the loop by reducing load below the point where the system can recover:

  1. Shed load hard at the edge — block a share of traffic or a class of clients, or turn on a “static mode” (6.f).
  2. Stop retries: disable client retries via a flag, or rely on retry budgets having been in place already.
  3. Add capacity much faster than the loop — usually not possible in time.
  4. Restore gradually: let traffic back in steps (10%, 25%, 50%…), warming caches, rather than all at once.

Then prevent it: retry budgets, load shedding before collapse, request coalescing for cache misses (one request refills an entry; the others wait), cache warming before deploys, and stress tests that check the system returns to normal after an overload, not just that it survives one.

Real examples#

  • Retry storm after a brief blip: AWS’s 2021 us-east-1 event was triggered by an automated scaling activity that caused a surge of connection activity on an internal network; retries and reconnections overwhelmed internal networking devices, prolonging the impairment.
  • Cache-dependent databases: many outages follow a cache cluster restart, where the database behind it was only ever sized for the cache-miss rate, not for full load.
  • Facebook’s 2021 outage was a different shape — a configuration change withdrew the BGP routes for Facebook’s DNS — but recovery showed the “restore gradually” lesson: bringing data centres back all at once risked power and cache surges, so traffic was returned in stages.

A worked example#

An API has capacity for 100 requests per second; normal traffic is 90 per second. Clients retry failed requests up to twice. Overload wastes work: at twice the load it can handle, half of its effort goes into requests that will time out anyway.

At t = 30 s a garbage-collection problem in a deploy halves capacity for 10 seconds. At t = 40 s the deploy is rolled back and capacity returns to 100.

  • Without a retry budget: failures during the dip produce retries; offered load rises to over 200/s; effective capacity falls; failures produce more retries. When capacity returns, offered load is still far above it. The service stays broken — metastable.
  • With a 10% retry budget: retries can add at most 9 requests per second. The service is overloaded during the dip, but recovers within seconds of the rollback.
  • Without a budget, but with load shed at the edge for 20 seconds (block 50% of new traffic): the loop is broken, the service recovers, and traffic is restored.

Code#

Go
// metastable.go — a retry feedback loop that keeps a service down after its trigger has gone.
package main

import (
	"fmt"
	"math"
)

const (
	base        = 90.0 // new requests per second
	maxAttempts = 3    // first try + 2 retries
	waste       = 1.0  // how much effective capacity overload destroys
	seconds     = 150
)

func capacity(t int) float64 {
	if t >= 30 && t < 40 {
		return 50 // the trigger: a bad deploy halves capacity for 10 s
	}
	return 100
}

type mode struct {
	name   string
	budget float64 // retries allowed as a fraction of new requests; 0 = unlimited
	shed   func(t int) float64
}

func run(m mode) {
	retries := make([]float64, maxAttempts) // retries[a] = requests arriving on attempt a
	fmt.Printf("\n%s\n  t(s)  offered  capacity  success\n", m.name)
	recoveredAt := -1
	for t := 0; t < seconds; t++ {
		newReq := base * (1 - m.shed(t))
		arrivals := make([]float64, maxAttempts)
		arrivals[0] = newReq
		for a := 1; a < maxAttempts; a++ {
			arrivals[a] = retries[a]
		}
		if m.budget > 0 { // retry budget: scale retries down to fit
			total := 0.0
			for a := 1; a < maxAttempts; a++ {
				total += arrivals[a]
			}
			if limit := m.budget * newReq; total > limit {
				for a := 1; a < maxAttempts; a++ {
					arrivals[a] *= limit / total
				}
			}
		}
		offered := 0.0
		for _, x := range arrivals {
			offered += x
		}
		c := capacity(t)
		over := math.Max(0, offered/c-1)
		eff := c / (1 + waste*over) // overload wastes work on requests that will time out
		success := math.Min(1, eff/offered)

		next := make([]float64, maxAttempts)
		for a := 0; a < maxAttempts-1; a++ {
			next[a+1] = arrivals[a] * (1 - success)
		}
		retries = next

		if t >= 40 && success > 0.99 && recoveredAt < 0 {
			recoveredAt = t
		}
		if t%10 == 0 || t == 31 || t == 41 {
			fmt.Printf("  %4d  %7.0f  %8.0f  %6.0f%%\n", t, offered, c, success*100)
		}
	}
	if recoveredAt >= 0 {
		fmt.Printf("  → recovered at t=%d s (trigger ended at t=40 s)\n", recoveredAt)
	} else {
		fmt.Println("  → never recovered: the trigger is gone but the retry loop sustains the overload")
	}
}

func main() {
	none := func(int) float64 { return 0 }
	run(mode{"retries, no budget", 0, none})
	run(mode{"retries with a 10% retry budget", 0.1, none})
	run(mode{"no budget, but 50% of new traffic shed at the edge from t=60 to t=80", 0, func(t int) float64 {
		if t >= 60 && t < 80 {
			return 0.5
		}
		return 0
	}})
}

Run it and watch the first case: when capacity returns to 100 at t = 40, offered load stays far above it, and success stays low for the rest of the run. Nothing in the system is broken any more — only the loop is. The third case shows the recovery technique: shedding for twenty seconds breaks the loop, and the service stays healthy after the shedding ends.

Remember this#

  • Cascades spread by load redistribution, resource exhaustion, retries, health-check spirals and shared dependencies.
  • Metastable failures persist after the trigger because a sustaining effect — retries, cold caches, wasted work, reconnections — keeps load above capacity.
  • Signs: trigger gone, 100% busy, near-zero goodput, rollback does not help.
  • Recover by breaking the loop — shed hard, stop retries — then restore traffic gradually.
  • Prevent with retry budgets, early load shedding, headroom, slow-start, request coalescing, and overload tests that check the system returns to normal.

Try it#

  1. Run metastable.go. Lower base to 70. Does the first case still get stuck? Find the base load at which the system becomes metastable.
  2. Set waste to 0 (overload wastes no work). What changes?
  3. Add a “cold cache” sustaining effect: after the trigger, each failed request also causes a cache miss that costs 2× the work.

Check yourself#

  1. What distinguishes a metastable failure from an ordinary overload?
  2. Why does rolling back the trigger not fix a metastable failure?
  3. Name three sustaining effects.
  4. Why restore traffic gradually after recovery?

Sources#

↑↓ navigate↵ openesc close

100% drag to pan · scroll or pinch to zoom · double-click to zoom in