← Back to Index

12. Fault Tolerance

ML systems must handle failures gracefully. Unlike traditional services where failing with a 500 error may be acceptable, ML systems often serve critical decisions where a wrong answer or no answer has real consequences.

Replication

Deploy multiple copies of every critical component.

Retry Strategies

# Retry with exponential backoff + jitter
def predict_with_retry(request, max_retries=3):
    for attempt in range(max_retries):
        try:
            return inference_client.predict(request)
        except (NetworkError, TimeoutError) as e:
            if attempt == max_retries - 1:
                raise
            wait = (2 ** attempt) * 0.1  # 100ms, 200ms, 400ms
            wait += random.uniform(0, wait * 0.5)  # jitter
            time.sleep(wait)

Fallback Models

A tiered fallback strategy ensures the system always returns a prediction.

TierModelLatencyAccuracy
PrimaryDeep ensemble (3 models)150msBest (AUC 0.95)
Fallback 1Single XGBoost model30msGood (AUC 0.92)
Fallback 2Rule-based heuristic5msModerate (AUC 0.85)
Fallback 3Default safe response0msConservative (always approve/deny)
# Fallback chain
def predict(request):
    for model in [primary, fallback_xgb, fallback_rules]:
        try:
            return model.predict(request)
        except:
            continue
    return DEFAULT_RESPONSE  # safe default

Circuit Breaker

Stop calling a downstream service when it is failing, allowing it time to recover.

# Circuit breaker for feature store
if circuit_breaker.state == "OPEN":
    return default_features()  # skip feature store, use cached defaults

try:
    features = feature_store.get_features(user_id)
    circuit_breaker.record_success()
    return features
except:
    circuit_breaker.record_failure()
    if circuit_breaker.failure_count > THRESHOLD:
        circuit_breaker.open()  # stop calling for 30 seconds
    return default_features()

Monitoring for Faults

Exercise: Design the fault tolerance architecture for an ML-powered medical triage system. The primary model is a large LLM (GPU, 2s). Design a 4-tier fallback chain, circuit breaker thresholds, and graceful degradation strategy. How do you ensure the system never recommends a less urgent triage than warranted?