Ordine dei decoratori di resilienza
Scopra perché l’ordine in cui si combinano i pattern di resilienza (retry, circuit breaker, bulkhead, timeout, rate limiter) ne modifica il comportamento e come scegliere l’ordine corretto.
Ordine dei decoratori di resilienza è una lezione Microservices Communication Patterns (Saga, Circuit Breaker) gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Microservices Communication Patterns (Saga, Circuit Breaker), e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Microservices Communication Patterns (Saga, Circuit Breaker) include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
Stacking Patterns
Resilience libraries let you wrap a call in multiple patterns at once: retry, circuit breaker, bulkhead, time limiter, rate limiter. The patterns form a chain, and order matters.
The same set of patterns produces different behavior depending on how they are nested.
Decorators as Layers
Think of each pattern as a layer wrapping the next. The outermost layer sees the call first; the innermost actually invokes the dependency.
layers = ['Retry', 'CircuitBreaker', 'Bulkhead', 'TimeLimiter', 'ServiceCall']
for i, l in enumerate(layers):
print(' ' * i + l)Retry Outside the Breaker
The recommended Resilience4j order puts Retry outside the Circuit Breaker.
This means each retry attempt is itself evaluated by the breaker. After enough failed attempts, the breaker opens and stops further retries entirely.
What If Retry Were Inside?
If Retry were inside the breaker, the breaker would only see one outcome per full retry cycle. A burst of internal retries could hammer a failing service before the breaker ever notices.
That is why retry usually wraps the breaker, not the other way around.
Bulkhead Placement
Place the Bulkhead inside the breaker so it limits concurrency on the actual calls. The breaker can then short-circuit before a thread is even acquired, keeping the bulkhead from filling with doomed calls.
breaker_open = True
def call(breaker_open):
if breaker_open:
return 'short-circuited; no bulkhead slot used'
return 'acquire bulkhead slot then call'
print(call(breaker_open))Time Limiter Placement
The TimeLimiter sits close to the call so each individual attempt is bounded. A slow call times out, counts as a failure for the breaker, and can trigger a retry from the outer layer.
Rate Limiter Placement
Put the RateLimiter on the outside if you want to cap total request rate including retries, or inside if you only want to cap actual dependency calls. Decide based on what you are protecting.
Recommended Default Order
A widely used default, from outermost to innermost:
- Retry
- Circuit Breaker
- Rate Limiter
- Time Limiter
- Bulkhead
Adjust to your goals, but understand each move's effect.
Fallback Goes Outermost
The fallback should wrap everything so it can catch failures from any inner layer, including a retry that exhausted attempts or a breaker that is open.
def with_fallback(inner):
try:
return inner()
except Exception:
return 'fallback value'
print(with_fallback(lambda: (_ for _ in ()).throw(Exception('all layers failed'))))Interactions to Watch
Watch for surprising combos:
- Retry plus a rate limiter can amplify load if not bounded.
- A short time limiter inside aggressive retry can storm a slow service.
- Bulkhead rejections may count as breaker failures.
Validate by Testing
Because ordering effects are subtle, validate your chain with integration tests that inject failures and slowness, then assert the observed behavior matches your intent.
Quick Check
Why is Retry typically placed OUTSIDE the Circuit Breaker rather than inside it?
Recap
You learned why decorator order matters:
- Patterns nest as layers; outermost runs first.
- Retry outside the breaker lets the breaker stop retry storms.
- Bulkhead and time limiter sit close to the call.
- Fallback wraps everything.
Choose order by intent, then verify it with failure-injection tests.
Impara Microservices Communication Patterns (Saga, Circuit Breaker) con un tutor IA — gratis
Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.
- Corsi
- 12
- Lezioni
- 48
Domande Frequenti
La lezione «Ordine dei decoratori di resilienza» è gratuita?
Sì — il testo completo di «Ordine dei decoratori di resilienza» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Microservices Communication Patterns (Saga, Circuit Breaker), passa a CoddyKit PRO. Il corso Microservices Communication Patterns (Saga, Circuit Breaker) include 4 lezioni in totale.
Cosa imparerò in «Ordine dei decoratori di resilienza»?
Scopra perché l’ordine in cui si combinano i pattern di resilienza (retry, circuit breaker, bulkhead, timeout, rate limiter) ne modifica il comportamento e come scegliere l’ordine corretto. Eserciti Microservices Communication Patterns (Saga, Circuit Breaker) con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Microservices Communication Patterns (Saga, Circuit Breaker)?
Non è richiesta alcuna esperienza precedente. Microservices Communication Patterns (Saga, Circuit Breaker) su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Ordine dei decoratori di resilienza»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Microservices Communication Patterns (Saga, Circuit Breaker)?
Sì. Ogni lezione Microservices Communication Patterns (Saga, Circuit Breaker) include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Circuit breaker e bulkhead
- Circuit breaker con logica di retry
- Integrazione della limitazione della frequenza
- Ordine dei decoratori di resilienza