Skip to content

Timeouts

Chapter 24: Timeouts & Graceful Degradation

Section titled “Chapter 24: Timeouts & Graceful Degradation”

Without Timeout
==============
Request sent
Service is slow (or down)
Client waits...
and waits...
and waits...
Client crashes or times out poorly
User experience: Frustrating

TimeoutDescription
ConnectTime to establish connection
ReadTime to receive response
WriteTime to send request
RequestTotal request time

Database Timeout
================
Connection timeout: 2-5 seconds
Query timeout: 5-10 seconds
db.query("SELECT *", timeout=5)
Service Timeouts
================
Critical service: 1 second
Non-critical: 5 seconds
Background: 30 seconds
timeout = {
"user-service": 1000,
"payment-service": 5000,
"notification-service": 30000
}

Timeout + Retry = Good UX
========================
Operation timeout: 3 seconds
Retry: 2 times
Total potential wait: 9 seconds
But mostly: 3 seconds (if works)
Combined Approach
=================
1. Timeout: Don't wait forever
2. Circuit Breaker: Stop after failures
3. Fallback: Return cached/default
Result: Fast failure, graceful degradation

Graceful Degradation
====================
Feature X fails:
- Instead of error: Return cached data
- Instead of error: Show default
- Instead of error: Disable feature
User still gets something!
FeatureDegraded Mode
RecommendationsShow popular items
SearchShow cached results
PaymentSave for later
AnalyticsShow “unavailable”

PracticeDescription
Set timeoutsNever wait forever
Fail fastQuick timeout > slow success
Graceful degradationShow fallback
Monitor timeoutsTrack slow services
Tiered timeoutsCritical vs non-critical

  1. Never wait forever - Always set timeouts
  2. Fail fast - Quick timeout is better
  3. Graceful degradation - Return fallback
  4. Monitor - Track timeout rates
  5. Circuit breaker - Combine with timeout