keepcalculator

06 Numbers and Powers

Timeouts: Three Numbers Per Service Call?

Card No. 06·30187 · Updated · Standard formula, shown below

No, timeouts are not a contract of three numbers per service call. A timeout is a single duration value that defines how long a client will wait for a response before giving up. The confusion arises because many systems configure timeouts in three separate layers (connection, read, and write), but each is an independent setting, not a single contractual trio.

Background: Why the “Three Numbers” Idea Exists

The misconception likely comes from popular HTTP client libraries and microservice frameworks that expose three timeout knobs:

  • Connection timeout – time allowed to establish a TCP connection (e.g., 5 seconds).
  • Read (or socket) timeout – time allowed between data packets once connected (e.g., 30 seconds).
  • Write (or request) timeout – time allowed to send the full request body (e.g., 10 seconds).

These are often configured together, leading people to think of them as a single “timeout contract.” But they are separate parameters that control different phases of a network call. A service call may also have an overall deadline (like a gRPC deadline) that supersedes all three.

How Timeouts Actually Work in Network Calls

When a client makes a service call, it goes through several stages:

  1. DNS resolution (often not covered by typical timeouts).
  2. TCP handshake – governed by the connection timeout.
  3. Sending the request – governed by the write timeout.
  4. Waiting for the response – governed by the read timeout.

Each stage can fail independently. A connection timeout of 5 seconds means if the TCP handshake doesn’t complete in 5 seconds, the call fails. A read timeout of 30 seconds means once connected, if no data arrives for 30 seconds, the call fails. These are not combined into a single 35-second limit; they are separate thresholds.

Choosing Timeout Values: What to Consider

Picking the right timeout is a balance between user patience and server capability. Key factors:

  • Expected latency – measure the 95th or 99th percentile response time of your service.
  • Retry budget – if you retry on timeout, the total time multiplies. A 1-second timeout with 3 retries can take 4 seconds worst-case.
  • Dependency SLAs – if a downstream service promises a 500ms response, your timeout should be slightly above that, say 800ms.
  • Queueing – if the server queues requests, a short read timeout may fail before the server even starts processing.

Common practice: set connection timeout low (1–5s), read timeout moderate (10–30s), and write timeout similar to read. For synchronous user-facing calls, keep total under 10 seconds; for background jobs, longer is acceptable.

Common Problems and Misconceptions

One frequent mistake is setting a read timeout that is too short for streaming responses. A server sending a large payload may pause between chunks; a 30-second read timeout would kill the call even though the server is healthy. Another issue is assuming timeouts are a contract: they are client-side limits, not server guarantees. The server may take longer and still respond, but the client has already given up.

Another misconception is that a timeout of 0 means “no timeout.” In some libraries (like Java’s HttpClient), a timeout of 0 is interpreted as infinite, but in others it may mean immediate failure. Always check your library’s documentation.

Comparison: Timeout vs. Deadline vs. Cancel

These terms are related but distinct:

  • Timeout – a duration after which the client gives up.
  • Deadline – an absolute point in time (e.g., 2025-01-01T12:00:00Z). gRPC uses deadlines, not timeouts.
  • Cancel – an explicit signal from the client to abort the call, often via a context or cancellation token.

In distributed systems, a deadline is often more useful because it accounts for time spent retrying or queueing. For example, a 5-second deadline means the entire call (including retries) must finish by that time.

Costs and Risks of Poor Timeout Configuration

Setting timeouts too high can tie up threads and connections, leading to resource exhaustion. Too low, and you’ll see spurious failures during normal slow periods. In microservices, cascading timeouts can cause a single slow service to trigger a wave of failures across the system. This is why circuit breakers and bulkheads are used alongside timeouts.

There is also a monetary cost in cloud environments: a hung call that eventually times out still consumes network egress and possibly server CPU. Proper timeouts reduce wasted resources.

Alternatives to Fixed Timeouts

Instead of a static timeout, consider:

  • Adaptive timeouts – based on recent latency percentiles, automatically adjust.
  • Client-side deadlines – as mentioned, an absolute time budget.
  • Circuit breakers – fail fast after a threshold of errors, without waiting for a timeout.
  • Retry with backoff – combine a short timeout with exponential backoff for transient failures.

These are not replacements but complements. A circuit breaker won’t help if the service is just slow; a timeout is still needed.

Frequently Asked Questions

What is the difference between connection and read timeout?

Connection timeout is the time to establish a TCP connection (including SSL handshake). Read timeout is the time to receive a response after the connection is established. A connection can succeed in 100ms, but the server may take 5 seconds to respond; the read timeout controls that wait.

Should I set all three timeouts to the same value?

Not necessarily. Connection timeouts should be short (1–5s) because network handshakes are fast. Read timeouts can be longer (10–30s) to allow for server processing. Write timeouts are rarely an issue unless you’re uploading large payloads. Use different values based on the phase.

What happens if a timeout is set to 0?

It depends on the library. In many languages, 0 means no timeout (infinite), but in some, it may cause an immediate timeout. Always check the documentation. For safety, use a large number like 86400000 (24h) instead of 0 if you want to effectively disable it.

How do I test if my timeout is working correctly?

Simulate a slow server by adding a sleep in your endpoint. Set a short timeout (e.g., 1 second) and verify the client fails after 1 second. Also test with a server that accepts the connection but never sends data to confirm the read timeout fires.

Can a timeout be too short for a legitimate request?

Yes. If your service occasionally takes 2 seconds but your timeout is 1 second, you’ll get false failures. Use the 99th percentile latency as a baseline, not the average. Also account for network jitter.

Is a timeout a contract between client and server?

No. A timeout is purely client-side. The server may have its own request timeout (e.g., in a web server), but that is a separate setting. The client’s timeout does not tell the server to cancel the work; it just stops the client from waiting.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →