TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About ยท Labs ยท Cheat sheets ยท Learning paths

โ† 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.
Trying again, politelyStep 3 of 3
๐ŸžToaster๐Ÿ”ŒWall socket
  1. The toaster asks the socket for power. Nothing. Probably a brief flicker.
  2. Rather than hammering the button, it waits a moment, then a bit longer each time.
  3. The third try works. Waiting a little gave the socket time to recover.
The fuse tripsStep 4 of 4
๐ŸณKitchenfallbackโšกFuse boxOPENโœ• โœ•๐Ÿ’ซBroken kettle
  1. Every appliance runs through the fuse box. Right now everything flows normally.
  2. The kettle develops a fault. Call after call fails, and the kitchen gets slower and slower waiting on it.
  3. The fuse trips. Power to the kettle is cut, so the fault can't spread to the rest of the house.
  4. Now the kitchen hears "no" instantly instead of waiting, and makes do with a backup plan.
Testing the fuseStep 3 of 3
๐ŸณKitchenโšกFuse boxclosed๐Ÿซ–Kettle
  1. The fuse has been off for a while. Meanwhile, someone fixed the kettle.
  2. The fuse box lets one careful test through. Just one, in case it's still broken.
  3. The test works, so the fuse resets and everything runs normally again. Had it failed, the fuse would trip again.
A slow dependency, with and without a tripStep 5 of 5
Cooks waiting ยท freedt1key Thread 1 waiting on payments: t10t2key Thread 2 waiting on payments: t21t3key Thread 3 waiting on payments: t323๐ŸณKitchenโšกFuse boxclosed๐Ÿซ–Kettle
  1. On a normal day the kettle answers quickly and the cooks get on with their work.
  2. The kettle gets slow. It still works, just very slowly, so each cook stands waiting and can't do anything else.
  3. 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.
  4. After a rest, the fuse box lets one test through.
  5. 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.