โ Back to Circuit breakers and retries
โก Circuit breakers and retries: cheat sheet
๐จ The analogyA fuse box: when an appliance keeps failing, the fuse trips so the fault can't spread through the house.
๐ The regular explanationKeeping one failing dependency from taking down everything that calls it. Retry transient failures with backoff and jitter, and use a circuit breaker to fail fast while a dependency is down.
- The toaster asks the socket for power. Nothing. Probably a brief flicker.
- Rather than hammering the button, it waits a moment, then a bit longer each time.
- The third try works. Waiting a little gave the socket time to recover.
- Every appliance runs through the fuse box. Right now everything flows normally.
- The kettle develops a fault. Call after call fails, and the kitchen gets slower and slower waiting on it.
- The fuse trips. Power to the kettle is cut, so the fault can't spread to the rest of the house.
- Now the kitchen hears "no" instantly instead of waiting, and makes do with a backup plan.
- The fuse has been off for a while. Meanwhile, someone fixed the kettle.
- The fuse box lets one careful test through. Just one, in case it's still broken.
- The test works, so the fuse resets and everything runs normally again. Had it failed, the fuse would trip again.
- On a normal day the kettle answers quickly and the cooks get on with their work.
- The kettle gets slow. It still works, just very slowly, so each cook stands waiting and can't do anything else.
- This fuse box was set to watch for slowness. It counts the slow calls, sees too many, and trips. No new cook has to stand and wait.
- After a rest, the fuse box lets one test through.
- The kettle is quick again, so the fuse resets and the cooks get back to work.
๐งฉ Key questions
- The toaster gets no power for a moment. What's the sensible thing to do?
- Wait a moment, then a little longer each time, and give up after a few tries. Often it's just a flicker, but frantic retrying only adds to the trouble.
- The kettle is broken and keeps failing. What does the fuse box do?
- Trips, so anything needing the kettle hears "no" instantly. The rest of the house carries on, and nobody waits on a broken kettle.
- After a while, what does the fuse box do before switching back on?
- Lets one careful test through first. If the test works, everything switches back on; if not, the fuse stays off a while longer.
- Three cooks in a row each try up to three times in all when the kettle is slow. How many requests can the kettle get for one order?
- Twenty-seven. Each cook multiplies the asking: 3 x 3 x 3. That is why only one cook should retry.
- The kettle answers every order, but takes eight seconds each time, and cooks pile up waiting. The fuse box stays on. Why?
- The fuse box was only told to watch for sparks, or has seen too few orders to judge. A fuse box can watch for slowness too, but only if it is set to, and only once it has seen enough orders.