A timeout is an explicit time limit on an operation: if a service does not respond in time, stop waiting and raise an error. It is an escape hatch, not a repair for the failing service. section 10.2.1
Without a timeout, a frozen API can leave server threads waiting indefinitely. As requests pile up, memory can run out and the application can crash. Assign a timeout to every network call rather than betting on the happy path. section 10.2.1 section 10.4.1
A network request has two phases, and many HTTP libraries let you set a separate limit for each:
- Connection timeout: How long to wait to establish a connection. If the server cannot answer the door, fail fast. section 10.2.1
- Read timeout: How long to wait for the connected server to return data. The server may need more time to process the request. section 10.2.1
A payment API under pressure
Suppose a payment API normally completes 99% of healthy requests in under two seconds, but now successful requests take eight seconds. A hard limit can prevent checkout threads from waiting too long. Choose the threshold using monitoring baselines and service-level agreement targets, rather than a guess. section 10.8 section 10.2.1
In Python's requests library, timeout=(2, 8) sets a two-second connection timeout and an eight-second read timeout. Those values are a rule of thumb, not universal settings: use evidence from the service and its operating requirements to choose yours. section 10.2.1
A timeout needs a plan for the error it raises. Retries with exponential backoff can give a temporary failure another chance; a circuit breaker can fail fast when a dependency is struggling; and a fallback can preserve an order for later processing. For payments, use an idempotency key before retrying so an uncertain response does not lead to a duplicate charge. Catch the final failure too, or the retry loop can still crash the application. section 10.10 section 10.4.2










