Circuit Breaker Pattern
Stop calling failing services temporarily.
Circuit Breaker Pattern is a free C# Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Problem Retry Cannot Solve
If a dependency is truly down, retrying every request just piles on load and slows your own app. The circuit breaker pattern detects sustained failure and stops calling the failing service for a while.
Borrowing From Electronics
Like an electrical breaker that trips to protect a circuit, the pattern "trips open" after too many failures, then automatically tests whether the dependency has recovered.
The Three States
A circuit breaker has three states: Closed (calls flow normally), Open (calls are blocked immediately), and Half-Open (a trial call tests recovery).
Closed State
In Closed state, requests pass through and Polly tracks the failure rate. If failures exceed the threshold within the sampling window, the breaker transitions to Open.
Open State
In Open state, Polly short-circuits: it throws a BrokenCircuitException immediately without calling the dependency, giving it room to recover and failing fast for callers.
try
{
await pipeline.ExecuteAsync(token => CallServiceAsync(token));
}
catch (BrokenCircuitException)
{
// circuit is open, fail fast
}Half-Open State
After the break duration elapses, the breaker enters Half-Open and allows a trial request. Success closes the circuit; failure reopens it for another break period.
Configuring The Breaker
Polly v8 uses CircuitBreakerStrategyOptions. Key settings: FailureRatio (e.g. 0.5 = 50%), MinimumThroughput, SamplingDuration and BreakDuration.
using Polly.CircuitBreaker;
var pipeline = new ResiliencePipelineBuilder()
.AddCircuitBreaker(new CircuitBreakerStrategyOptions
{
FailureRatio = 0.5,
MinimumThroughput = 10,
SamplingDuration = TimeSpan.FromSeconds(30),
BreakDuration = TimeSpan.FromSeconds(15)
})
.Build();FailureRatio And MinimumThroughput
FailureRatio is the proportion of failures that trips the breaker. MinimumThroughput ensures the breaker only trips after enough calls, so one early failure does not open it prematurely.
Reacting To State Changes
Use OnOpened, OnClosed and OnHalfOpened callbacks to log transitions or raise alerts when a dependency degrades.
new CircuitBreakerStrategyOptions
{
FailureRatio = 0.5,
OnOpened = args =>
{
Console.WriteLine($"Circuit opened for {args.BreakDuration}");
return default;
}
};Combining With Retry
Retry and circuit breaker complement each other: retry handles brief blips, the breaker stops retrying once failure is sustained. Order matters, which we revisit in the pipelines lesson.
Manual Control
You can hold a CircuitBreakerManualControl to isolate (force open) or close the breaker manually, useful for maintenance windows.
var manualControl = new CircuitBreakerManualControl();
await manualControl.IsolateAsync(); // force open
await manualControl.CloseAsync(); // force closedQuick Check
Test the circuit breaker pattern.
Recap
The circuit breaker protects struggling dependencies by moving between Closed, Open and Half-Open states. In Polly v8 AddCircuitBreaker with FailureRatio, MinimumThroughput, SamplingDuration and BreakDuration controls tripping. Open state throws BrokenCircuitException to fail fast, and callbacks plus manual control let you observe and override state.
Frequently asked questions
Is the “Circuit Breaker Pattern” lesson free?
Yes — the full text of “Circuit Breaker Pattern” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “Circuit Breaker Pattern”?
Stop calling failing services temporarily. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Circuit Breaker Pattern” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Retry Policies
- Circuit Breaker Pattern
- Timeout and Fallback Policies
- Resilience Pipelines