liveBook · Manning

Timeout

As explained in Grokking Software Architecture

A time limit after which an operation is stopped rather than allowed to wait indefinitely.

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

How Grokking Software Architecture covers it

How deep it goes
Defines timeouts, distinguishes connection and read phases, and explains threshold selection using monitoring and SLA targets.
Examples
Uses a stressed payment API and a checkout resilience exercise to illustrate timeout choices.
What you can do after
Apply it via Python's requests library using separate connection and read timeout values.
How it connects
Connects timeouts with retries, exponential backoff, circuit breakers, fallbacks, and idempotency.

Also explained in

Compare how 11 books explain timeout →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Software Architecture. The section links point to the parts of the book it draws on.