What it does
- Retries only when the operation is marked idempotent
- Full jitter by default
- Circuit breaker with a mandatory minimum call count
- Manual clock for testing without waiting
Measured
- Release, MSVC 19.51. Zero allocations across ten thousand successful calls and one thousand failing ones
- Chain of four policies: 101 to 123 ns. Target of 50 ns not met
The policies are complete and measured. Retry, circuit breaker, bulkhead, rate limiter and their composition, as templates and at run time. One spec target is not reached, and the section "The 50 ns target" says which one, how far off it is and why. It is the first thing to read before counting on this module's performance.
---
What this module is
Five policies that wrap a call that can go wrong:
CBulkhead Bulkhead( 8 ); // at most eight at the same time
CCircuitBreaker Breaker( BreakerPolicy );
CRetry Retry( RetryPolicy );
CRateLimiter Rate( 100 ); // one hundred per second
auto Chain = MakePipeline( Bulkhead, Breaker, Retry );
const EStatus e = Chain.Execute( []() noexcept { return CallTheService(); } );The order matters, and it is this: bulkhead → circuit breaker → retry → timeout → the operation.
- The bulkhead on the outside because what it protects are the caller's threads. Inside the retry, three attempts of a thousand calls would take up slots as if they were three thousand.
- The circuit breaker before the retry because an open breaker has to cut off the whole call, without it being retried three times. There is a test that measures this: with the breaker open, the operation runs zero times, and the breaker counts one call, not three.
- The timeout on the inside because what it limits is each attempt. For the total there is already the retry budget.
---
Three decisions the API does not let you work around
There is no default value that retries
Retrying a read is free. Retrying "transfer a hundred euros" transfers two hundred, and the case in which that happens is the most likely of all: the request arrived, was carried out, and it was the response that got lost. Whoever retries cannot tell that apart from "it did not arrive".
That is why SRetryPolicy::bIdempotent starts at false, and with it at false the operation runs once, even with ten attempts configured. It is a gate, in addition to a warning in the documentation: a warning is read once and forgotten. A field that has to be set to true leaves in the code, forever, the statement that the operation can be retried.
Jitter is mandatory in the default values
A hundred clients call the same service. The service goes down. All hundred fail at the same instant, and wait exactly the same time, because exponential backoff without jitter is a function, and a function gives the same result for everyone. At the end of that time all hundred hit at the same moment.
Backoff without jitter synchronizes the load instead of spreading it. The default values come with full jitter, and there is a test that requires a hundred draws of the same backoff to give different values spread over the whole interval.
The draw is xoshiro256++, one per thread. No rand (global and poor), no std::mt19937 (two and a half kilobytes flushing the cache), and no shared generator, which would put a lock in the path of everyone backing off at the same time, which is exactly when the most callers go through there.
Until 2026-10-06 the draw did not spread long waits. RandomBelow multiplied 32 random bits by the limit in a 64-bit product, and above 2^32 the product overflowed: the jitter of a 10 s wait never went past 4.29 s. The tests used short waits and did not see it. Now, above 2^32, the computation uses the high half of the 64 by 64-bit product. On the same date, linear backoff, decorrelated backoff, the conversion from milliseconds to nanoseconds and the budget computation started to saturate at the largest 64-bit value instead of wrapping around.
Until 2026-10-11 the other conversions still wrapped around. The breaker's open time, slow call threshold and bucket width, and the now + uTimeoutMs deadlines of the bulkhead and the limiter, multiplied by 10^6 without checking. With UINT64_MAX the deadline fell in the past, and an "endless" wait was rejected on the spot. With 2^58 ms the open time became zero. Now all of them go through the same saturating computations (Detail::MsToNs and Detail::SaturatingAdd, in policy.hpp), and a deadline that goes past the 64-bit limit waits forever. CManualClock also saturates, instead of going back in time.
The minimum number of calls is not optional
A failure rate of one hundred percent over one call is one hundred percent, and means nothing. Without uMinimumCalls, the first request of a freshly started process (the one that gets the still-cold connection) opens the breaker for everyone. There is a test that sends five failures in five calls and requires the breaker to stay closed, with the rate reading one hundred percent.
---
The 50 ns target
"Success path target: under 50 ns of total policy overhead for a full pipeline of retry plus circuit breaker plus timeout plus bulkhead."
Not reached. Measured: 101 to 123 ns for a chain of four policies, in Release, MSVC 19.51, x64.
The spec says what to do in this case, "stop and report the measured number, not ship something slower in silence", and this is the report.
The cause, measured and not assumed
The test suite first measures a single relaxed atomic write on its own, which is this machine's unit. The chain of four policies performs six of those writes:
| policy | atomic writes | why |
|---|---|---|
| bulkhead | 2 | entering and leaving the semaphore |
| circuit breaker | 1 to 2 | recording the call in the window |
| retry | 2 | attempt and success |
| rate | 1 | spending the token |
Six times the cost of a lock xadd is the whole budget, and more. There is nothing to tune here: it is the price of the instruction, and the code around it weighs nothing. On a machine where an atomic write costs five nanoseconds (such machines exist), the same six give thirty, and the target is met.
And there is a switch, with the second number next to it
ETHEREAL_RESILIENCE_NO_TELEMETRY removes the counters that only serve to count. The ones a policy needs in order to decide stay: the breaker's window is the information it opens with, not telemetry.
| overhead of the four-policy chain | |
|---|---|
| with telemetry | ~123 ns |
| without telemetry | ~101 ns |
It saves little, and it is honest to say so: most of the atomic writes are functional. Anyone who expected that turning off counters would solve the performance learns that it does not, and that is the useful information.
The switch has its own test suite (resilience.lean), for two reasons: it gives the second number, and it keeps the #ifdef from rotting. A branch that is never compiled stops compiling without anyone noticing.
What is required
The template composition is inlined: in Release the chain costs less than the sum of the policies measured separately (123 ns against 95 to 236 ns depending on the run), which only happens if the compiler sees the whole chain and shares work between the links.
This was once wrong and the test suite caught it: the first version of the composition constructed an SOperation in each link, which erased the type of the next link and turned the chain into four indirect calls. The policies now have a templated Execute.
Why none of these numbers is a gate
Between two consecutive runs of this test suite, on the same machine, the cost of an atomic write measured 4.9 ns and 20.1 ns. A fourfold difference, without a single line having changed: it is the machine being more or less busy.
With that variation, any limit set here is a coin toss: one day it flips, someone clicks retry, it passes, and from then on nobody believes a failure from this file again. So the numbers are printed. What remains required are the properties that do not depend on the clock.
---
What was measured, and is required
| memory allocations, success path | zero, in ten thousand calls through the four-policy chain |
| memory allocations, failure path | zero, in a thousand failing calls |
| rate limiter | 60,000 out of 60,000: a thousand per second for sixty seconds, exact |
| half-open admission | exactly 5, with eight threads and four hundred attempts |
| bulkhead under concurrency | never more than the limit, with eight threads hammering |
The allocation counter fails the test: the spec asks for "enforced by hard-failing test allocator", not one that only prints. The global allocation functions are replaced, the complete set, including the nothrow variants (half of them give alloc-dealloc-mismatch under ASan).
---
How this was verified
tests/resilience.cpp, 59 checks, and the same test suite again without telemetry (58. The missing one is the check of the counter that no longer exists, and the test says that this is what is lost).
The manual clock is what makes this testable. A circuit breaker that opens for thirty seconds is not verified by waiting thirty seconds: it is verified by pushing the clock forward. Without that seam, half of this module's properties would only show up by waiting, and a test that waits is a test that one day fails because the machine is busy. The breaker's whole cycle (closed → open → half-open → closed, and half-open → open when a probe fails) runs in microseconds.
Two defects the test harness itself caught:
- The baseline measured zero. The reference operation only returned
eOk, and the compiler removed the entire loop: it measured 0.0 ns. The difference to the case with policies then included the loop itself, and the overhead appeared inflated by more than a hundred nanoseconds. The operation now has an observable effect on both sides. - Two meters, two baselines. The direct case was measured with one and the chain with another, and the two gave 8.6 ns and 20.1 ns for the same operation. Subtracting one from the other compared things that were not comparable. There is now a single meter.
---
C layer
Header ethereal/c/resilience/resilience.h (source src/c/resilience/resilience.cpp), 38 functions in four groups: ETLC_Breaker* (state, allow, record success and failure, failure rate, manual control, state change callback), ETLC_Retry* (the next delay with backoff and jitter for attempt N, or the execution of a C function with retry and an optional predicate), ETLC_RateLimiter* (token bucket or leaky bucket: try, wait, available) and ETLC_Bulkhead*. Each policy has a ConfigInit with the C++ defaults, and statistics. The breaker and the limiter accept the manual clock.
The path has no callback: the C code asks the breaker, runs and reports the result. There are two kinds of code: the call status (ETLC_RESILIENCE_OK and the negative ones) and the operation outcome (ETLC_RESILIENCE_OUTCOME_*, the EStatus values), which the function passed to retry returns.
Left out is the pipeline (pipeline.hpp), which composes the policies at compile time: in C the caller calls one after the other. pOnRetry is left out too: anyone who wants to see each wait uses ETLC_RetryDelay in their own loop.
Tests resilience.cabi.c11 and resilience.cabi.c17 (tests/resilience_c_abi.c): the closed, open, half-open and closed cycle driven by the manual clock with the callback, rate trigger, exact delays without jitter, retry with predicate, idempotency and budget, token bucket and leaky bucket, full bulkhead and the extra release.
---
What is missing
- The timeout policy. The spec's chain is retry + breaker + timeout + bulkhead, and the timeout is not here as a policy of its own. The reason is the one the spec itself says to document: a timeout cannot interrupt an operation that does not observe cancellation, and a policy that pretended to would be worse than not existing. The deadline this module respects is the retry's
uTotalBudgetMs, which applies only to what the retry has yet to do. What is already running is left out. - Hedging. Launching a second speculative attempt and keeping whichever arrives first requires running both at the same time (a thread or an I/O loop), and this module brings neither, on purpose.
- Per-key bulkheads and limiters (one per destination service), with the table sized at initialization.
- **The
fallbackwith a cached value** and an age limit. - **The
IMetricSinkseam** for the Stats module to consume the telemetry without being linked.