Resilience & Service PatternsTimeout PatternsHard⏱️ ~1 min

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.

⚠️ Key Trade-off: Aggressive cancellation saves resources but adds complexity. Some systems support cooperative cancellation (check flag periodically), others require forceful termination. Know your systems capabilities.

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.

💡 Key Takeaways
No timeout is the worst anti-pattern. Audit every external call; missing timeout is a code review fail.
Different operations need different timeouts: cache 100ms, API 1-5s, complex query 10-30s, batch minutes
Timeout without cancellation wastes resources. Attempt to cancel downstream operations when timing out.
📌 Interview Tips
1List timeout values by operation type: cache 100ms, simple API 1-5s, complex query 10-30s
2Recommend backoff: 1s, 2s, 4s between retries with jitter to prevent thundering herd
3Make timeout handling explicit: catch, log, metric, propagate. Never silently return empty results.
← Back to Timeout Patterns Overview