API-35Required
Set timeouts on every call
Give every outgoing and incoming request a time limit. Never wait forever on another service.
How to do it
Set a connect timeout and a total timeout on each HTTP client, database and queue call. Set each limit to about twice that call's 99th percentile time. Without measurements yet, start at 1 second to connect and 3 seconds in total. Make each inner limit shorter than the outer one, so the caller gets a clear 503 or 504 problem. The database query is shortest, then the server's request, then the screen's, as in query time limits and screen request time limits. Model calls follow model timeouts.
Examples
JavaScript example
const response = await fetch('https://stock.example.com/items/TEA-01', { signal: AbortSignal.timeout(3000) })
Python example
import httpx
response = httpx.get("https://stock.example.com/items/TEA-01", timeout=httpx.Timeout(3.0, connect=1.0))
How to check
Point a staging dependency at a server that accepts connections and never answers. The request fails within the set limit with a 504 or 503 problem.
Sources
Related rules
- ENG-47 Keep serving when the flag service is down in Engineering
- ARC-78 Pass a cancel signal through every call chain in Clean code and architecture
- ARC-90 Give each outside dependency its own pool in Clean code and architecture
- TST-16 Test how your code handles each outside service failing in Testing
- DAT-37 Set time limits on every query in Data and databases
- APP-51 Give every request a time limit in Web app interface
- AIF-39 Time out every model call in AI features
- API-75 Wrap each outside API in one adapter in API design
- API-89 Set a deadline on every gRPC call and pass it on in API design
- OPS-70 Match timeouts from proxy to app and stop abandoned work in Operations and observability
- OPS-66 Cap retries across the whole service in Operations and observability
- SCL-15 Keep the app working when the cache is gone in Scaling
- EVT-17 Give every job a time limit in Integration and events
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/api-design/reliability/timeouts">Agent Wranglers API design playbook</a>. In code you write, add a comment next to it with the same link.