Connection draining
Taking a server out of service
Section titled “Taking a server out of service”A server has to be taken out now and then: for a new version, for a patch, for a smaller pool. Three questions decide what the clients see.
- Where do new requests go? If the balancer is not told, it keeps sending them until its health checks notice, and the lesson on health checks counted what that costs.
- What happens to the requests in flight? Each is already inside the server. If the server stops, they are cut.
- When is it safe to stop? When nothing is left in it, or when waiting any longer is not worth it.
Connection draining answers all three: send nothing new, let what is in flight finish, then stop, with a limit on the waiting. AWS’s Application Load Balancer calls it deregistration. It stops sending requests to a target that is deregistering, and by default waits 300 seconds, which it says can help requests in flight to complete. The target is shown as draining in that time, and if it has no requests in flight and no active connections, the deregistration completes at once without waiting. If the target ends a connection before the delay is over, the client receives a response in the 500 range. nginx does the same for its own worker processes when its configuration is reloaded: the old ones keep serving their clients, and are shut down after all of them have been served.
Mental model. A server is a room with people in it. Stopping it at once is turning the lights off. Draining is closing the door to newcomers, waiting for those inside to leave, and only then turning off the lights.
Try it: stop, remove, drain
Section titled “Try it: stop, remove, drain”The lab can take its last server out in three ways. Set Traffic to 140, which is 70% utilisation, and set The last server to each of the three deploys in turn. The deploy starts at 30.5 seconds and the new version starts 30 seconds later. Read Failed for round robin.
Requests reach a load balancer, which places each one on one of several servers. Each server has its own waiting line. Choose how the balancer decides, slow one server down, switch on sticky sessions, and make a server fail or be replaced, then compare what every algorithm does with the identical requests.
Each dot stands for many requests. The numbers show the real rate.
Servers saturated: 160 req/s against request capacity of 200 req/s.
Load balancer: the same requests under every algorithm
- Utilisation
- 80%Saturated107% without the last server
- 99th percentile
- 311.8 msone shared queue: 142.4 ms
- Failed requests
- 00% of the requests
Round robin, 160 requests per second on 4 servers, 80% utilised: half of the requests took under 48.3 ms, 95% under 201.1 ms and 99% under 311.8 ms. The busiest server received 25% of the requests, where an even split would give 25%. No request failed. One shared queue, which no balancer can run, would give a 99th percentile of 142.4 ms.
| Server | Share of requests | Busy | Waiting in line | 99th percentile |
|---|---|---|---|---|
| Server 1 | 25% | 81% | 2.1 | 348.4 ms |
| Server 2 | 25% | 83% | 2.3 | 315.8 ms |
| Server 3 | 25% | 82% | 1.7 | 291.1 ms |
| Server 4 | 25% | 81% | 1.8 | 246.3 ms |
| Algorithm | Mean | Median | 95th percentile | 99th percentile | Failed | Busiest server |
|---|---|---|---|---|---|---|
| Round robin · shown above | 68.3 ms | 48.3 ms | 201.1 ms | 311.8 ms | 0 | 25% |
| Random | 103.8 ms | 73.7 ms | 301.9 ms | 433.8 ms | 0 | 25% |
| Least connections | 40.6 ms | 30.1 ms | 113.8 ms | 172.0 ms | 0 | 25% |
| Power of two choices | 48.9 ms | 37.4 ms | 135.0 ms | 193.8 ms | 0 | 25% |
| Consistent hash | 305.4 ms | 74.5 ms | 987.0 ms | 997.4 ms | 2,977 (15.2%) | 39% |
| One shared queue (benchmark) | 36.2 ms | 28.2 ms | 99.0 ms | 142.4 ms | 0 | — |
Times are for the requests that were answered within a second. The last row is a benchmark and not an algorithm: it needs the servers to take work when they are free.
| Component | State | Utilisation | Offered (req/s) | Served (req/s) | Rejected (req/s) | p50 | p95 | p99 |
|---|---|---|---|---|---|---|---|---|
| Clients | Healthy | <1% | 160 | 160 | 0 | 0 ms | 0 ms | 0 ms |
| Load balancer | Healthy | <1% | 160 | 160 | 0 | 0.03 ms | 0.15 ms | 0.23 ms |
| Servers | Saturated | 80% | 160 | 160 | 0 | 27 ms | 95 ms | 138 ms |
- Traffic
- 160 req/s
- Successful
- 160 req/s
- Error rate
- 0%
- Mean latency
- 35 ms
Illustrative values, not benchmarks of any real product.
Model notes
Assumptions
- Each node is one queue, served first-come first-served by several identical slots (instances × concurrency).
- Requests arrive at random at a steady average rate. Each needs an exponentially distributed time on a server, 20 ms on average. Every algorithm in the table gets the identical requests, so the differences between its rows come from the algorithm and not from the luck of the draw.
- Each server works on one request at a time, and has a waiting line of its own that holds up to 100 requests. A request that has not been answered after 1 second has failed, and a server does not work on one that has already given up.
- The balancer knows only how many requests each server holds. It answers instantly, has room for any traffic, and never fails itself.
- Each figure comes from one run of 120 simulated seconds, after 10 seconds of warm-up, with a fixed random seed. The run can be repeated exactly, but it is one run: a difference of a few per cent between two rows is within the noise.
Simplifications
- Above capacity there is no steady state. The model switches to a fluid approximation: the node serves at capacity, its queue grows until it reaches its limit, and further requests are rejected.
- The diagram, and the table of components under it, come from the graph engine: they treat the servers as one pool with one shared queue and never put a request on one server. That is the benchmark in the table, not what any balancing algorithm achieves.
- A server that fails stops answering without a word, so each request sent to it waits for its timeout. A server whose program has stopped but whose machine is up can refuse connections at once instead, and a balancer can try another server. The model shows neither the refusal nor the retry.
- A health check here is a probe that fails only when the server is down. Real probes can also fail on a server that is merely busy, and a balancer can also learn from the requests that fail.
- Sticky sessions: 200 sessions send the sticky share of the traffic, a few of them much more than the rest (a Zipf distribution with exponent 0.8). Each is pinned to one server at random. If its server is taken out, the session is placed again and loses what that server held. Real systems have more sessions, and other distributions.
- Consistent hash gives each server 100 places on a ring and hashes the session, so a session always reaches the same server, whatever the sticky share. It places requests by session and not by load.
- A deploy takes out the last server. The new version starts 30 seconds later and is ready at once. The balancer puts it back at once if it was told about the deploy, and at its next passing check if it was not. Real servers take time to start and warm up.
- Only the first server can be slow, by the factor you set, on every request. Servers of different sizes, or that slow down only under load, are not modelled.
- There is no network time, connection set-up, TLS or keep-alive, and no difference between layer 4 and layer 7: a request is placed on a server, and nothing is said about how.
- The one shared queue is a benchmark, not an algorithm that a balancer can run, because it needs the servers to take work when they are free. It shows how much the choice of server costs.
Parameter values are illustrative. They are not benchmarks of any real product.
| The last server | Round robin, Failed | Of which sent to the stopped server | Of which cut inside it |
|---|---|---|---|
| is stopped for a deploy | 530 | 526 | 4 |
| is removed, then stopped, for a deploy | 4 | 0 | 4 |
| is drained, then stopped, for a deploy | 0 | 0 | 0 |
- Stopped. The balancer is not told, so it finds out by its checks, 14.5 seconds later with the settings of the lab, and sends the server a quarter of the traffic until then. That is 526 requests. Four more were inside the server when it stopped.
- Removed, then stopped. The balancer is told first and sends nothing new, so the 526 disappear. The server is still stopped at once, and the 4 requests it held are cut.
- Drained. The balancer is told first and the server is stopped only when it has finished what it held. The summary says how long that took: 0.10 seconds. Nothing is lost. Least connections shows the same steps, with 75, 2 and 0.
The 4 is Little’s Law at work, from the lesson on it. A server that works on one request at a time and is 70% utilised holds, on average, 0.7 ÷ (1 − 0.7) = 2.3 requests, those being served and those waiting. The run cut 4, which is not the average but is of that size. The model’s server holds a handful of requests because it is small. A real server taking 500 requests a second, each of which takes 200 ms, holds 500 × 0.2 = 100 at any moment, and stopping it at once cuts all 100.
How long to wait
Section titled “How long to wait”Draining has to stop waiting somewhere. The balancer’s limit is a trade: a long wait lets long requests finish and delays the deploy. AWS’s default of 300 seconds is a limit, not the time a drain takes, since a target with nothing in flight is finished at once. But the limit can matter for a rolling deploy, which replaces the servers one after another so that the others keep serving. With 10 servers and a drain that ran to the limit each time, the waiting alone would take 10 × 300 = 3,000 seconds, 50 minutes. The limit should be set from the longest request the service has to complete, and not left at a default that nobody chose. The lab’s requests all end within a second, by its timeout, so its drains are short and cannot show this.
The new version has a similar problem at the other end: it should not receive a full share of traffic the moment it starts. AWS’s slow start mode raises the requests that a new target gets gradually over a period that you set, and a target must pass its first health check before it is sent anything. The lab puts the new server back with a full share.
What happens if the rest cannot carry the traffic?
Section titled “What happens if the rest cannot carry the traffic?”Raise Traffic to 180, which is 90% utilisation, and run the same three deploys. With three servers left, 180 requests are 120% of the 150 they can serve. Round robin fails 1,997, 1,911 and 1,883 requests, in the order stopped, removed and drained: 9.1%, 8.7% and 8.6%. The deploy stopped at once cuts 28 requests, against 4 at 140, because a server at 90% holds more. Telling the balancer saves the requests sent to a stopped server, and draining saves the ones that were cut, and together they are only 114 of the 1,997 failures. The rest fail because three servers cannot serve what four were serving.
Draining deals with cutting requests. It does not create capacity. A rolling deploy needs the spare capacity of the server being replaced, the same condition as for losing a server in the lesson on availability, and the same arithmetic as the one in the health check lesson. A pool at 90% cannot be deployed to without failures, however well it drains.
- A deploy has to settle three things for a server: where new requests go, what happens to those in flight, and when it is safe to stop. Draining sends nothing new, lets what is in flight finish, and then stops, with a limit.
- Stopping a server without telling the balancer cost round robin 530 failed requests at 140 requests a second: 526 sent to it before the checks noticed, and 4 cut inside it.
- Telling the balancer first leaves only the 4 cut. Draining leaves none, and it took 0.10 seconds.
- A server holds its arrival rate times the time a request takes: 500 requests a second at 200 ms is 100 requests, all cut by a sudden stop.
- AWS waits 300 seconds by default, which is a limit and not the time taken. A rolling deploy of 10 servers could wait 3,000 seconds, 50 minutes, if every drain ran to the limit.
- Draining does not create capacity: at 180 requests a second, with three servers left, the deploy fails 1,883 requests even when drained.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.