Core Concepts
Load Balancing
Choose which server takes new work, and keep requests or connections working as the fleet changes.
A load balancer routes new requests or connections among several copies of a service. It assigns work according to a routing rule and which copies are ready. At a planned stop, the balancer can route new assignments elsewhere while existing work has time to finish.
Choose an eligible target
When the shortener's edge server receives GET /a7Kp2Qx, the request's host, method, and path can identify the redirect service. The balancer then chooses among that service's registered instances, called targets. Request Path follows the earlier steps, including getting the edge server's address.
Suppose API A and API B are ready. A has eight unfinished requests and B has two. API C is starting. The balancer skips C, then applies a policy to the remaining targets.
| Policy | What it chooses | When to try it |
|---|---|---|
| Round robin | The next eligible target in a rotation | Instances have similar capacity and the operations cost roughly the same. |
| Least outstanding requests | An eligible target with fewer requests still in progress | Some requests stay busy longer than others, and unfinished requests track that difference. |
| Least connections | An eligible target with fewer active connections | Open connections consume a resource that matters, such as gateway memory. |
| Weighted distribution | A larger share of new work for a target with a higher weight | Measurements show that some targets can sustain more of this workload. |
In our example, round robin can send the next request to A. Least outstanding requests chooses B from the same snapshot. If Redirect API A can sustain twice as many requests per second as B at the same latency target, a 2:1 weight can give it about twice as much new work over time. Measure whether that share stays within both targets' capacity.
A count is a signal for a particular kind of demand. Eight quick cache hits can cost less than two requests blocked on the database. Even a sensible target policy leaves a shared database bottleneck in place.
NGINX routing policies · AWS least outstanding requests and other routing algorithms
Check readiness and drain before a planned stop
A health check is a probe the balancer or its control system uses to judge a target. A readiness check asks if the target can take work now. API C may have a running process but still be loading configuration. When it becomes ready it can join the pool. In Kubernetes, for example, failing readiness changes which endpoints the service publishes as ready. Allow for that change to reach the component routing traffic.
A check can pass shortly before a server fails. Track real request failures and response times too. If every target depends on the same unavailable database, the way the checks handle that dependency can remove the whole pool. Decide what the balancer and application do when no target can serve and test that case.
For a planned deployment, start draining API A. In this request-aware HTTP balancer, wait for new requests to stop routing to A. Let work that already reached A finish or run to a deadline. In the example, A is already serving request R42. It can finish R42 while the balancer sends the next request, R43, to B. If A suddenly loses power, R42 may fail because the server can no longer finish it.
Check the remaining targets' capacity before removing A. A drain can also leave long-lived connections open past a deployment budget. With a connection-level balancer, stopping new TCP connections can leave open connections able to carry more requests. Coordinate application draining and set a deadline for clients to reconnect.
Kubernetes readiness probes · AWS target draining
What gets assigned when a connection stays open?
A connection-level balancer can select one target when a new Transmission Control Protocol (TCP) connection opens. Traffic on that connection follows the chosen path. An HTTP request-level balancer can read each HTTP request and select a target for it. Two short-link requests can reach different API instances even when the browser reuses its connection to the proxy, provided routing policy permits it.
A WebSocket carries live messages over a lasting connection. In Online Chess, the WebSocket gateway keeps that connection, then uses the game ID to find the process that owns the board. The balancer's choice of gateway and the application's choice of game owner answer different questions. A new gateway can route to the same game owner after a reconnect.
Adding gateways creates room for new connections. Already upgraded WebSockets keep their established gateway until they close or reconnect. Equal connection counts can still hide skew, or uneven load. One server can have mostly idle sockets while another pushes updates to active spectators. Monitor open connections, messages and bytes sent, and time spent waiting to write to a client.
AWS connection-level routing · WebSocket connections
Keep a session usable after a target changes
Session affinity, also called stickiness, sends new requests from a session to a preferred target while it is eligible. A cookie can carry that preference. This can preserve the value of data kept in one process. If the process goes away, a different target needs another source for any durable session state. Affinity can also concentrate work when one session or routing key draws much more traffic than the rest.
The shortener's redirect servers can look up the same mapping in shared stores. It can spread new requests across eligible instances without remembering which machine handled the last redirect.
For a live reply, consider the ChatGPT walkthrough. The browser applied event 1700-2 for turn 17 through Gateway A before the connection closed. The worker adds event 1700-3 while the browser is disconnected. After reconnecting, the balancer selects Gateway B. The browser supplies the turn and last event ID, which acts as its position in the reply. While the shared log is available, B reads the next event, 1700-3, and sends it over the new connection. The old connection ends, but the state needed to continue still exists.
If the shared log no longer supports replay, the walkthrough can fall back to the saved reply once the turn ends. That is an application decision. The balancer can choose a target for the new connection. The target decides what it can recover and tell the client.
See also
- Request Path - follow the address lookup and edge route before a target receives the request.
- Scalability - choose which resource needs more capacity.
- Availability - check what happens to the remaining load when a target disappears.
- Consistent Hashing - limit ownership changes when balancing by a key and the target set changes.
Used in
- URL Shortener - redirect requests can go to any healthy API instance that can look up the code.
- Online Chess - a live connection reaches a WebSocket gateway, then a game ID identifies the separate owner of the board.
- ChatGPT - a reconnect can reach a different gateway and read later approved reply events from the shared log.
Done reading? Mark it so it sticks in your dashboard.