What it is
Connection management is everything a system does about the lifecycle of network connections: when to open them, how long to keep them, how many to allow, how to reuse them, and when and by whom they are closed. It applies at every hop: browser to edge, load balancer to backend, service to service, application to database or cache.
Why it exists
Connections are not free. Opening one costs round trips (TCP, TLS, sometimes authentication) and CPU (key exchange). Keeping one open costs memory and a slot on both ends: a file descriptor, kernel buffers, often a thread or, for Postgres, a whole backend process. Get it wrong and you pay in latency (handshakes on every call), in capacity (workers waiting on handshakes, slots held by idle connections) and in errors (requests sent on connections the other side just closed).
How it works
Setup cost. A TCP connection needs a handshake (SYN, SYN-ACK, ACK): one round trip before data can flow. TLS 1.3 adds one more round trip (TLS 1.2 added two). So a new HTTPS connection costs two round trips before the first request byte, plus the request’s own round trip. At 40 ms RTT that is 80 ms of setup for a call whose real work might be 15 ms. TLS session resumption and 0-RTT reduce this for repeat connections (0-RTT data can be replayed, so it is limited to safe requests); QUIC (HTTP/3) combines the transport and TLS handshakes into one round trip.
Reuse. HTTP/1.1 keeps connections open by default (keep-alive) but carries one request at a time per connection, so clients keep a pool of connections per destination. HTTP/2 and gRPC multiplex many concurrent requests over one connection. In both cases the setup is paid once and amortised over many requests, but only if the client object (and its pool) is created once and shared, not created per request.
Limits. Every server caps connections: web servers and proxies by configuration and file descriptors, databases by settings such as Postgres max_connections (default 100). Clients are limited too: each outgoing connection to the same destination uses an ephemeral port, and closed connections linger in TIME_WAIT on the side that closed first, so opening and closing connections at a high rate can exhaust ports.
Idle timeouts. Both ends close connections that stay idle too long, and they rarely agree on how long. If the server closes first while the client still considers the connection reusable, a request sent at that moment meets a closing socket and fails with a reset (or a 502 from a proxy). The fix is to make the reusing side (client pool, load balancer) close idle connections before the server does, and to retry idempotent requests once on a fresh connection.
Lifetime. Long-lived connections pin a client to the backend it first reached. After scaling out or a DNS change, old connections keep going to old backends. A maximum connection lifetime (and HTTP/2 GOAWAY on the server side) forces periodic reconnects, so load spreads to new instances.
When to use it
Always, on any path that makes repeated calls to the same destination: service-to-service HTTP and gRPC, database and cache clients, load balancer to backend. It matters most when round trips are long (cross-zone, cross-region, mobile), when TLS is used everywhere (service meshes, zero-trust networks), and when the server has a hard connection cap.
When not to
- One-off or very rare calls, where an idle pool only holds resources.
- Clients behind middleboxes that silently drop idle connections; there, shorter idle timeouts or TCP keepalive probes matter more than long reuse.
- Huge numbers of mostly idle clients (IoT, push): use a server designed for many cheap connections (event-driven, not thread-per-connection) rather than relying on reuse.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| Long idle timeout on the client pool | Fewer handshakes after quiet periods | More idle slots held; risk of racing the server’s close |
| Short idle timeout | Fewer idle connections, no stale sockets | More handshakes and setup latency |
| Larger pool per destination | Fewer waits for a connection | More load and memory on the server; pools multiply with instances |
| HTTP/2 or gRPC multiplexing | Many requests over one connection | One connection pins to one backend; needs per-request balancing or a max lifetime |
| Maximum connection lifetime | Picks up new backends and DNS changes | Periodic reconnect cost |
Alternatives
- A pooler or proxy in the middle (PgBouncer for Postgres, a sidecar proxy in a service mesh): many cheap client connections share a few expensive server connections.
- Multiplexing protocols (HTTP/2, HTTP/3, gRPC) instead of many HTTP/1.1 connections.
- Event-driven servers (epoll/kqueue-based) that keep idle connections cheap compared with one thread per connection.
- Asynchronous messaging, where producers keep one long-lived connection to a broker instead of calling each consumer.
Failure modes
- Client per request: an HTTP or database client created inside the request handler opens a new connection every time; latency and CPU rise, and ports run out under load.
- Idle-timeout race: intermittent resets or 502s after quiet periods, because the server closes idle connections before the client or balancer does.
- Pool starvation by idle connections: a job or a leak holds connections without using them (
idle in transactionin Postgres), others time out waiting, while the database is nearly idle. - Connection storms: after a restart or failover every client reconnects at once, and the handshake CPU load slows the server further. Jittered reconnects and backoff help.
- Pinned connections: new instances get no traffic because existing long-lived connections stay on the old ones.
In production
Create HTTP and database clients once per process and share them. Set the client pool’s idle timeout below the server’s keep-alive timeout (for example 4 s against 5 s; behind a load balancer, the backend’s keep-alive above the balancer’s idle timeout). Give connections a maximum lifetime of minutes, not days. On databases, set an idle-in-transaction timeout and give batch jobs their own small pool. Monitor new connections per second, pool wait time and acquire timeouts, connections by state, TLS handshake rate and resets.
Common misconceptions
- “Keep-alive is on, so connections are reused.” Only if the same client object is reused; a client created per request has its own empty pool.
- “Handshakes only add latency.” They also hold the caller’s worker, thread or connection slot, which costs capacity.
- “Idle connections are free.” They take memory and a slot from a fixed budget on the server.
- “Longer idle timeouts are always better.” Not if the other side closes first; then they cause errors.
Interview angle
When a design has service-to-service calls across zones or regions, mention connection reuse and estimate the setup cost in round trips. For databases, size pools against the server’s connection cap across all instances, and mention a pooler. If asked about intermittent 502s behind a load balancer, explain the idle-timeout mismatch and its fix. Bonus: long-lived HTTP/2 or gRPC connections and how they defeat connection-level load balancing.
Model assumptions
Simplified model, not a benchmark. Handshakes, keep-alive and the idle-timeout race are scripted illustrations: the engine has no TCP or TLS model. API → Payments is a cross-region hop of 20 ms one way (40 ms RTT). In the simulated steps, a new TCP + TLS 1.3 connection per call is modelled by adding its two round trips (80 ms) to that hop; the API has 24 workers with 5 ms of work each and holds a worker while it waits for Payments (40 workers, 15 ms). The ledger database is the engine’s connection pool (16 connections, 25 ms per query, lognormal); connections held idle by the reporting job are modelled as fewer usable connections. Arrivals are random (Poisson). Utilisation is the fraction of busy workers or connections, not CPU. Numbers are illustrative.