Engineering Labs

Resilience patterns, one at a time

Eleven resilience patterns added in sequence to one deliberately naive service call, each on its own branch, each shown failing under injected faults before it is fixed.

Contents

The question#

Resilience patterns are usually described one by one, but in production they interact. A retry multiplies load that a bulkhead has to absorb; a circuit breaker changes what a fallback sees; a deadline decides whether a hedge is worth sending. This lab adds the patterns to the same call, in order, so each can be judged by what it changes in the one before.

The setup#

Two small REST services. Placing an order reserves stock in inventory, and inventory has a fault injector that sets latency, jitter and failure rate while it runs.

client --> orders :8090 --POST /reservations--> inventory :9001
           (upstream)                           (downstream, with a fault injector)

The baseline is naive on purpose: no timeout, one attempt, no memory of failure, no concurrency limit, no fallback. It does two things right that every later pattern depends on: business errors (unknown SKU, out of stock) are kept apart from failures, so only failures are ever retried or counted, and cancellation propagates downstream.

The sequence#

Each branch builds on the previous one.

BranchAdds
01-timeout-patternA deadline on every call to inventory
02-retry-patternRetries with exponential backoff and jitter, made safe by idempotency keys
03-circuit-breakerStops calling inventory for a while once it keeps failing
04-bulk-headCaps the calls in flight at once
05-rate-limiterLimits how often each client may order, with five strategies to compare
06-fallbackAccepts orders as pending while inventory is unavailable, and reserves them later
07-hedged-requestsSends a second copy of a slow call and takes the first answer
08-deadline-propagationOne time budget per order, shared with inventory so it can refuse work nobody will wait for
09-load-sheddingInventory refuses work it has no room for, background work first
10-retry-budgetCaps retries across all calls at about 10% of traffic
11-adaptive-concurrencyThe bulkhead finds its own size (AIMD) and follows inventory’s capacity as it changes

What it shows#

  • Retries need an idempotency key before they are safe. Without one, a retry after a timeout can reserve stock twice.
  • Per-call retries multiply load during an outage; a retry budget across all calls is what keeps a struggling dependency from being hit harder.
  • Fixed limits are guesses. The fixed bulkhead of branch 04 is either too tight on a good day or too loose on a bad one; the adaptive limit of branch 11 tracks the capacity inventory actually has.
  • Shedding belongs at the server. Only inventory knows how much room it has; deadlines passed down from orders let it drop work whose caller has already given up.

Reproduce it#

git clone https://github.com/caelus-engineering-labs/resilience-patterns-in-go.git
cd resilience-patterns-in-go
git switch 03-circuit-breaker
./demo.sh
go test -race ./...

Change the faults while it runs:

curl -X PUT -d '{"fail_rate":0.5}' localhost:9001/admin/faults
curl -X PUT -d '{"latency":"2s","jitter":"1s"}' localhost:9001/admin/faults

Limits#

Two processes on one machine, in-memory stock, and synthetic faults. The lab shows mechanisms and their interactions; the thresholds that suit a real system still have to come from that system’s own latency and failure data.

Repository