Resilience & Service PatternsTimeout PatternsMedium⏱️ ~1 min

Timeout Implementation: Strategies for Different Operation Types

HTTP Client Timeouts

Configure at client creation, not per request. Most HTTP libraries support: connectTimeout, readTimeout, writeTimeout. Some add callTimeout for total request duration. Always set all available timeouts; defaults are often infinite or very long.

Database Query Timeouts

Set at multiple levels: connection pool acquisition timeout (1-5s), query execution timeout (5-30s), and transaction timeout (30-60s). A runaway query can hold connections and locks. Query timeout kills the query server side; connection timeout kills client side waiting. Both are needed.

Message Queue Timeouts

Consumer processing timeout: how long before message is considered failed and requeued. Producer send timeout: how long to wait for broker acknowledgment. Visibility timeout (SQS pattern): how long message is hidden while being processed. Set based on expected processing time plus buffer for retries.

✅ Best Practice: Audit all external calls in your service. For each, verify timeout is explicitly set. Missing timeout is a bug waiting to cause an outage.

Cache Timeouts

Cache read timeout should be very short (50-200ms). Cache is supposed to be fast; a slow cache defeats its purpose. If cache times out, fall back to primary data source. Cache write can be async with longer timeout or fire-and-forget.

Async Operation Timeouts

For background jobs and async processing, use job-level timeouts that kill the entire operation if it runs too long. Also implement heartbeat timeouts: if worker stops sending heartbeats, assume it died and reassign work. Prevents jobs from running forever or getting stuck.

💡 Key Takeaways
HTTP: set connectTimeout, readTimeout, writeTimeout, callTimeout. Defaults are often infinite.
Database: connection pool acquisition (1-5s), query execution (5-30s), transaction (30-60s). All three needed.
Cache timeout should be very short (50-200ms). Slow cache defeats its purpose; fall back to primary source.
📌 Interview Tips
1Audit checklist: every external call needs explicit timeout. Missing timeout is a bug.
2Database layers: pool acquisition timeout + query timeout + transaction timeout all serve different purposes
3Cache fallback pattern: try cache with 100ms timeout, on timeout/miss call primary source
← Back to Timeout Patterns Overview