Timeout Granularity: Per-Operation vs Global
You'll learn: How to choose between setting a single timeout for an entire request versus separate timeouts for each operation within that request.
The Problem with One-Size-Fits-All
Imagine you're cooking a complex meal with multiple dishes. You could set one kitchen timer for everything, but your salad would sit wilting while the roast finishes. Or you could set separate timers for each dish—much more precise, but requires juggling multiple alarms.
Timeouts work the same way. You have two fundamental approaches to granularity:
Global Timeout
A global timeout applies to the entire user request from start to finish. If your API endpoint needs to fetch user data, check permissions, and format a response, one timeout covers all three steps combined.
Pros: Simple to implement and reason about. The client knows exactly how long they'll wait maximum.
Cons: Doesn't account for varying operation costs. A slow database query eats up budget that a fast cache lookup didn't need.
Per-Operation Timeout
A per-operation timeout assigns specific limits to individual steps: database queries get 500ms, external API calls get 2s, cache lookups get 100ms.
Pros: Prevents one slow operation from consuming the entire time budget. You can fail fast on expensive operations while allowing cheap ones to complete.
Cons: More configuration complexity. The sum of per-operation timeouts might exceed what's acceptable to the user.
Combining Both
The best approach often uses both: set per-operation timeouts to prevent individual bottlenecks, and a global timeout as an absolute ceiling. If the database gets 500ms but the overall request must complete in 3s, you've protected both individual operations and the total user experience.
Key Takeaway: Per-operation timeouts prevent individual bottlenecks and enable fast-fail behavior, while global timeouts guarantee total response time; using both provides defense in depth against slow requests.