When should I use createCircuitBreaker instead of retry? #8
-
|
I understand |
Beta Was this translation helpful? Give feedback.
Answered by
shnwazdeveloper
May 10, 2026
Replies: 1 comment
-
|
Use In this package:
import { createCircuitBreaker } from "@shnwazdeveloper/shnwazdev";
const breaker = createCircuitBreaker(
() => fetch("https://api.example.com/health"),
{
failureThreshold: 3,
recoveryTime: 30_000,
successThreshold: 1,
onStateChange(next, previous) {
console.log({ previous, next });
}
}
);
const response = await breaker.execute();A good rule: use |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
clyxudev
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use
retryfor one operation that may recover after a few attempts. UsecreateCircuitBreakerwhen a dependency is failing repeatedly and you want to stop sending traffic for a recovery window.In this package:
closed: calls go through normally.open: calls fail fast withCircuitBreakerOpenErroruntilrecoveryTimepasses.half-open: the breaker allows test calls; enough successes closes it again.