Microservices Communication Patterns (Saga, Circuit Breaker) · บทเรียน

การเพิ่มกลไกสำรองให้ Circuit Breaker

เรียนรู้วิธีจับคู่ Circuit Breaker กับตรรกะสำรอง เพื่อให้เมื่อเบรกเกอร์เปิดอยู่ บริการยังลดความสามารถลงอย่างเหมาะสมแทนที่จะทำให้คำขอของผู้ใช้ล้มเหลว

บทเรียน 4 จาก 413 ขั้นตอน

การเพิ่มกลไกสำรองให้ Circuit Breaker เป็นบทเรียน Microservices Communication Patterns (Saga, Circuit Breaker) ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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.

เริ่มต้นได้ฟรี

เรียนรู้ Microservices Communication Patterns (Saga, Circuit Breaker) ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “การเพิ่มกลไกสำรองให้ Circuit Breaker” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การเพิ่มกลไกสำรองให้ Circuit Breaker” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Microservices Communication Patterns (Saga, Circuit Breaker) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Microservices Communication Patterns (Saga, Circuit Breaker) มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การเพิ่มกลไกสำรองให้ Circuit Breaker”

เรียนรู้วิธีจับคู่ Circuit Breaker กับตรรกะสำรอง เพื่อให้เมื่อเบรกเกอร์เปิดอยู่ บริการยังลดความสามารถลงอย่างเหมาะสมแทนที่จะทำให้คำขอของผู้ใช้ล้มเหลว คุณปฏิบัติ Microservices Communication Patterns (Saga, Circuit Breaker) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Microservices Communication Patterns (Saga, Circuit Breaker) หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Microservices Communication Patterns (Saga, Circuit Breaker) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การเพิ่มกลไกสำรองให้ Circuit Breaker” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Microservices Communication Patterns (Saga, Circuit Breaker) นี้ได้ไหม

ได้ บทเรียน Microservices Communication Patterns (Saga, Circuit Breaker) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การเลือกไลบรารีตัวตัดวงจร
  2. การตั้งค่าอินสแตนซ์ตัวตัดวงจร
  3. การผสานเข้ากับการเรียกใช้บริการ
  4. การเพิ่มกลไกสำรองให้ Circuit Breaker
← กลับไปที่ Microservices Communication Patterns (Saga, Circuit Breaker)