รูปแบบด้านความทนทาน: Circuit Breaker การลองใหม่ และการหมดเวลา
รักษาสุขภาพของระบบแบบกระจายเมื่อเกิดความล้มเหลวด้วยการหมดเวลา การลองใหม่แบบมีขอบเขตพร้อมการหน่วงเวลา และรูปแบบ Circuit Breaker เพื่อป้องกันการหยุดทำงานต่อเนื่องเป็นลูกโซ่
รูปแบบด้านความทนทาน: Circuit Breaker การลองใหม่ และการหมดเวลา เป็นบทเรียน Linux Networking & TCP/IP for Developers ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Linux Networking & TCP/IP for Developers และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Linux Networking & TCP/IP for Developers มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Failure Is Normal
In a microservices network, calls cross machines and links that will fail. Resilience patterns keep one slow or broken service from dragging down the whole system.
Always Set Timeouts
A call with no timeout can hang forever, exhausting threads and connections. Every remote call must have a deadline.
import requests
r = requests.get('http://orders/api', timeout=2.0)The Danger of Naive Retries
Retrying immediately after a failure can amplify an outage — a struggling service gets hit even harder. Retries must be bounded and spaced.
Exponential Backoff
Increase the wait between attempts exponentially so a recovering service gets breathing room.
for attempt in range(5):
try:
return call()
except Exception:
wait = 2 ** attempt
time.sleep(wait)Adding Jitter
If many clients back off on the same schedule they retry in sync, creating a thundering herd. Add random jitter to spread the load.
import random
wait = (2 ** attempt) + random.uniform(0, 1)Retry Only Idempotent Operations
Retrying a non-idempotent write (like 'charge card') can double-execute it. Only retry safe operations, or use idempotency keys to make writes safe.
The Circuit Breaker
A circuit breaker tracks failures to a dependency. After too many, it opens and fails fast instead of waiting on a dead service.
This stops resources from piling up on a doomed call.
Breaker States
A circuit breaker has three states:
- Closed — calls flow normally
- Open — calls fail immediately
- Half-Open — a few test calls probe recovery
Breaker in Code
A minimal breaker counts failures and trips after a threshold, refusing calls until a cooldown elapses.
if breaker.is_open():
raise CircuitOpenError()
try:
result = call()
breaker.record_success()
except Exception:
breaker.record_failure()
raiseFallbacks and Graceful Degradation
When a breaker is open, return a sensible fallback: cached data, a default value, or a reduced feature set. A degraded response beats a total failure.
Bulkheads
The bulkhead pattern isolates resources (thread pools, connection pools) per dependency, so one saturated dependency cannot starve the others.
Quick Check
Test your resilience knowledge.
Recap
You can now build resilient service calls:
- Always set timeouts
- Bounded retries with exponential backoff + jitter
- Retry only idempotent operations
- Circuit breakers (closed/open/half-open) to fail fast
- Fallbacks and bulkheads for graceful degradation
This complements your load balancing, service mesh, and API gateway lessons.
เรียนรู้ Linux Networking & TCP/IP for Developers ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “รูปแบบด้านความทนทาน: Circuit Breaker การลองใหม่ และการหมดเวลา” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “รูปแบบด้านความทนทาน: Circuit Breaker การลองใหม่ และการหมดเวลา” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Linux Networking & TCP/IP for Developers ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Linux Networking & TCP/IP for Developers มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “รูปแบบด้านความทนทาน: Circuit Breaker การลองใหม่ และการหมดเวลา”
รักษาสุขภาพของระบบแบบกระจายเมื่อเกิดความล้มเหลวด้วยการหมดเวลา การลองใหม่แบบมีขอบเขตพร้อมการหน่วงเวลา และรูปแบบ Circuit Breaker เพื่อป้องกันการหยุดทำงานต่อเนื่องเป็นลูกโซ่ คุณปฏิบัติ Linux Networking & TCP/IP for Developers ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Linux Networking & TCP/IP for Developers หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Linux Networking & TCP/IP for Developers บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “รูปแบบด้านความทนทาน: Circuit Breaker การลองใหม่ และการหมดเวลา” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Linux Networking & TCP/IP for Developers นี้ได้ไหม
ได้ บทเรียน Linux Networking & TCP/IP for Developers ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- กลยุทธ์การกระจายโหลด
- สถาปัตยกรรมเซอร์วิสเมช (Istio/Linkerd)
- เกตเวย์ API และการกำหนดเส้นทางขอบเครือข่าย
- รูปแบบด้านความทนทาน: Circuit Breaker การลองใหม่ และการหมดเวลา