nexss/kernel
Total 16 Resilience Patterns

Resilience Patterns

Sixteen production-grade resilience patterns for Go services — retries, circuit breaking, distributed locking, sagas, and more. Typed, composable, zero-allocation hot paths.

Tier 1 · Core
Tier 2 · Targeted
Tier 3 · Specialized
Core Foundations

Tier 1 · Core

9 patterns

Retry

/retry Tier 1

Retry transient failures with a configurable backoff strategy. Three flavors: Retry (transient-only via xerr.IsTransient), RetryIf (custom predicate), RetryAll (any non-nil error — idempotent jobs only).

t=0t→Initialtransient err100msRetry 1transient err200msRetry 2transient err400msRetry 3✓ okmaxRetry=3 → 4 total invocations (1 initial + 3 retries)

Signature

action contract
.Retry(maxRetry int, backoff func(int) time.Duration) *Builder[Req, Res]

Backoff Strategies

Four backoff functions passed to Retry / RetryIf / RetryAll. Constant, Linear, Exponential, and Exponential Jitter (the default for SmartResilience, with ±30% crypto-rand jitter to prevent thundering herd).

0s1s2s3s4s5s123456attemptmax cap (5s)Constant (1s)Linear (0.5s)ExponentialExp + Jitter

Signature

action contract
func ExponentialBackoff(base, max time.Duration) func(attempt int) time.Duration

RateLimit

Token-bucket admission control built on golang.org/x/time/rate. Three flavors: global limiter, per-key LRU-bounded limiter (per-tenant or per-user), and distributed limiter (Redis/etcd via your own implementation).

burst=200capacity+rps refillrequestrequestallowed429TooManyRequestsx/time/rate token bucket — refill at rps, drain at burst

Signature

action contract
.RateLimit(requestsPerSecond float64, burst int) *Builder[Req, Res]

ConcurrencyLimit

Lock-free atomic-counter ceiling. Rejects excess calls with ErrConcurrencyLimit before any retry or circuit-breaker logic runs. Use to protect handlers from being overwhelmed by their own callers.

atomic.Int3247/ 50 limit3 slots freecall #1call #2call #3call #4calls #1, #2, #3 PASS+1 → next(ctx, req)call #4 REJECTEDcount.Add(-1)ErrConcurrencyLimitatomic.Int32 counter — Add(1) → if > limit (50), Add(-1) and fast-reject

Signature

action contract
.ConcurrencyLimit(limit int32) *Builder[Req, Res]

Idempotency

Idempotency-Key contract: claim → execute → complete/release with leased in-progress locks and SHA-256 request-hash validation. Concurrent identical requests collapse into a single execution; reused keys with different payloads return Conflict (not silent replay).

no keypassthroughInProgresslease heldConflicthash mismatchCompletedreplay cachedClaim()Complete()hash mismatchconcurrent callerscaller A → executescaller B → waitscaller C → waitssingleflight + leased store = atomic claim, single execution, safe replay

Signature

action contract
.Idempotent() *Builder[Req, Res]

FanOut

Execute an action concurrently for each item in a slice, preserving input order in the returned slice. Bounded concurrency via semaphore channel. Respects context cancellation for both launch and result collection.

reqs []Reqreq[0]req[1]req[2]req[3]req[4]semaphoremax=3worker 1worker 2worker 3→ waitingresults []order preservedsemaphore channel gates concurrency — result slice indexed by input position

Signature

action contract
func FanOut[Req, Res any](ctx, act *BuiltAction[Req, Res], reqs []Req, maxConcurrency int) []AsyncResult[Res]

Cache (Multi-Layer)

/cache Tier 1

Read-through cache with singleflight stampede protection and automatic L1 back-fill on L2 hits. Layers are checked in order: L1 (fast, local) → L2 (shared, e.g. Redis). On miss, the handler runs once and the result is written to all layers.

requestL1 memory5min TTLL2 redis30min TTLsingleflightcollapse misshandlerexecutemisshitmisshitback-fill L1write to all layers on missRead-through with stampede protection and L1 back-fill

Signature

action contract
.Cache(ttl time.Duration, keyFn func(Req) string, layers ...CacheLayer[Res]) *Builder[Req, Res]

Dedup

/dedup Tier 2

Per-action request deduplication. Concurrent identical requests collapse into a single execution; all waiters share the result. Cancellation of a waiter cancels the in-flight call (contrast with Coalesce).

caller Acaller Bcaller Cin-flightshared callkey=SKU-42handler1 executionshared result → all waitersPer-action singleflight: caller cancel returns ctx.Err() to that waiter only

Signature

action contract
.Dedup(keyFn func(Req) string) *Builder[Req, Res]

Coalesce

Cross-action request coalescing via a shared *Coalescer. Identical to Dedup but: (1) one coalescer can span multiple actions, (2) a canceled waiter exits cleanly while the in-flight call KEEPS RUNNING for other waiters.

Caller 1 (cancels)Caller 2 (waiting)*CoalesceractionName:keyc.pending[fullKey]waiters: atomic.Int32In-flight Executioncontext.WithoutCancel1 handler runshared result → delivered to Caller 2Caller 1 cancels → returns ctx.Err() immediatelyIn-flight handler NEVER aborted — runs to completion for remaining waiters

Signature

action contract
.Coalesce(c *Coalescer, keyFn func(Req) string) *Builder[Req, Res]
Targeted Resilience

Tier 2 · Targeted

3 patterns

SmartResilience

Auto-wired composite: circuit breaker + exponential jitter retry driven entirely by xerr.Kind. Fast-fails 4xx without consuming retries. Use InferredResilient() for zero-config sensible defaults.

requestCircuitBreakerAdaptiveRetryIf!IsPermanenthandlerfailures ≥ 5 → OPENcaller-visible3 retries, ExpJitterper-attemptpredicate = !xerr.IsPermanent(err) → 4xx fast-fail, 5xx/timeout retry

Signature

action contract
.InferredResilient() *Builder[Req, Res]

ExclusiveFenced

Distributed lock with monotonically increasing fence counter. Background goroutine renews the lease at ttl/3; if renewal fails, the action context is canceled immediately to abort in-flight work. The handler can retrieve the lease and pass the fence to downstream storage.

t=0t→Acquirefence=NRenewttl/3Renew2·ttl/3Renew FAILlease lostcancel ctxaborthandler executing with lease.Fence=Nlease retrieved via action.LeaseFromContext(ctx)downstream storage MUST validate fenceUPDATE ledger SET ... WHERE fence >= @lease.FenceLease renewal at ttl/3 · loss → immediate ctx cancellation

Signature

action contract
.ExclusiveFenced(m FencedMutex, ttl time.Duration, keyFn func(Req) string) *Builder[Req, Res]

Saga

/saga Tier 2

Distributed-transaction coordinator with LIFO compensating rollback. Each step has Do + Undo. If a mandatory step fails, all prior Undo functions run in reverse order under an uncanceled context. Optional steps may fail without aborting the saga.

step 1reservestep 2chargestep 3 ✗ship FAILstep 4notifyskippedundo 2refundundo 1releaseRollback context: context.WithoutCancel(ctx)Compensating transactions complete even if caller cancelledUndo failures logged via slog, do NOT stop the chain

Signature

action contract
action.NewSaga[Req, Res](name string) *SagaBuilder[Req, Res]
Specialized Scenarios

Tier 3 · Specialized

4 patterns

Adaptive (Circuit Breaker)

Three-state circuit breaker: Closed → Open (after FailureThreshold consecutive failures) → Half-Open (after ResetTimeout, one probe allowed) → Closed (on success). Dynamic timeout scaling. Never trips on permanent 4xx errors.

Closedall calls passfailures=0okOpenfast-failCircuitBreaker errHalf-Open1 probeothers rejectedfailures ≥ 5ResetTimeoutprobe ok → Closed (failures=0)probe failReleaseProbe() on Canceled Probereturns to Open, prevents permanent half-open lock3-state machine — never trips on 4xx (xerr.IsPermanent)

Signature

action contract
func Adaptive[Req, Res any](name string, cfg AdaptiveConfig) Middleware[Req, Res]

AdaptiveLoadShedding

Priority-tiered load shedding based on live system stats (CPU percentage + goroutine count). Lower-priority traffic is dropped earlier than critical traffic. The server actively sheds load to preserve stability instead of failing slowly.

0%25%50%75%100%Critical Shed (100%)Normal Shed (85%)Low Shed (70%)current CPU: 88%Low: SHED ✗ (88 > 70)Normal: SHED ✗ (88 > 85)Critical: PASS ✓ (88 < 100)

Signature

action contract
func AdaptiveLoadShedding[Req, Res any](stats SystemStats, cfg LoadShedConfig, p Priority) Middleware[Req, Res]

Race

/race Tier 3

Hedged execution: run N variants concurrently, the first success wins, losing attempts are cancelled. If all fail, returns the last error. Useful for hedged requests against read replicas or multi-region endpoints.

replica-1slow / errreplica-2✓ winnercancelledreplica-3cancelledreturn res, nilFirst success wins — losers cancelled via context

Signature

action contract
func Race[Req, Res any](ctx, act *BuiltAction[Req, Res], reqs []Req) (Res, error)

StateMachine

Entity lifecycle transition guard. The middleware runs AFTER the handler — handlers don't need to know the rules. If the handler mutates state to an invalid transition, the middleware rolls back the mutation before returning xerr.Conflict.

pendingpaidcanceledshippedrefundedfailedAllowAllowAllow✗ invalidinvalid → rollback SetState(orig)+ return xerr.Conflict

Signature

action contract
action.NewStateMachine[Req StateEntity, Res any](name, exec) *StateMachineBuilder[Req, Res]
Nexss Kernel · Apache 2.0 · Copyright © 2018–2026 Marcin Polak and Contributors