🏗️ Scenario
Let’s say we have a microservice called OrderService that needs to call another service — PaymentService — through an HTTP API.
We want to make sure our OrderService:
-
Retries if the call fails (Retry Pattern)
-
Stops repeated failing calls (Circuit Breaker Pattern)
-
Times out long requests (Timeout Pattern)
-
Returns fallback data if all else fails (Fallback Pattern)
🧰 Step 1: Install Required Package
Run this command in your OrderService project:
⚙️ Step 2: Configure Resilience Policies in Program.cs
💡 Step 3: Use HttpClient in Controller
🔍 Step 4: Add Health Check for Kubernetes or Azure
Add in Program.cs:
✅ This allows Kubernetes or Azure App Service to detect and restart unhealthy instances automatically.
📊 Step 5: Add Observability (Optional)
Integrate Application Insights, Serilog, or ELK Stack to monitor:
-
Retry attempts
-
Circuit breaker states
-
Timeout events
This helps detect performance or availability issues early.
🧠 Output Behavior Summary
| Failure Type | Pattern Triggered | System Reaction |
|---|---|---|
| Temporary network error | Retry | Automatically retries |
| Persistent downstream failure | Circuit Breaker | Blocks further calls temporarily |
| Long response | Timeout | Cancels request after 3 seconds |
| Service completely down | Fallback | Returns cached/default message |
| Pod unhealthy | Health Check | Auto restarted by Kubernetes |
🚀 Result
With these configurations:
-
Your microservices become self-healing.
-
You prevent cascading failures.
-
The system delivers consistent performance and high availability.
✅ Pro Tip: Combine Polly with
Serilogfor logging andOpenTelemetryfor tracing to create a fully observable resilient microservice system.