Skip to content

Configure a Circuit Breaker

The circuit breaker is an opt-in feature that detects when the GLIDE core is unhealthy (sustained error rate) and rejects requests at the FFI boundary before threads stall. This prevents thread explosion under degraded conditions by failing fast instead of queueing requests indefinitely.

The circuit breaker option is defined on the base client configuration, so it applies to both standalone and cluster clients. The examples below use the standalone client; pass the same configuration to the cluster client configuration to enable it for a cluster client.

from glide import GlideClientConfiguration, ClientCircuitBreakerConfiguration, NodeAddress
circuit_breaker = ClientCircuitBreakerConfiguration(
window_size_ms=10000,
failure_rate_threshold=0.5,
min_errors=50,
open_timeout_ms=5000,
count_timeouts=False,
consecutive_successes=3,
)
config = GlideClientConfiguration(
[NodeAddress("localhost", 6379)],
client_circuit_breaker=circuit_breaker,
)

The circuit breaker uses a state machine with three states:

  1. Closed (normal operation) — All requests pass through. The breaker monitors the error rate within a sliding window. If the error rate exceeds the threshold (and the minimum error count has been reached), the breaker trips to Open.

  2. Open (rejecting) — All requests are immediately rejected with a CircuitBreakerException/CircuitBreakerError. After the open timeout elapses, the breaker transitions to HalfOpen.

  3. HalfOpen (probing recovery) — All traffic is allowed through optimistically. If the configured number of consecutive successes is reached, the breaker closes. If a failure occurs, the breaker returns to Open.

Closed ──(error rate exceeds threshold)──► Open
▲ │
│ (timeout)
│ ▼
└──(N consecutive successes)────────── HalfOpen

Only transport-level failures count toward tripping the breaker:

  • Connection failures (e.g. connection refused) and dropped connections
  • Failures sending or receiving on the connection (pipeline send/receive failures)
  • Request timeouts — but only when countTimeouts is enabled

Application and server errors do not count, because the request reached the server and got a valid response. For example, WRONGTYPE, MOVED, and similar command-level errors leave the breaker closed.

When the circuit breaker is open, requests throw immediately. Catch these exceptions to implement fallback logic or surface appropriate errors to callers.

from glide import CircuitBreakerError
try:
value = await client.get("key")
except CircuitBreakerError:
# Circuit breaker is open — use fallback or back off
pass

All parameters are optional. The circuit breaker uses sensible defaults if you provide no configuration beyond enabling it.

ParameterTypeDefaultDescription
windowSizeMs / window_size_msinteger10000 (10s)Sliding window duration for error rate calculation.
failureRateThreshold / failure_rate_thresholdfloat0.5 (50%)Error rate (0.0–1.0) that trips the breaker.
minErrors / min_errorsinteger50Minimum errors within the window before the rate is evaluated. Prevents tripping on low traffic.
openTimeoutMs / open_timeout_msinteger5000 (5s)Time the breaker stays Open before transitioning to HalfOpen.
countTimeouts / count_timeoutsbooleanfalseWhether request timeouts count as failures toward tripping the breaker.
consecutiveSuccesses / consecutive_successesinteger3Consecutive successful requests in HalfOpen needed to close the breaker.

When to enable:

  • Applications where thread exhaustion or resource starvation is a concern during server outages.
  • Services that can surface a degraded response (cache miss fallback, stale data) rather than hanging.
  • High-throughput workloads where queued requests would pile up faster than they drain.

Tuning guidance:

  • Start with defaults and adjust based on observed behavior.
  • Lower failureRateThreshold if you want faster detection of degraded backends.
  • Increase minErrors in low-traffic environments to avoid false trips from small sample sizes.
  • Increase openTimeoutMs if your server typically takes longer to recover (e.g., failover scenarios).
  • Set countTimeouts: true if your timeout configuration is tight and timeouts reliably indicate a backend issue.
  • Keep consecutiveSuccesses low (2–5) for faster recovery; increase it if premature recovery causes cascading failures.