Timeouts
Chapter 24: Timeouts & Graceful Degradation
Section titled “Chapter 24: Timeouts & Graceful Degradation”Handling Unresponsive Systems
Section titled “Handling Unresponsive Systems”24.1 Why Timeouts Matter
Section titled “24.1 Why Timeouts Matter” Without Timeout ==============
Request sent Service is slow (or down) Client waits... and waits... and waits... Client crashes or times out poorly
User experience: Frustrating24.2 Types of Timeouts
Section titled “24.2 Types of Timeouts”| Timeout | Description |
|---|---|
| Connect | Time to establish connection |
| Read | Time to receive response |
| Write | Time to send request |
| Request | Total request time |
24.3 Setting Timeouts
Section titled “24.3 Setting Timeouts”Per-Operation
Section titled “Per-Operation” Database Timeout ================
Connection timeout: 2-5 seconds Query timeout: 5-10 seconds
db.query("SELECT *", timeout=5)Per-Service
Section titled “Per-Service” Service Timeouts ================
Critical service: 1 second Non-critical: 5 seconds Background: 30 seconds
timeout = { "user-service": 1000, "payment-service": 5000, "notification-service": 30000 }24.4 Timeout Strategies
Section titled “24.4 Timeout Strategies”Timeout vs. Retry
Section titled “Timeout vs. Retry” Timeout + Retry = Good UX ========================
Operation timeout: 3 seconds Retry: 2 times
Total potential wait: 9 seconds But mostly: 3 seconds (if works)Circuit Breaker + Timeout
Section titled “Circuit Breaker + Timeout” Combined Approach =================
1. Timeout: Don't wait forever 2. Circuit Breaker: Stop after failures 3. Fallback: Return cached/default
Result: Fast failure, graceful degradation24.5 Graceful Degradation
Section titled “24.5 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!Examples
Section titled “Examples”| Feature | Degraded Mode |
|---|---|
| Recommendations | Show popular items |
| Search | Show cached results |
| Payment | Save for later |
| Analytics | Show “unavailable” |
24.6 Best Practices
Section titled “24.6 Best Practices”| Practice | Description |
|---|---|
| Set timeouts | Never wait forever |
| Fail fast | Quick timeout > slow success |
| Graceful degradation | Show fallback |
| Monitor timeouts | Track slow services |
| Tiered timeouts | Critical vs non-critical |
Summary
Section titled “Summary”- Never wait forever - Always set timeouts
- Fail fast - Quick timeout is better
- Graceful degradation - Return fallback
- Monitor - Track timeout rates
- Circuit breaker - Combine with timeout