Sticky Sessions
Sticky sessions are a load balancer setting that keeps one user on the same server. Every request from that user goes to the server the balancer first chose for them.
When the next request lands elsewhere
The basket of the user sits on server A. Round robin sends the next request to server B, and server B prints an empty basket.
servers = {"A": ["milk", "bread"], "B": []}
turns = ["A", "B"]
for i in range(2):
server = turns[i]
print("request", i + 1, "goes to server", server, "| basket:", servers[server])The sticky fix
A mapping pairs the user with server A. Every request follows the mapping, so the basket prints correctly each time.
servers = {"A": ["milk", "bread"], "B": []}
mapping = {"Ana": "A"}
for i in range(3):
server = mapping["Ana"]
print("request", i + 1, "goes to server", server, "| basket:", servers[server])Two users, two home servers
Each user maps to a different server. Each user keeps seeing their own basket, and the baskets stay separate.
servers = {"A": ["milk"], "B": ["shoes"]}
mapping = {"Ana": "A", "Ben": "B"}
for user in ["Ana", "Ben", "Ana", "Ben"]:
server = mapping[user]
print(user, "goes to server", server, "| basket:", servers[server])Example
turn_servers = {"A": [], "B": []}
turns = ["A", "B"]
items = ["milk", "bread"]
for i, item in enumerate(items):
turn_servers[turns[i]].append(item)
print("in turn, server A basket:", turn_servers["A"])
print("in turn, server B basket:", turn_servers["B"])
sticky_servers = {"A": [], "B": []}
mapping = {"Ana": "A"}
for item in items:
sticky_servers[mapping["Ana"]].append(item)
print("sticky, server A basket:", sticky_servers["A"])
print("sticky, server B basket:", sticky_servers["B"])Key points
- Session: a run of related requests from one user, such as building one basket.
- Sticky sessions: a balancer setting that sends one user to the same server every time.
- Mapping: the balancer record that pairs a user with their server.
In this example, the same two additions run twice, once in turn and once with a sticky mapping. The turn run splits the basket across two servers, and the sticky run keeps the whole basket on one server.
Note
sticky sessions help most when the server itself holds the state of the user.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
A shop keeps each basket in server memory, and shoppers keep seeing empty baskets. What should the balancer do?