Fail fast under known failure
Do not keep exhausting threads on a dependency that is already unhealthy.
When a downstream service is timing out or failing consistently, blindly sending more traffic often makes the whole platform worse. The Circuit Breaker pattern detects unhealthy dependency behavior and temporarily fails fast, giving the caller a chance to degrade gracefully, retry later, or route work differently.
If shipment estimates are slow or unavailable, checkout should not let every request thread pile up waiting on the same broken dependency. A circuit breaker can preserve responsiveness and protect upstream capacity while the downstream recovers.
Circuit breakers usually track failure rates or timeout counts and move between closed, open, and half-open states. Closed means normal traffic flows. Open means requests fail fast for a time window. Half-open allows limited probes to test recovery. This is fundamentally a capacity-protection pattern, not a business consistency pattern.
The circuit breaker helps the platform survive dependency failure. It does not decide whether a fallback result is business-acceptable. That decision belongs to the application and domain context.
Do not keep exhausting threads on a dependency that is already unhealthy.
The caller remains more available by limiting pointless waiting.
Returning stale shipping estimates may be acceptable; faking payment success is not.
| State | Meaning |
|---|---|
| Closed | Normal operation; failures are still below the threshold |
| Open | Dependency is considered unhealthy; calls fail fast temporarily |
| Half-open | Limited requests test whether the dependency has recovered |
package org.javaomnibus.checkout;
public final class ShippingEstimateService {
private final CircuitBreaker circuitBreaker;
private final ShippingClient shippingClient;
public ShippingEstimateView estimate(DeliveryAddress address) {
return circuitBreaker.execute(
() -> shippingClient.estimate(address),
() -> ShippingEstimateView.unavailable("Estimate temporarily unavailable")
);
}
}