How to read this chapter
Every backend depends on things it does not control, clients that send too much, services that answer too slowly, providers that go down, and mail servers that decide whether your receipts are spam. Resilience is the set of habits that keep one misbehaving party from taking everyone else down with it.
The first three parts protect Wren from its callers with rate limits, from the five classic algorithms to limits shared across a fleet and limits that are really security controls. Parts IV to VI protect Wren from its dependencies. They cover timeouts and deadlines, retries that help rather than hurt, and the circuit breakers, bulkheads and fallbacks that contain a failing dependency. Parts VII to IX deal with the outside world, receiving and sending webhooks, integrating third-party APIs, and getting email delivered. Part X follows a payment call through every protection.
Each section starts from a Wren situation, explains the mechanism, works the numbers and then shows a figure. Picture this boxes give analogies, notes add detail, Watch out names real mistakes, and Interview lens gives an answer to say aloud. In one breath closes each part.
By the end you should be able to implement a token bucket in Redis, explain why a dependency that takes 30 s instead of 30 ms can take down three services, design a webhook receiver that survives duplicates and replays, and explain why a receipt landed in spam.
Meet Wren
Wren is the illustrative food-ordering service from earlier units. These are the numbers this unit uses. All are assumptions chosen for readable arithmetic, not measurements.
| Setting | Value | What it controls |
|---|---|---|
| API rate limit | 100 requests per user per minute, burst 20 | Rate limiting examples |
| Login limit | 5 attempts per account per minute | Security limits |
| Instances | 20 at peak | Distributed limits |
| Gateway deadline | 300 ms for reads, 3 s for checkout | Timeouts |
| Payment provider | p99 400 ms normally, limit 100 requests per second | Integration |
| Provider timeouts | connect 200 ms, overall 2 s | Timeout values |
| Circuit breaker | opens at 50% failures over the last 20 calls, stays open 30 s | Breakers |
| Payments bulkhead | 20 concurrent calls per instance | Isolation |
| Webhook tolerance | 300 s timestamp window | Replay protection |
| Receipts | 50 per second at peak, 180,000 an hour | Email volume |
The main flow in this unit is POST /orders/124/pay, the checkout payment, and the webhook that later confirms it. Part X follows both.
What is inside
Protect the service from its users, from its dependencies and from the outside world, and keep working when they misbehave.
- 01Rate limitingSections 1 to 3. We limit how much any one caller can ask of the service.
- 02More limiting algorithmsSections 4 to 6. We look at the algorithms that give accurate limits with constant state per key.
- 03Distributed and security limitsSections 7 to 9. We make limits work across a fleet and then use them as security controls.
- 04TimeoutsSections 10 to 12. We put a time limit on every call to something we do not control, and we make the limits agree across a chain of services.
- 05RetriesSections 13 to 15. We decide when to try again and how to keep retries from making a bad situation worse.
- 06Circuit breakers and bulkheadsSections 16 to 18. We stop calling a dependency that is clearly failing, and we wall off its failures so they cannot spread.
- 07WebhooksSections 19 to 21. We receive events that other companies push to us over HTTP, and we send our own to restaurants.
- 08Third-party APIsSections 22 to 24. We integrate services owned by other companies, payment providers, SMS gateways, maps and email providers.
- 09EmailSections 25 to 27. We get Wren's receipts and password resets into inboxes rather than spam folders.
- 10The payment call end to endSections 28 to 30. We follow Wren's checkout payment through every protection in the unit, then watch the payment provider slow down mid-dinner.
- +Interview prepInterview question bank, graded exercises and worked solutions.