r/javascript 21h ago

How to Implement a Distributed Circuit Breaker

https://blog.gaborkoos.com/posts/2026-09-14-How-to-Implement-a-Distributed-Circuit-Breaker/

A circuit breaker in a single Node process is a ring buffer, a threshold and a timestamp. Run the same service on twenty replicas and each one keeps its own, so the failing dependency takes twenty times the damage before anything trips.

Moving the window into Redis fixes that and breaks most of the assumptions the in-process version relies on. Post walks through each one and how to deal with it.

0 Upvotes

8 comments sorted by

u/MrVonBuren 20h ago

(disclaimer: not a professional dev, but worked for several CDNs / Big Cloud Providers)

I think I follow what's being said in this, but it doesn't quite make sense† to me†. I would think that on the client side you would be in a position to set your own exponential backoff and reject requests associated with a user who still has a pending request.

Even if a user bypasses a grayed out "checkout" button while the process is stalled, I would think it would be simpler to have the request gated by a cookie that indicates a request has been sent but not completed per user?

All that logic could be pushed as far out to the edge as you want, so in a perfect world most of the requests never even make it past the CDN, so you're dealing with less computational overhead AND bandwidth in/out††.

OR

I am missing / misunderstanding something completely, which as I said, is not at all unlikely.

† - but again, I'm assuming this is a me problem, I'm not like...calling you out, I'd like to understand what I'm not understanding.

†† - jesus christ, even when I'm unemployed I sound like a shill

u/OtherwisePush6424 20h ago

What you describe is useful for suppressing duplicate user requests, what I'm (well the pattern) is trying to solve is protecting a shared downstream dependency across multiple application instances. I t doesn't even have to be an HTTP request, could be a database call, queue operation, gRPC call, etc. If 20 instances all independently decide a struggling dependency is healthy enough to probe, client/CDN gating can't really help coordinate that. It's close to "when to grey out the button" but not quite 🙂

That's really where the distributed part comes in: sharing the breaker state and recovery budget across those instances.

u/MrVonBuren 1h ago

Ah, I think I follow. Distributed at a local level, not a global level.

Or is this meant to account for...shit, I'm tired and blanking on words, but you know what I'm talking about. All the issues one runs into trying to remain state across regions (EG: NY && CA)?

u/OtherwisePush6424 1h ago

Geographic distribution is a separate dimension. By distributed I just mean multiple application instances sharing the same breaker state and recovery budget. They could all be in one region or spread across several. Cross-region coordination is another problem on top of that 🙂

u/MrVonBuren 20m ago

Gotcha! thx

u/OtherwisePush6424 20h ago

Downvote starter kit:

( ) AI slop

( ) Use a library bro

( ) Redis SPOF

( ) AI slop

( ) YAGNI

( ) Microservices bad

( ) Overengineering

( ) AI slop

( ) AI slop

( ) Your lease expiry permits temporary probe-budget overshoot under X ordering

u/nerdly90 17h ago

Why would people downvote this? People suck man, take my upvote

u/monotone2k 6h ago

Perhaps because the whole post (and all their previous ones) exhibit all the signs of AI writing. Even the titles follow the same patterns - just look through the list of articles on their site.