This project shows how to use resilience patterns in .NET applications. It uses the Polly library to show real examples of how these patterns work.
- ResiliencePatterns.WebApi: A test API service with endpoints
- ResiliencePatterns.ConsoleApp: A console app that tests all resilience patterns
Automatically tries again when a request fails, using exponential backoff and jitter to wait between retries.
Stops sending requests to a failing service after multiple failures, and waits before trying again.
Cancels requests that take too long to complete, so they don't block resources forever.
Returns a default response when the service fails, so the user always gets an answer.
Limits how many requests can run at the same time, so one service can't use all resources.
Combines resource limits with automatic retry when requests are rejected or fail.
Quickly returns a fallback response when the circuit breaker is open and the service is down.
A complete resilience stack that uses timeout, then retry, then fallback if everything fails.
Controls how many requests can be sent per time period using a token bucket algorithm.
Sends backup requests in parallel if the first request is slow, and uses the fastest response.
Stores responses in memory and returns cached data for repeated requests to reduce backend load.
Reads the Retry-After header from server responses and waits the specified time before retrying.
cd ResiliencePatterns.WebApi
dotnet runThe API will run at: http://localhost:5018
Endpoints:
GET /unstable- Test retry and circuit breakerGET /slow- Test timeoutGET /alwaysfail- Test fallbackGET /limited- Test bulkheadGET /ratelimit?clientId=test- Test rate limitingGET /chaos- Test different error scenariosGET /health- Health checkGET /stats- StatisticsPOST /reset- Reset counters
cd ResiliencePatterns.ConsoleApp
dotnet runThe console app will test all resilience patterns one by one and show the results.
- .NET 9.0
- Polly 8.6.4 - For resilience patterns
- System.Threading.RateLimiting 9.0.0 - For token bucket rate limiter
- Microsoft.Extensions.Caching.Memory 9.0.0 - For caching
- Try each pattern one by one - Study each pattern and understand how it works
- Change the parameters - Try different retry counts, timeout values, and see what happens
- Combine patterns - Mix different patterns together to create stronger resilience
- Think about real situations - Match each pattern to real-world scenarios
| Situation | Recommended Pattern |
|---|---|
| Temporary errors | Retry |
| Continuous failures | Circuit Breaker |
| Long-running operations | Timeout |
| Non-critical services | Fallback |
| Resource management | Bulkhead |
| API rate limits | Rate Limiting |
| Maximum resilience | Timeout + Retry + Fallback |
| High latency | Hedging |
| Frequently read data | Cache |
| Server Retry-After header | Retry-After Aware |
This project is for learning. Feel free to add your own examples and improve it.
This project is for educational purposes.