Microservices Communication Patterns (Saga, Circuit Breaker) · Lekcja

Dodawanie mechanizmów fallback do circuit breakerów

Poznaj sposób łączenia circuit breakera z logiką fallback, aby po otwarciu obwodu usługa degradowała się w kontrolowany sposób zamiast kończyć żądanie użytkownika błędem.

Lekcja 4 z 413 kroki

Dodawanie mechanizmów fallback do circuit breakerów 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.

Why Fallbacks Matter

A circuit breaker protects your service by failing fast when a dependency is down. But failing fast still means the user gets an error unless you provide a fallback.

A fallback is the plan B that runs when the breaker is open.

The Fallback Contract

A fallback should return a sensible default quickly and never call the same failing dependency. It is invoked when:

  • The breaker is open, or
  • The protected call throws or times out.

A Simple Fallback

Here is the core idea: try the real call, and if it fails, return the fallback value.

def get_price(call_remote):
    try:
        return call_remote()
    except Exception:
        return 'fallback: last-known price'

print(get_price(lambda: (_ for _ in ()).throw(Exception('down'))))

Fallback: Cached Value

A common strategy is to serve the last successful response from a cache. The user sees slightly stale data instead of an error.

cache = {'price': 42}

def get_with_cache(breaker_open):
    if breaker_open:
        return cache.get('price', 'unavailable')
    return 'fresh value'

print(get_with_cache(True))

Fallback: Default Value

When no cache exists, return a safe default: an empty list, a neutral recommendation, or a generic message. The key is that the user experience degrades, not breaks.

def recommendations(breaker_open):
    if breaker_open:
        return ['Popular item A', 'Popular item B']
    return ['Personalized 1', 'Personalized 2']

print(recommendations(True))

Fallback: Alternate Service

Sometimes plan B is another provider. If the primary payment gateway's breaker is open, route to a secondary gateway. Each provider has its own breaker.

Wiring Fallbacks in Resilience4j

Most libraries let you attach a fallback declaratively. With Resilience4j you decorate the call with a circuit breaker and supply a recover function that runs on failure or open state.

Keep Fallbacks Fast and Safe

A fallback must not introduce new failure modes:

  • No call to the broken dependency.
  • No blocking I/O that could also hang.
  • Bounded, predictable execution time.

Communicating Degradation

Tell the user (and your dashboards) when degraded data is served. A subtle UI note like 'showing cached results' sets expectations, and a metric on fallback rate reveals dependency health.

fallback_count = 0

def record_fallback():
    global fallback_count
    fallback_count += 1
    return fallback_count

print('Fallbacks served:', record_fallback())

When NOT to Fall Back

Some operations have no safe default. You cannot 'fall back' on confirming a payment. In those cases, fail clearly and let the caller retry later rather than fabricate a result.

Testing Fallbacks

Write tests that force the breaker open and assert the fallback runs and returns the expected safe value. Fallbacks that are never tested tend to break silently.

Quick Check

Which of these is a valid requirement for a circuit breaker fallback?

Recap

You learned to add fallbacks to circuit breakers:

  • Fallbacks run when the breaker is open or the call fails.
  • Common strategies: cached value, safe default, alternate service.
  • Keep fallbacks fast, safe, and free of the broken dependency.
  • Communicate degradation and test fallbacks explicitly.

Fallbacks turn fast failures into graceful degradation.

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 „Dodawanie mechanizmów fallback do circuit breakerów” jest bezpłatna?

Tak — pełny tekst „Dodawanie mechanizmów fallback do circuit breakerów” 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 „Dodawanie mechanizmów fallback do circuit breakerów”?

Poznaj sposób łączenia circuit breakera z logiką fallback, aby po otwarciu obwodu usługa degradowała się w kontrolowany sposób zamiast kończyć żądanie użytkownika błędem. Ć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 „Dodawanie mechanizmów fallback do circuit breakerów”?

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. Wybór biblioteki circuit breaker
  2. Konfigurowanie instancji circuit breaker
  3. Integracja z wywołaniami usług
  4. Dodawanie mechanizmów fallback do circuit breakerów
← Powrót do Microservices Communication Patterns (Saga, Circuit Breaker)