サーキットブレーカーへのフォールバック追加
サーキットブレーカーとフォールバック処理を組み合わせ、ブレーカーがオープン状態になったときにユーザーのリクエストを失敗させるのではなく、サービスを適切に縮退させる方法を学びます。
「サーキットブレーカーへのフォールバック追加」はCoddyKit上の無料Microservices Communication Patterns (Saga, Circuit Breaker)レッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMicroservices Communication Patterns (Saga, Circuit Breaker)学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Microservices Communication Patterns (Saga, Circuit Breaker)コースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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.
AI チューターと学ぶ Microservices Communication Patterns (Saga, Circuit Breaker) — 無料
ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。
- コース
- 12
- レッスン
- 48
よくある質問
「サーキットブレーカーへのフォールバック追加」レッスンは無料ですか?
はい。「サーキットブレーカーへのフォールバック追加」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Microservices Communication Patterns (Saga, Circuit Breaker)コースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Microservices Communication Patterns (Saga, Circuit Breaker)コースには全4レッスンが含まれています。
「サーキットブレーカーへのフォールバック追加」で何を学びますか?
サーキットブレーカーとフォールバック処理を組み合わせ、ブレーカーがオープン状態になったときにユーザーのリクエストを失敗させるのではなく、サービスを適切に縮退させる方法を学びます。 ブラウザで直接実行するハンズオンコードでMicroservices Communication Patterns (Saga, Circuit Breaker)を演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Microservices Communication Patterns (Saga, Circuit Breaker)を始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのMicroservices Communication Patterns (Saga, Circuit Breaker)は初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「サーキットブレーカーへのフォールバック追加」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このMicroservices Communication Patterns (Saga, Circuit Breaker)レッスンでコードを書いて実行できますか?
はい。すべてのMicroservices Communication Patterns (Saga, Circuit Breaker)レッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- サーキットブレーカーライブラリの選択
- サーキットブレーカーインスタンスの設定
- サービス呼び出しへの統合
- サーキットブレーカーへのフォールバック追加