Timeout Anti-patterns: Common Mistakes and How to Avoid Them
No Timeout Set
The most dangerous anti-pattern. Library defaults are often infinite or very long. A single hung connection can block a thread forever. Audit every external call and verify explicit timeout is configured. This is a code review checklist item.
Same Timeout for All Operations
A 30 second timeout applied everywhere is too short for batch operations and too long for cache reads. Different operations have different latency profiles. Cache: 100ms. Simple API: 1-5s. Complex query: 10-30s. Batch job: minutes.
Timeout Without Cancellation
Client times out but downstream continues working. This wastes resources and can cause inconsistency. When timing out, attempt to cancel the operation: close connections, send cancellation signals. Some operations cannot be cancelled once started; acknowledge this and design accordingly.
Retry Without Backoff After Timeout
Immediate retry after timeout hammers already struggling services. If Service A is slow, instant retries make it slower. Use exponential backoff: wait 1s, 2s, 4s between retries. Add jitter to prevent thundering herd when many clients retry simultaneously.
Ignoring Timeout Errors
Catching timeout exceptions and returning empty results silently hides problems. Users see incomplete data without knowing why. Log timeouts, track metrics, alert on elevated rates. Make timeout handling explicit in code so readers understand the failure mode.