Preventing Cascading Microservice Outages with Redis Circuit Breakers and Token-Bucket Rate Limiting
Shafikul Islam
Backend & Cloud Infrastructure Engineer · Infrastructure & Cloud Team Lead

How One Slow Dependency Triggers a Cascading Cluster Outage
Imagine your core API calls an external shipping or SMS gateway that normally responds in 120ms. Suddenly, that provider degrades and hangs for 30 seconds before timing out.
Within seconds, every incoming request ties up a worker connection waiting on that 30-second socket. Your database connection pool saturates, memory climbs, Kubernetes liveness probes time out, and your entire API goes dark because of one non-critical downstream service.
1. Implementing a Distributed Three-State Circuit Breaker
Instead of letting each pod discover downstream timeouts independently, we shared circuit state across replicas in Redis using a fast state machine (CLOSED -> OPEN -> HALF-OPEN):
- CLOSED (Normal): Requests flow normally with a strict 1,500ms context deadline. Failures increment a rolling 10-second counter.
- OPEN (Tripped): If the failure rate exceeds 25% across at least 20 requests in 10 seconds, the circuit trips OPEN for 15 seconds. All calls fail fast in 0.2ms (or queue asynchronously to RabbitMQ) without opening a network socket.
- HALF-OPEN (Probe): After 15 seconds, a single probe request is allowed through. If it succeeds, the circuit closes; if it fails, the circuit stays open for another 15 seconds.
func (cb *CircuitBreaker) Execute(ctx context.Context, key string, fn func(context.Context) error) error {
state, err := cb.getState(ctx, key)
if err == nil && state == StateOpen {
return ErrCircuitOpenFastFail
}
callCtx, cancel := context.WithTimeout(ctx, 1500*time.Millisecond)
defer cancel()
err = fn(callCtx)
if err != nil {
cb.recordFailure(ctx, key)
return err
}
cb.recordSuccess(ctx, key)
return nil
}
2. Per-Tenant Noisy-Neighbor Isolation via Token Buckets
In a multi-tenant SaaS, a single customer running an unthrottled bulk import script can starve other tenants. We added an atomic Redis Lua token-bucket rate limiter keyed by tenant_id at the API gateway so noisy tenants receive clean HTTP 429 Retry-After headers while every other tenant experiences flat sub-20ms latency.