Design Patterns Mastery

Circuit Breaker & Retry Patterns (Resilience with Polly)

1 Views Updated 5/4/2026

Resilience Patterns

In a Microservices world, your app will eventually fail because of a network timeout or a remote service crash. Resilience Patterns ensure that your application stays alive even when its dependencies are dying. We primarily use the Polly library in .NET to implement these.

1. Retry Pattern

If a network call fails, don't just crash. Wait 1 second and try again. This fixes "Transient" errors (short blips in the internet).

// Try 3 times, waiting longer each time (Exponential Backoff)
var policy = Policy.Handle<Exception>()
                   .WaitAndRetryAsync(3, i => TimeSpan.FromSeconds(i));

2. Circuit Breaker Pattern

If a remote service is truly DOWN, retrying 1,000 times will just slow down your server and overwhelm the remote service. The Circuit Breaker "Trips" and stops all calls immediately for 30 seconds, allowing the remote service time to recover and giving your users a faster "Service Unavailable" response.

4. Interview Mastery

Q: "What is Exponential Backoff, and why is it better than a simple loop?"

Architect Answer: "Exponential Backoff is the practice of increasing the wait time between retries (e.g., 1s, 2s, 4s, 8s). This is superior to a simple loop because it prevents the 'Thundering Herd' problem. If 1,000 servers all retry a failed database every 1 second, they will essentially DDOS the database and prevent it from ever recovering. By 'Backing off', we give the resource the breathing room it needs to heal."

Design Patterns Mastery
1. Introduction to Design Patterns
Introduction to GoF Patterns: Why patterns matter? SOLID Principles: The foundation of all patterns DRY, KISS, and YAGNI (Architectural Philosophy)
2. Creational Patterns
Singleton Pattern: Thread-safety & Captive Dependencies Factory Method: Abstracting complex object creation Abstract Factory: Creating families of related objects Builder Pattern: Constructing complex fluents Prototype Pattern: Cloning high-cost objects
3. Structural Patterns
Adapter Pattern: Bridging incompatible interfaces Bridge Pattern: Decoupling abstraction from implementation Composite Pattern: Managing tree structures & hierarchies Decorator Pattern: Enhancing behavior without inheritance Facade Pattern: Simplifying complex library subsystems Flyweight Pattern: Drastically reducing memory footprint Proxy Pattern: Interception, Lazy-Loading, and Security
4. Behavioral Patterns
Chain of Responsibility: Middleware & Pipeline Architecture Command Pattern: Implementing Undo/Redo & Queueing Interpreter Pattern: Building domain-specific languages Iterator Pattern: Unified traversal of collections Mediator Pattern: Decoupling components with MediatR Memento Pattern: Capturing and restoring object state Observer Pattern: Pub/Sub & Event-driven architecture State Pattern: Managing complex object lifecycles Strategy Pattern: Swappable algorithms at runtime Template Method: Defining skeleton algorithms Visitor Pattern: Separating operations from data structures
5. Modern Enterprise & Cloud Patterns
Repository & Unit of Work (The EF Core Standard) CQRS Pattern (Command Query Responsibility Segregation) Circuit Breaker & Retry Patterns (Resilience with Polly) Dependency Injection Pattern (The Modern King)