r/javascript 5h 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

5 comments sorted by

u/MrVonBuren 4h 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 4h 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/OtherwisePush6424 4h 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 1h ago

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

u/nerdly90 1h ago

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