Tutorials Design Patterns Mastery

Circuit Breaker & Retry Patterns (Resilience with Polly)

On this page

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."

Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

Design Patterns Mastery
Course syllabus
1. Introduction to Design Patterns
2. Creational Patterns
3. Structural Patterns
4. Behavioral Patterns
5. Modern Enterprise & Cloud Patterns
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details