重試預算、指數退避與抖動
Agent 輔助工程檢查
前置要求
第 76-77 天的重試計數器機制;基礎機率。
學習目標
設計一個帶硬性嘗試預算、指數退避與全抖動的重試策略;解釋無界的同步重試如何把局部故障放大為級聯故障。
操作指引
1. 專案的策略刻意保持最小:每次 ok=false 觀測使 retries 遞增,retries > max_retries=2 時執行失敗——立即重發,無延遲。
2. 正式環境策略(AWS Builder's Library):delay = random_between(0, min(cap, base * 2**attempt));全抖動去除各用戶端相關性,使競爭期的總工作量最小化。
3. 級聯故障模型(Google SRE 第 22 章):依賴方降級時重試恰好成倍放大負載;重試預算加抖動退避為這種放大設定了上限。
4. 只對暫時故障重試;永久故障(invalid_arguments、not_found)需要修正——配接器先改正引數再重發,這正是其重試有效的原因。
自主實作與記錄
實作 retry_with_backoff(fn, attempts=3, base=0.05, cap=0.5):每次嘗試間按全抖動間隔休眠,耗盡後重新拋出例外。用一個失敗兩次後成功的不穩定樁與一個必然耗盡全部預算的永久失敗分別驅動它。
sleep = random.uniform(0, min(cap, base * 2attempt))——絕不用確定性的 2attempt 休眠。
import random, time
def retry_with_backoff(fn, attempts=3, base=0.05, cap=0.5):
for i in range(attempts):
try:
return fn()
except Exception:
if i == attempts - 1:
raise
time.sleep(random.uniform(0, min(cap, base * 2**i)))
藉助抖動公式說明:專案的重試預算(2)與正式環境抖動退避都約束了工作量,並指出各自防禦的故障(預算耗盡 vs 相關重試風暴)。
權威來源資源與權利證明
Exponential Backoff And Jitter
對應章節 / 時間戳: Adding Backoff; Adding Jitter (Full Jitter, Equal Jitter, Decorrelated Jitter)
驗證版本與日期: AWS Architecture Blog 2015 (Verified: 2026-10-30)
Detailed practitioner reference proving that full jitter (sleep = random_between(0, min(cap, base*2**attempt))) prevents synchronized retry storms and reduces total work during contention.
Retries, timeouts, and basic probability of correlated load.
The backoff exercise is implemented in pure Python with a local flaky stub; the derivation of jitter benefits is fully reproduced in lesson instructions.
對比候選資源與選型分析 (2)
Product-scoped truncated exponential backoff recipe; Brooker's analysis explains why jitter matters, not just what to configure.
Library implementation reference; the AWS article supplies the comparative evidence (with/without jitter) that anchors the lesson.
Site Reliability Engineering, Chapter 22: Addressing Cascading Failures
對應章節 / 時間戳: Preventing Server Overload: Retries
驗證版本與日期: O'Reilly 2016 free online edition (Verified: 2026-10-30)
Primary analysis of how unbounded retries amplify partial outages into cascading failures, motivating hard retry budgets and deadlines in agent runtimes.
Retries, queueing, and load-shedding basics.
The retry-amplification scenario and budget arithmetic are fully reproduced in the lesson's worked example.
對比候選資源與選型分析 (2)
Broader stability-pattern catalog (circuit breakers, bulkheads); the SRE chapter is free and directly quantifies retry amplification.
Strong companion article; the SRE chapter frames the failure mode (cascading overload) that the retry budget defends against.