Microservices Communication Patterns (Saga, Circuit Breaker) · Lekcja

Kolejność dekoratorów odporności

Poznaj wpływ kolejności nakładania wzorców odporności (retry, circuit breaker, bulkhead, timeout, rate limiter) na działanie systemu oraz dowiedz się, jak wybrać właściwą kolejność.

Lekcja 4 z 413 kroki

Kolejność dekoratorów odporności to bezpłatna lekcja Microservices Communication Patterns (Saga, Circuit Breaker) na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Microservices Communication Patterns (Saga, Circuit Breaker), a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Microservices Communication Patterns (Saga, Circuit Breaker) zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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.

Bezpłatny start

Ucz się Microservices Communication Patterns (Saga, Circuit Breaker) dzięki korepetycjom AI — za darmo

Pisz i uruchamiaj kod w przeglądarce, otrzymuj natychmiastową pomoc od korepetytora AI dostępnego 24/7 i kontynuuj naukę w sieci lub w aplikacji.

Kursy
12
Lekcje
48

Często zadawane pytania

Czy lekcja „Kolejność dekoratorów odporności” jest bezpłatna?

Tak — pełny tekst „Kolejność dekoratorów odporności” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Microservices Communication Patterns (Saga, Circuit Breaker), przejdź na CoddyKit PRO. Kurs Microservices Communication Patterns (Saga, Circuit Breaker) zawiera 4 lekcji w sumie.

Co nauczysz się w „Kolejność dekoratorów odporności”?

Poznaj wpływ kolejności nakładania wzorców odporności (retry, circuit breaker, bulkhead, timeout, rate limiter) na działanie systemu oraz dowiedz się, jak wybrać właściwą kolejność. Ćwiczysz Microservices Communication Patterns (Saga, Circuit Breaker) z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Microservices Communication Patterns (Saga, Circuit Breaker)?

Nie wymagamy żadnego doświadczenia. Microservices Communication Patterns (Saga, Circuit Breaker) w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Kolejność dekoratorów odporności”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Microservices Communication Patterns (Saga, Circuit Breaker)?

Tak. Każda lekcja Microservices Communication Patterns (Saga, Circuit Breaker) zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Circuit breaker i bulkhead
  2. Circuit breaker z logiką ponawiania prób
  3. Integracja ograniczania liczby żądań
  4. Kolejność dekoratorów odporności
← Powrót do Microservices Communication Patterns (Saga, Circuit Breaker)