Skip to content

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.

License

Notifications You must be signed in to change notification settings

hgulbicim/Resilience-Patterns

Repository files navigation

Resilience Patterns in .NET

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.

🎯 Project Structure

  • ResiliencePatterns.WebApi: A test API service with endpoints
  • ResiliencePatterns.ConsoleApp: A console app that tests all resilience patterns

📚 Resilience Patterns

1. Retry Pattern

Automatically tries again when a request fails, using exponential backoff and jitter to wait between retries.

2. Circuit Breaker Pattern

Stops sending requests to a failing service after multiple failures, and waits before trying again.

3. Timeout Pattern

Cancels requests that take too long to complete, so they don't block resources forever.

4. Fallback Pattern

Returns a default response when the service fails, so the user always gets an answer.

5. Bulkhead Pattern

Limits how many requests can run at the same time, so one service can't use all resources.

6. Bulkhead + Retry Pattern

Combines resource limits with automatic retry when requests are rejected or fail.

7. Circuit Breaker + Fallback Pattern

Quickly returns a fallback response when the circuit breaker is open and the service is down.

8. Timeout + Retry + Fallback Pattern

A complete resilience stack that uses timeout, then retry, then fallback if everything fails.

9. Rate Limiting Pattern

Controls how many requests can be sent per time period using a token bucket algorithm.

10. Hedging Pattern

Sends backup requests in parallel if the first request is slow, and uses the fastest response.

11. Cache Pattern

Stores responses in memory and returns cached data for repeated requests to reduce backend load.

12. Retry-After Aware Pattern

Reads the Retry-After header from server responses and waits the specified time before retrying.

🚀 How to Run

1. Start the Web API

cd ResiliencePatterns.WebApi
dotnet run

The API will run at: http://localhost:5018

Endpoints:

  • GET /unstable - Test retry and circuit breaker
  • GET /slow - Test timeout
  • GET /alwaysfail - Test fallback
  • GET /limited - Test bulkhead
  • GET /ratelimit?clientId=test - Test rate limiting
  • GET /chaos - Test different error scenarios
  • GET /health - Health check
  • GET /stats - Statistics
  • POST /reset - Reset counters

2. Run the Console App

cd ResiliencePatterns.ConsoleApp
dotnet run

The console app will test all resilience patterns one by one and show the results.

📦 Technologies Used

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

💡 Learning Tips

  1. Try each pattern one by one - Study each pattern and understand how it works
  2. Change the parameters - Try different retry counts, timeout values, and see what happens
  3. Combine patterns - Mix different patterns together to create stronger resilience
  4. Think about real situations - Match each pattern to real-world scenarios

🎓 Pattern Selection Guide

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

📖 Resources

🤝 Contributing

This project is for learning. Feel free to add your own examples and improve it.

📄 License

This project is for educational purposes.

About

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.

Topics

Resources

License

Stars

Watchers

Forks

Languages