# Connection timeout

## What it is

A connection timeout limits how long an operation may spend establishing a
usable connection. A client can need to resolve an address, open a transport
connection and complete a security handshake before it can send an application
request. Different tools assign those steps to different limits. Check the
implementation's definition before interpreting a setting or comparing results.

The time for establishing a connection is only one part of an operation's
budget. There can also be time spent waiting for a free connection in a pool,
sending the request, waiting for the first response bytes and reading the
remaining response. A service can define limits for those steps and a deadline
for the entire operation. A timeout record becomes more useful when it
identifies the phase that ran out of time.

Some clients work with persistent connections. A later request can reuse a live
connection and skip some or all of its initial setup. As a result, a connection
timeout observed during startup can describe a different path from the time
spent serving ordinary traffic. A representative check should cover both new
connections and reuse.

## Why it matters

An unbounded wait can tie up resources and make users wait long after the
product could give them a useful answer. A timeout puts a limit on an operation
during a defined phase. A very small value can also reject viable connections
during typical network variance. Choose the limit from the operation's total
budget, the cost of a failed attempt and what users can realistically do if it
fails.

The position of the timeout affects what can safely be repeated. When contact
was never established, a caller has different evidence from a caller that sent
the complete request and lost the reply. A later retry could duplicate a durable
effect. Track when the request was sent and use an operation identity where the
outcome can be uncertain. Schedule repetitions within the original deadline,
leaving time for the caller to see a meaningful result.

Timed observations help find the bottleneck. Failed address resolution, a
stalled connection and an upstream that accepts a connection but takes a long
time to reply are different problems. Before changing a timeout, record which
phase failed and what happened to other requests using the same route. A longer
value can consume more connection slots while leaving the actual cause
untouched.

## What it looks like in practice

Consider an application calling an external notification service. Start with a
usable budget for the full operation and allocate a part for new connections.
Measure normal connection setup from the real deployment environment. Exercise a
request using a fresh connection, a reused connection and a deliberately slow
upstream reply. Confirm which limits apply and which error the caller receives.
When a request fails, record whether any bytes were sent before the deadline.

Include the retry policy in the one experiment. If the first attempt consumes
most of the available time, the next attempt has only the remaining budget. Use
a bounded number of attempts and measure the pooled connections and active
requests while an upstream is slow. See whether the team can tell when a
connection is unavailable and when an upstream application is taking longer than
the product can wait.

Engineer's
[email delivery case study](/portfolio/built-email-with-failover-and-delivery-logging/)
records a timeout on the send path, along with provider failover and evidence
about message delivery. Those features give a concrete context for classifying
the failure. The notification example here makes connection setup and later
response time separate measurements. This distinction helps an operator decide
whether a failure concerned contact with a provider or the result of a request
it already received.

Plan an exercise for a service that has several possible addresses or endpoints.
A client may divide the budget among attempts or try them according to its own
connection strategy. Record which endpoint was tried and the deadline applied.
These details make the result explainable when a service is reachable by one
path while another path remains slow.

## Where it goes wrong

A project can copy a timeout value from another tool and assume the same phases
are covered. Read how the input is defined. For example, a client may include
DNS and negotiation in its period for establishing a connection, while a reverse
proxy describes an upstream-specific connection step. Test the real
implementation and document the boundary with the configured value.

Pooling can also obscure a problem. A healthy warm connection hides a slow
establishment path until the pool turns over. Test a fresh process, a normal
reused connection and recovery after an idle period. Measure what happens when
many clients need new connections at the same time. A client can spend more time
waiting for its pool than it spends making a single connection.

A useful diagnostic set includes a timestamp for the start of a request, the
start of connection acquisition and the first observation after connection. Add
a separate field for receipt of the first response byte. When a recorded
timestamp is unavailable, preserve the reason or phase that could be observed.
With those readings, a release test can tell whether a new client began waiting
earlier or stayed longer in one particular step.

Finally, name the effect a deadline has on the next action. The service might
return a clear error, queue the request for a later attempt or report an
uncertain outcome. The useful timeout is one whose trigger, observed phase and
recovery behaviour can all be reproduced. Include the trigger, phase and
expected recovery behaviour in the operational record so responders can match it
to a real failure.

## Further reading

- [libcurl CURLOPT_CONNECTTIMEOUT](https://curl.se/libcurl/c/CURLOPT_CONNECTTIMEOUT.html).
  Describes the time used for the connection phase in libcurl.
- [nginx upstream connection timeout](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_connect_timeout).
  Documents connection setup for a proxied request.
- [Go net.Dialer](https://pkg.go.dev/net#Dialer). Documents deadline and timeout
  behaviour for network dialing.

<https://engineer.company/glossary/connection-timeout/>
