Where the shopping cart lives
After this: Say why a cart kept in one server's memory goes missing, and fix it with a shared session store.
On this page · 6 sections · 3 checks
Ana shops on a site with three servers behind a load balancer. She adds a book to her cart, then a pen. Then she opens her cart, and it is empty. Every server ran the same code, and every request worked. Where did her things go?
Data a server remembers
When Ana added the book, the server that answered saved it in its own memory, in a dictionary (a map in Java). That kind of data, remembered by the server from one request to the next, is called . The cart, together with who is logged in, belongs to one user's visit, and is called that user's .
Follow Ana's three requests under round robin:
- Add the book. Request 1 goes to Server A, which keeps the book in its own memory.
- Add the pen. Request 2 goes to Server B, which keeps the pen in its own memory.
- View the cart. Request 3 goes to Server C, which has never seen Ana's cart. It shows 0 items.
Each server holds a different piece of the cart, and no server holds all of it.
Keep the state somewhere shared
The fix is to keep nothing on the servers. Each server reads the session from one shared place, changes it there, and forgets it once it has answered. A server that keeps nothing between requests is , and the shared place is a . It is often a separate fast store that keeps values under keys, like a dictionary on its own machine (Redis is a well-known one), or a table in the database.
Run the same three requests with a shared store:
- Server A writes the book to the store.
- Server B writes the pen to the store.
- Server C reads the store and shows 2 items.
- Sessions · Server memoryRun it with the cart in each server's memory. Step through and watch which server keeps the book, which keeps the pen, and which one answers when Ana views her cart. Then switch the policy to Shared store and run it again.
Press Run it, then step through and watch the requests move.
Finding the right cart
How does Server C know which cart in the store is Ana's? When her visit starts, the site gives her browser a short random code, a . The browser keeps it in a and sends it with every request. The store keeps each cart under its session id, like a dictionary, so any server can look it up.
What stateless servers give you
Stateless servers are what lessons 1 to 3 needed without saying so. Any server can answer any request, so the load balancer can send a request anywhere. A server can crash without taking anyone's cart with it, and a new server can join without copying anything.
The weaker fix: sticky sessions
There is another way to keep the cart together. The load balancer can send all of one user's requests to the same server, every time. This is called . Ana's book and pen would both land on A, and the cart would show 2 items.
It works until A dies. Then Ana's requests go to another server, and her cart is gone, because it lived only in A's memory. Sticky sessions can also make the sharing uneven: a server stuck with several very busy users cannot pass them on.
Check yourself
Four designs for the shop's servers. Which one is stateless?
Carts live in each server's memory again, and the load balancer uses round robin over A, B and C, starting at A. Ana adds a book, then a pen, then a cup, and then views her cart. What does she see?
The shop uses sticky sessions: all of Ana's requests go to Server B, which keeps her cart in its memory. Server B crashes. What happens to Ana's next request?
- Say what state and a session are: data a server remembers between requests, such as a user's cart or who is logged in.
- Explain why a cart kept in each server's memory goes missing under round robin: each server holds a different piece of it.
- Make servers stateless by keeping sessions in a shared session store, found by the session id the browser sends in a cookie.
- Say why sticky sessions are the weaker fix: the cart is lost when that server dies, and the sharing can become uneven.