ตัวตัดวงจรและการลดความสามารถอย่างราบรื่น
เรียนรู้ว่าตัวตัดวงจรช่วยป้องกันความล้มเหลวที่ลุกลามได้อย่างไร และการลดความสามารถอย่างราบรื่นช่วยให้ระบบยังมีประโยชน์แม้การพึ่งพาอื่นล้มเหลวได้อย่างไร
ตัวตัดวงจรและการลดความสามารถอย่างราบรื่น เป็นบทเรียน System Design Basics for Backend Developers ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน System Design Basics for Backend Developers และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส System Design Basics for Backend Developers มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
The Cascading Failure Problem
In a system of dependent services, one slow service can drag down everything that calls it. Threads pile up waiting, queues fill, and the failure cascades across the whole system.
High availability means containing failures, not just preventing them.
What a Circuit Breaker Does
A circuit breaker wraps calls to a dependency. When failures cross a threshold, it opens and fails fast instead of waiting on a dead service.
- Stops wasting threads on doomed calls
- Gives the failing service time to recover
The Three States
A circuit breaker has three states:
- Closed: calls flow normally, failures are counted
- Open: calls fail immediately without hitting the dependency
- Half-open: a few trial calls test whether the dependency recovered
CLOSED --(too many failures)--> OPEN
OPEN --(timeout elapsed)--> HALF_OPEN
HALF_OPEN --(trial succeeds)--> CLOSED
HALF_OPEN --(trial fails)--> OPENA Simple Breaker in Code
Here is the core idea: count failures, trip when a threshold is reached, and refuse calls while open.
class Breaker:
def __init__(self, limit):
self.fails = 0
self.limit = limit
self.open = False
def call(self, ok):
if self.open:
return 'rejected'
if ok:
self.fails = 0
return 'success'
self.fails += 1
if self.fails >= self.limit:
self.open = True
return 'failure'
b = Breaker(3)
for ok in [False, False, False, True]:
print(b.call(ok))Timeouts Are Essential
A breaker only helps if calls have timeouts. Without a timeout, a hung dependency holds a thread forever and failures are never counted. Always set aggressive, explicit timeouts on remote calls.
Retries and Backoff
Retries can help with transient errors but can also amplify an overload. Use exponential backoff with jitter and cap the retry count. Combine with a circuit breaker so retries stop entirely when the circuit is open.
import random
delay = 1
for attempt in range(4):
wait = delay + random.uniform(0, delay)
print('attempt', attempt, 'wait', round(wait, 2))
delay *= 2Graceful Degradation
Graceful degradation means the system still does something useful when a dependency is down, instead of returning an error.
- Serve stale cached data
- Hide a non-critical feature
- Return a sensible default
Fallbacks
When the breaker is open, route to a fallback. For a product page, if the recommendations service is down, show a generic best-sellers list instead of failing the whole page.
def get_recommendations(breaker):
if breaker.open:
return ['bestseller-1', 'bestseller-2']
return ['personalized-1', 'personalized-2']Bulkheads
The bulkhead pattern isolates resources so one failing dependency cannot consume all threads or connections. Give each downstream dependency its own bounded pool — like watertight compartments in a ship.
Load Shedding
Under extreme load, it is better to reject some requests quickly than to slow down for everyone. Load shedding drops low-priority traffic to protect critical paths and keep latency bounded.
Putting It Together
Resilient services layer these patterns: tight timeouts, circuit breakers, bulkheads to isolate, fallbacks for degradation, and load shedding under pressure. Together they turn a potential outage into a minor, contained blip.
Quick Check
Test your understanding of circuit breakers.
Recap
You learned to contain failures for high availability:
- Circuit breakers fail fast and cycle through closed, open, and half-open
- Timeouts and capped backoff retries prevent overload amplification
- Graceful degradation and fallbacks keep the system useful
- Bulkheads and load shedding isolate and protect critical paths
เรียนรู้ System Design Basics for Backend Developers ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “ตัวตัดวงจรและการลดความสามารถอย่างราบรื่น” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ตัวตัดวงจรและการลดความสามารถอย่างราบรื่น” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส System Design Basics for Backend Developers ให้อัปเกรดเป็น CoddyKit PRO คอร์ส System Design Basics for Backend Developers มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ตัวตัดวงจรและการลดความสามารถอย่างราบรื่น”
เรียนรู้ว่าตัวตัดวงจรช่วยป้องกันความล้มเหลวที่ลุกลามได้อย่างไร และการลดความสามารถอย่างราบรื่นช่วยให้ระบบยังมีประโยชน์แม้การพึ่งพาอื่นล้มเหลวได้อย่างไร คุณปฏิบัติ System Design Basics for Backend Developers ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน System Design Basics for Backend Developers หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน System Design Basics for Backend Developers บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “ตัวตัดวงจรและการลดความสามารถอย่างราบรื่น” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน System Design Basics for Backend Developers นี้ได้ไหม
ได้ บทเรียน System Design Basics for Backend Developers ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- กลไกความซ้ำซ้อนและการสลับระบบเมื่อขัดข้อง
- การวางแผนกู้คืนระบบจากภัยพิบัติ
- การเฝ้าติดตาม การแจ้งเตือน และการบันทึกข้อมูลระบบ
- ตัวตัดวงจรและการลดความสามารถอย่างราบรื่น