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.

Redundancy and Failover

Module 7 Lesson 7.a 50 min Difficulty 3/5

Prerequisites 1.b

The idea in one minute#

Redundancy means having more than one of everything that can fail, arranged so that one failure does not stop the service. Its value depends entirely on independence: copies in the same rack, zone, region, software version or config fail together. So you place copies across failure domains — hosts, racks, availability zones, regions — and size the fleet so the survivors can carry the load (N+1, N+2).

Failover is the act of moving work from a failed copy to a healthy one. It is easy for stateless services (the load balancer stops sending traffic) and hard for stateful ones (databases), where two copies both believing they are the primary — split brain — can corrupt data. Stateful failover needs a quorum to decide who leads, and fencing to stop the old leader.

An analogy#

A plane has two engines so it can fly on one — but only if they do not share a fuel line. If a single blocked pipe starves both, the second engine was decoration. Redundancy is about what the copies do not share.

A picture#

flowchart TB
  LB[":envoyproxy: <b>Regional load balancer</b>"]
  subgraph AZA["Zone A"]
    A1[":go: app"] & A2[":go: app"]
  end
  subgraph AZB["Zone B"]
    B1[":go: app"] & B2[":go: app"]
  end
  subgraph AZC["Zone C"]
    C1[":go: app"] & C2[":go: app"]
  end
  LB --> A1 & A2 & B1 & B2 & C1 & C2
  A1 & B1 & C1 --> DB[(":postgresql: <b>Primary</b><br/><small>zone A</small>")]
  DB -->|"sync replication"| R1[(":postgresql: Standby<br/><small>zone B</small>")]
  DB -->|"async"| R2[(":postgresql: Replica<br/><small>zone C</small>")]
  Q[":etcd: <b>Consensus / quorum</b><br/><small>who is primary?</small>"] -.-> DB & R1 & R2
  class LB io
  class A1,A2,B1,B2,C1,C2 compute
  class DB,R1,R2 memory
  class Q queue

How it really works#

Failure domains#

DomainFails together because ofSpread across it by
Process / hostCrash, kernel panic, disk, NICMultiple replicas; pod anti-affinity
RackTop-of-rack switch, power distributionRack-aware placement
Availability zonePower, cooling, network, flooding in one data centre (or group)Multi-AZ deployments, topologySpreadConstraints
RegionRegion-wide control plane or network eventMulti-region (7.c, 9.b)
Software version / configA bad release or config everywhere at onceStaged rollouts (Module 8)
Provider / dependencyOne cloud, one DNS provider, one CDNMulti-provider for the few things that justify it

The most common correlated failure is not hardware — it is the same bad change applied everywhere. Spreading across zones does not protect you from a config push to all zones.

Sizing: N+1 and N+2#

If you need N instances to carry peak load, run N+1 so one can fail, or N+2 so one can fail while another is down for maintenance or a deploy. Across zones, size so that losing an entire zone still leaves enough:

3 zones, need capacity N at peak  →  each zone must carry N/2  →  total 1.5 N
2 zones                            →  each zone must carry N    →  total 2 N

This is why three zones is the common choice: it costs 50% headroom instead of 100%.

Active-active versus active-passive#

Active-activeActive-passive
HowAll copies serve trafficStandby waits, takes over on failure
FailoverInstant — load balancer stops sending to the dead copySeconds to minutes; must be detected and triggered
CapacityUsed all the time; must still have headroom to absorb a failureStandby capacity idle (or cold)
ConfidenceHigh — every copy is exercised every secondLower — standby can rot unnoticed
Typical forStateless services, read replicasDatabases with one writer, legacy systems

Prefer active-active wherever state allows. A passive standby that is never exercised will fail when needed — test failovers regularly (Module 10).

Health checks decide failover#

Load balancers remove instances that fail health checks. Get them wrong and redundancy fails:

  • Too shallow (process is up) — a broken instance keeps getting traffic.
  • Too deep (checks every dependency) — one dependency blip fails every instance at once, and the load balancer removes them all. Fail open: if most instances look unhealthy, many load balancers (Envoy’s panic threshold, AWS target groups) send traffic to all of them anyway, assuming the health check is wrong.
  • Use outlier detection on real traffic (6.c) in addition to synthetic checks.

Stateful failover, quorum and split brain#

With a single-writer database, failover means promoting a replica. Dangers:

  • Split brain: the old primary is not dead, only partitioned. Both accept writes, and the data diverges.
  • Data loss: with asynchronous replication, the promoted replica may be missing the last writes. Synchronous replication to at least one standby prevents that at the cost of write latency.

Defences:

  • Quorum: a majority of an odd number of voters (3 or 5) must agree on the leader, so at most one side of a partition can have one. Consensus systems (etcd, ZooKeeper, Consul — Raft or Zab) provide this; Patroni uses etcd/Consul to manage PostgreSQL failover; managed databases do it internally.
  • Fencing: the old leader must be stopped from writing — leases that expire, fencing tokens (an increasing number every write must carry), or STONITH (“shoot the other node in the head”, power it off).
  • Quorum math: n voters tolerate ⌊(n − 1)/2⌋ failures. 3 tolerate 1, 5 tolerate 2. Even numbers add cost but no tolerance. Put voters in 3 zones so one zone’s loss keeps a majority.

A worked example#

A service needs 12 instances at peak.

  • Option 1: 2 zones × 12 = 24 instances. Survives a zone loss. 100% overhead.
  • Option 2: 3 zones × 6 = 18 instances. Survives a zone loss (12 left). 50% overhead.
  • Option 3: 3 zones × 4 = 12 instances. Cheapest; a zone loss leaves 8 — 67% of peak. Combined with load shedding (6.d) the service survives but sheds a third of peak traffic.

The team picks option 2. Then the program below asks the harder question: if zones fail independently, how available is the service — and what happens when failures are correlated?

Code#

Go
// redundancy.go — availability of zone-redundant deployments, independent versus correlated failures.
package main

import (
	"fmt"
	"math/rand"
)

func main() {
	r := rand.New(rand.NewSource(3))
	const trials = 2_000_000
	const zoneDown = 0.002   // a zone is unavailable 0.2% of the time (~1.5 h a month)
	const badChange = 0.0005 // a bad change hits ALL zones at once 0.05% of the time

	type design struct {
		name          string
		zones, needed int // zones deployed, zones needed to carry peak
	}
	designs := []design{
		{"1 zone", 1, 1},
		{"2 zones, each sized for 100%", 2, 1},
		{"3 zones, each sized for 50%", 3, 2},
		{"3 zones, each sized for 33%", 3, 3},
	}
	fmt.Printf("%-30s %14s %14s\n", "design", "independent", "+ correlated")
	for _, d := range designs {
		okInd, okCor := 0, 0
		for i := 0; i < trials; i++ {
			up := 0
			for z := 0; z < d.zones; z++ {
				if r.Float64() >= zoneDown {
					up++
				}
			}
			if up >= d.needed {
				okInd++
				if r.Float64() >= badChange {
					okCor++
				}
			}
		}
		fmt.Printf("%-30s %13.4f%% %13.4f%%\n", d.name,
			100*float64(okInd)/trials, 100*float64(okCor)/trials)
	}

	fmt.Println("\nQuorum: failures tolerated by n voters")
	for n := 1; n <= 7; n++ {
		fmt.Printf("  %d voters: majority %d, tolerates %d\n", n, n/2+1, (n-1)/2)
	}
}

The first column shows why redundancy across zones is so effective — when failures are independent, and when the survivors can carry the load. The last design is a warning: three zones each sized for a third of peak is less available than one zone, because it needs all three up at once — three hard dependencies in series (1.b). The second shows the ceiling that correlated failures put on it: no number of zones helps against a bad change applied to all of them at once. That ceiling is lifted by staged rollouts (Module 8), not by more hardware.

Remember this#

  • Redundancy is only as good as the independence of the copies; the most common shared cause is a change.
  • Spread across failure domains; size N+1 or N+2, and for zone loss (3 zones → 1.5× capacity).
  • Prefer active-active; exercise any passive standby regularly.
  • Health checks: not too shallow, not too deep; fail open when most instances look unhealthy.
  • Stateful failover needs quorum (odd numbers, across zones) and fencing; choose sync or async replication knowing the data-loss trade-off.

Try it#

  1. Run redundancy.go. Set badChange to 0. How many nines does each design reach?
  2. Your PostgreSQL cluster has two nodes. Explain why it cannot fail over safely on its own, and what to add.
  3. Check a Kubernetes Deployment you own: does it have topologySpreadConstraints across zones?

Check yourself#

  1. Why is three zones cheaper than two for surviving a zone loss?
  2. What is split brain, and what two mechanisms prevent it?
  3. Why do even numbers of voters add no fault tolerance?
  4. What does a load balancer’s panic threshold protect against?

Sources#

↑↓ navigate↵ openesc close

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