0Pricing
Microservices Communication Patterns (Saga, Circuit Breaker) · Lektion

Fallbacks zu Circuit Breakern hinzufügen

Lernen Sie, wie Sie einen Circuit Breaker mit Fallback-Logik kombinieren, damit Ihr Dienst bei geöffnetem Breaker kontrolliert degradiert, anstatt die Benutzeranfrage fehlschlagen zu lassen.

Fallbacks zu Circuit Breakern hinzufügen ist eine kostenlose Microservices Communication Patterns (Saga, Circuit Breaker)-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Microservices Communication Patterns (Saga, Circuit Breaker)-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Microservices Communication Patterns (Saga, Circuit Breaker)-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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.

Häufig gestellte Fragen

Ist die Lektion „Fallbacks zu Circuit Breakern hinzufügen“ kostenlos?

Ja — der vollständige Text von „Fallbacks zu Circuit Breakern hinzufügen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Microservices Communication Patterns (Saga, Circuit Breaker)-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Microservices Communication Patterns (Saga, Circuit Breaker)-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Fallbacks zu Circuit Breakern hinzufügen“?

Lernen Sie, wie Sie einen Circuit Breaker mit Fallback-Logik kombinieren, damit Ihr Dienst bei geöffnetem Breaker kontrolliert degradiert, anstatt die Benutzeranfrage fehlschlagen zu lassen. Du übst Microservices Communication Patterns (Saga, Circuit Breaker) mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Microservices Communication Patterns (Saga, Circuit Breaker) zu starten?

Keine Vorkenntnisse erforderlich. Microservices Communication Patterns (Saga, Circuit Breaker) auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.

Wie lange dauert die Lektion „Fallbacks zu Circuit Breakern hinzufügen“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Microservices Communication Patterns (Saga, Circuit Breaker)-Lektion Code schreiben und ausführen?

Ja. Jede Microservices Communication Patterns (Saga, Circuit Breaker)-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Eine Circuit-Breaker-Bibliothek auswählen
  2. Circuit-Breaker-Instanzen konfigurieren
  3. In Serviceaufrufe integrieren
  4. Fallbacks zu Circuit Breakern hinzufügen
← Zurück zu Microservices Communication Patterns (Saga, Circuit Breaker)