Circuit Breakers: Knowing When to Stop Trying
Start with the actual electrical thing
You know the breaker box in your house? If something draws too much power, the breaker trips and cuts the power to that part of the house. It doesn't try to push through. It just stops, before something catches fire.
Software has the same idea.
The problem it solves
Say your service calls another service. Maybe a payment API. That other service starts failing.
Without any protection, your service just keeps calling it anyway. Every request still waits, still fails, still takes time to time out. If a hundred requests come in, that's a hundred slow, failed calls. You're not helping the other service get better. You're just piling on while it's already struggling.
A circuit breaker's job is simple: notice something is failing, stop trying for a bit, and let everyone move on faster.
The three states
Think of it as one switch with three positions:
Closed - normal. Requests go through like usual. It's just quietly watching for failures.
Open - too many failures happened. The switch flips. Now it doesn't even try calling the other service anymore. It just says "no" right away, instantly, instead of waiting and failing slowly.
Half-Open - after waiting a bit, it lets through one single test request to check if things are better. Works? Go back to normal. Still broken? Flip back to "no" and wait again.
Watch. Stop. Wait. Test. Go back to normal. That's the whole idea.
A small example in code
import time
class CircuitBreaker:
def __init__(self, max_failures=3, wait_time=10):
self.failures = 0
self.max_failures = max_failures
self.wait_time = wait_time
self.state = "closed"
self.opened_at = None
def call(self, func):
if self.state == "open":
if time.time() - self.opened_at > self.wait_time:
self.state = "half-open"
else:
raise Exception("Not even trying, still cooling down")
try:
result = func()
self.failures = 0
self.state = "closed"
return result
except Exception as e:
self.failures += 1
if self.failures >= self.max_failures:
self.state = "open"
self.opened_at = time.time()
raise e
Using it:
breaker = CircuitBreaker()
breaker.call(lambda: requests.post("https://payments.example.com/charge"))
Fail three times in a row, and it stops even trying for 10 seconds. Then it tests once, and either goes back to normal or waits again.
Why this actually helps
Your own service stays fast, even when something it depends on is down.
The other service gets a break instead of getting hit even more while it's already struggling.
It checks for recovery automatically, no one has to manually turn it back on.
One thing worth remembering
A circuit breaker doesn't fix the real problem. It just stops things from getting worse while the real problem gets fixed, same as your house's breaker doesn't repair a bad wire, it just stops the fire until someone calls an electrician.
Find me on LinkedIn 😄.
About the author
- I'm a backend engineer who spends most days deep in distributed systems, services talking to each other, breaking in interesting ways, and occasionally behaving. I write here about what I actually learn while building and debugging things, not the polished version.
Comments
Join the discussionNo comments yet. Be the first to comment.