☁️ Cloud Architecture Patterns

← Back to Gallery
πŸ›‘οΈ

Circuit Breaker Pattern

Definition

The Circuit Breaker Pattern prevents an application from repeatedly trying to execute an operation that's likely to fail. It monitors for failures and, when a threshold is reached, trips the circuit breaker to prevent further attempts. After a timeout, it allows test requests to check if the underlying issue is resolved.

When to Use It

  • Protecting against cascading failures in distributed systems
  • Calling remote services that might be unavailable
  • You want to fail fast instead of waiting for timeouts
  • Implementing graceful degradation
  • Providing fallback mechanisms
  • Systems requiring high availability

Pros & Cons

βœ“ Pros

  • Prevents cascading failures
  • Fail fast instead of long timeouts
  • Improves system stability
  • Automatic recovery detection
  • Better resource utilization
  • Enables graceful degradation

βœ— Cons

  • Adds complexity to the system
  • Requires careful threshold tuning
  • May block valid requests temporarily
  • Monitoring overhead
  • State management required
  • Testing edge cases is complex

Architecture Diagram

Circuit Breaker States and Flow
================================

                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
              β”Œβ”€β”€β”€β”€β”€β”€    CLOSED    β”œβ”€β”€β”€β”€β”€β”
              β”‚     β”‚  (Normal)    β”‚     β”‚
              β”‚     β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜     β”‚
              β”‚            β”‚             β”‚
    Success   β”‚            β”‚ Failure     β”‚ Reset
    Count     β”‚            β”‚ Threshold   β”‚ Timer
    Reset     β”‚            β–Ό Exceeded    β”‚
              β”‚     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”‚
              β”‚     β”‚     OPEN     β”‚     β”‚
              β”‚     β”‚  (Blocking)  β”‚     β”‚
              β”‚     β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜     β”‚
              β”‚            β”‚             β”‚
              β”‚            β”‚ Timeout     β”‚
              β”‚            β–Ό             β”‚
              β”‚     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”‚
              └────►│ HALF-OPEN    β”‚β”€β”€β”€β”€β”€β”˜
                    β”‚  (Testing)   β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Request Flow:
─────────────

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚          β”‚         β”‚ Circuit Breaker  β”‚         β”‚ External β”‚
β”‚  Client  │────────►│                  │────────►│ Service  β”‚
β”‚          β”‚ Request β”‚  State: CLOSED   β”‚ Forward β”‚          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              β”‚
                              β”‚ Track Failures
                              β–Ό
                     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                     β”‚  Failure Count  β”‚
                     β”‚  Exceeds Limit? β”‚
                     β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              β”‚ Yes
                              β–Ό
                     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                     β”‚  State: OPEN    β”‚
                     β”‚  Return Error   β”‚
                     β”‚  (Fail Fast)    β”‚
                     β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              β”‚ Wait Timeout
                              β–Ό
                     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                     β”‚ State: HALF-OPENβ”‚
                     β”‚ Allow Test Req  β”‚
                     β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              β”‚
                     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”
                     β”‚                 β”‚
              Successβ”‚                 β”‚Failure
                     β–Ό                 β–Ό
              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
              β”‚  CLOSED  β”‚      β”‚   OPEN   β”‚
              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Configuration:
β€’ Failure Threshold: 5 failures
β€’ Timeout: 30 seconds
β€’ Success Threshold: 2 successes