Skip to content

Sticky sessions

By default a balancer treats each request on its own. Some servers keep state in their own memory: a login, a cart, a half-finished upload. A request that reaches another server would find nothing. One answer is to make the balancer send a client back to the same server every time. The lesson on session handling in the scalability module describes the session and the alternatives. This lesson looks at the balancer’s side.

How a balancer does it depends on what it can see. A layer 7 balancer can use a cookie: AWS’s Application Load Balancer, with stickiness switched on, binds a user’s session to a target by a cookie that it generates, or one that the application sets, for a duration. After the first request, later requests bypass the routing algorithm and go to the same target. A balancer can also hash something that the client already carries. nginx’s ip_hash hashes the client address, and HAProxy’s balance source keeps a client on a server by its IP address. A layer 4 balancer that hashes the connection, as AWS’s Network Load Balancer does for TCP, keeps each connection on one target for its whole life, which is a pinning of its own.

Mental model. A sticky session turns the algorithm off for that client. The balancer chooses once, and every later request is already decided.

The lab pins a share of the requests. The requests come from 200 sessions, and a few of them send much more than the rest: the heaviest session sends 10% of the sticky traffic, the 10 heaviest send 36% and the 50 heaviest 65%. Each session is pinned at random to one server. The remaining requests are placed by the algorithm in the usual way.

Set Traffic to 120, which is 60% utilisation, and move Sticky sessions from 0% to 50% and then to 100%.

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.

160 req/s
Servers
4
ClientsHealthy<1%Load balancerHealthy<1%ServersSaturated80%
ClientsHealthy<1%Load balancerHealthy<1%ServersSaturated80%

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

The balancer
1×
0%
Health checks
5 s
3
During the run
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.

Round robin: what each server did
ServerShare of requestsBusyWaiting in line99th percentile
Server 125%81%2.1348.4 ms
Server 225%83%2.3315.8 ms
Server 325%82%1.7291.1 ms
Server 425%81%1.8246.3 ms
The same requests, under every algorithm
AlgorithmMeanMedian95th percentile99th percentileFailedBusiest server
Round robin · shown above68.3 ms48.3 ms201.1 ms311.8 ms025%
Random103.8 ms73.7 ms301.9 ms433.8 ms025%
Least connections40.6 ms30.1 ms113.8 ms172.0 ms025%
Power of two choices48.9 ms37.4 ms135.0 ms193.8 ms025%
Consistent hash305.4 ms74.5 ms987.0 ms997.4 ms2,977 (15.2%)39%
One shared queue (benchmark)36.2 ms28.2 ms99.0 ms142.4 ms0—

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.

0 ms
Metrics
ComponentStateUtilisationOffered (req/s)Served (req/s)Rejected (req/s)p50p95p99
ClientsHealthy<1%16016000 ms0 ms0 ms
Load balancerHealthy<1%16016000.03 ms0.15 ms0.23 ms
ServersSaturated80%160160027 ms95 ms138 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.

Sticky sessions Round robin, Mean Round robin, 99th percentile Least connections, Mean Round robin, busiest server
0% 34.8 ms 150.8 ms 25.9 ms 25%
50% 44.2 ms 191.1 ms 32.9 ms 29%
100% 55.4 ms 238.0 ms 55.4 ms 32%

Three things show. First, the latency rises with the share that is pinned: round robin’s mean goes from 34.8 ms to 55.4 ms, 1.6 times as long, with no change in the traffic. Second, the gap between the algorithms closes. At 100% round robin, random, least connections and two choices all give 55.4 ms, and the same 99th percentile of 238.0 ms, because none of them chooses any more. The one shared queue of the benchmark is unaffected, at 23.6 ms. Third, the servers are no longer equally loaded. The busiest receives 32% of the requests, and the others 26%, 21% and 22%. Read the table of servers: they hold 46, 58, 47 and 49 sessions, and the one with the fewest, 46, is the busiest, because the heavy sessions are not spread evenly. The number of sessions a server holds is not its load.

The imbalance is the cost, and it is not an accident of this lab. A balancer that chooses with a count would have steered the next request to the least loaded server. A pinned request cannot be steered, and a pinned client who sends a lot makes its server hot for as long as the pin lasts.

Set Traffic to 100, keep Sticky sessions at 100%, and set The last server to “fails”. With no pinning, round robin failed 385 requests in the lesson on health checks, and least connections failed 38, because it steered away from the server that held its requests. Now every algorithm fails the same 336 requests, 2.7% of them. A pinned request goes to its server whatever the count says, so until the check takes the server out, at 45 seconds, nothing steers requests away from it.

Then the sessions of that server have to go somewhere. In the lab 49 of the 200 sessions are placed again, on the servers that are left. AWS describes the same step: if the cookie refers to a target that is unhealthy or was deregistered, the balancer selects a new target and updates the cookie. The new server holds none of what the old one held, so each of those sessions starts again from nothing. Whether that means signing in again or losing a cart depends on the application, and it is the cost of keeping state in a server that can fail.

Now set The last server to “is drained, then stopped, for a deploy”. The balancer stops sending to the last server at 30.5 seconds, drains it, and puts the new version in rotation at 60.5 seconds. With Sticky sessions at 0% the new server takes its quarter again. With 100% it takes nothing: every session has been pinned to another server, and a new server gets no sessions, so it receives 4% of the requests in the run, only the ones from before the deploy, while the first server receives 39%. AWS’s documentation says, for application-based stickiness, that a session that has moved to a new target stays with it even if the failed target comes back, and it warns that when the number of targets increases considerably, stickiness can leave the load unevenly spread. It describes setting a short expiry on the cookie as a way to rebalance by hand.

So sticky sessions have three costs: the algorithm no longer balances the pinned share, the sessions of a server that goes away go with it, and a server that is added or comes back is left empty. If the reason for pinning is state in the server, the way out is to move the state out, into a shared store or a token, as the session handling lesson describes. The balancer is then free again to send each request where the lines are shortest.

  • A sticky session pins a client to one server, with a cookie, a hash of the client’s address, or a connection. The algorithm chooses once, and every later request is already decided.
  • At 120 requests a second, round robin’s mean goes from 34.8 ms with no pinning to 44.2 ms at 50% and 55.4 ms at 100%, and the busiest server goes from 25% of the requests to 32%.
  • At 100% pinned, every algorithm gives the same figures: the choice has been made by the pin.
  • When a pinned server fails, every algorithm fails the same 336 requests until the check removes it, and the 49 sessions on it are placed again and lose what the server held.
  • A new or restarted server receives nothing from pinned clients: after a drain at 100% it handles 4% of the requests, against 39% for the busiest.
  • The way out is to keep the state outside the server, so that nothing needs pinning.

Next: consistent hashing, which keeps a key on a server without a table.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.