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.
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.