Session handling
What a session is
Section titled “What a session is”HTTP treats every request as independent, yet a user expects to stay signed in and to find the cart where they left it. A session is the state that ties one user’s requests together.
RFC 6265 describes the mechanism that makes it possible. A server sends a Set-Cookie header, the browser returns the cookie in a Cookie header on later requests, and so, in the RFC’s words, servers can maintain a stateful session over the mostly stateless HTTP protocol. The RFC’s own example is a session identifier named SID.
What the cookie says is up to the server, and that is the design decision. The session’s data can be in one of three places.
Mental model. The session is in one instance, and the load balancer must always send the user back to it. Or it is in a store that every instance can read, and the cookie is only a key. Or it is in the token the user carries. Each choice moves the difficulty somewhere else.
Sticky sessions: the data in one instance
Section titled “Sticky sessions: the data in one instance”With sticky sessions, also called session affinity, the data stays in the memory of the instance that created it, and the load balancer sends the user back to that instance. AWS’s Application Load Balancer describes it as binding a user’s session to a specific target, which is useful for servers that maintain state information. It does this with a cookie, and the stickiness duration can be set from 1 second to 7 days.
The costs are those of state in an instance.
- A failure ends the session. If the cookie refers to a target that was deregistered or is unhealthy, the load balancer selects a new target. The user is routed, but the new instance has no session for them.
- Scaling in ends sessions. Every instance that is removed takes its users’ sessions with it.
- Scaling out may not help. AWS’s documentation warns that when the number of targets rises considerably, stickiness can leave the load unevenly spread, because the existing users are tied to the old targets, and it describes ways to rebalance by hand.
The Twelve-Factor App is blunt: sticky sessions are a violation of twelve-factor and should never be used or relied upon. They are sometimes the practical choice for software that was written before scaling out mattered. They are a cost to know about, not a design to start from.
A shared session store
Section titled “A shared session store”The data lives in a store that every instance reads, and the cookie holds only an identifier. RFC 6265 describes this practice: servers typically store a nonce, or session identifier, in the cookie and use it as a lookup key for state kept on the server, which limits the damage if someone reads the cookie. OWASP says the same: the session identifier must be only an identifier on the client side, and what it means must be stored on the server.
The Twelve-Factor App names the kind of store: a datastore that offers time-expiration, such as Memcached or Redis.
What the design buys is that instances become interchangeable. A removed instance holds no sessions, any instance can serve any request, and ending a session takes one delete in the store.
What it costs is the store. Every request needs a lookup, so the store is a dependency of every request and has to be fast, available, and large enough for every active session. That last part can be sized.
How big is the store?
Section titled “How big is the store?”Little’s Law says that the number of items in a system is the arrival rate times the time each stays. For sessions, the items are the sessions held, the arrival rate is the rate at which sessions begin, and the time is how long each is kept.
OWASP gives an idle timeout of 15 to 30 minutes for low-risk applications, against 2 to 5 minutes for high-value ones. Take 30 minutes, and 50 new sessions per second.
- Sessions held: 50 × 1,800 seconds = 90,000.
- At an illustrative 2 kB each: 90,000 × 2 kB = 180 MB.
The size of a session is yours to measure. The arithmetic is the point: the store holds the arrival rate times the timeout, so halving the timeout halves the store.
A signed token
Section titled “A signed token”The data travels with each request. RFC 7519 defines the JSON Web Token as a compact, URL-safe means of representing claims to be transferred between two parties. The claims can be digitally signed, so that any instance can check them, or encrypted, or both. The server keeps nothing per user, and there is no lookup.
The costs are different.
- Ending a session early is hard. A token is valid until it expires. OWASP’s cheat sheet says that an application using tokens for sessions needs a way to invalidate them, a deny list for example, and that an application that keeps one no longer has completely stateless sessions.
- The data is readable unless the token is encrypted. RFC 7519 warns that a token may contain privacy-sensitive information, and names an encrypted token as one way to keep it from being disclosed. A signature shows that the data has not been changed. It does not hide it.
- The token travels with every request, so what it holds adds to every request’s size.
Compared
Section titled “Compared”| Sticky sessions | Shared store | Signed token | |
|---|---|---|---|
| Where the data is | One instance’s memory | A store every instance reads | The token the client holds |
| An instance is removed | Its users lose their sessions | Nothing is lost | Nothing is lost |
| What the balancer must do | Route by cookie | Nothing special | Nothing special |
| Cost per request | None extra | A lookup in the store | Checking a signature |
| Ending a session at once | Delete it in that instance | One delete | A deny list, which is state |
Try it: where does the session live?
Section titled “Try it: where does the session live?”Nine statements, each true of one design. Put each under the design it describes.
Challenge
Where does the session live?
A session ties a user's requests together. It can live in the memory of one instance, with sticky sessions sending the user back to it; in a store that every instance shares, with the cookie holding only an identifier; or in a signed token that travels with every request. Put each statement under the design it describes.
Sorting needs JavaScript.
Categories
- Sticky sessions
- The session is in one instance's memory, and the user is sent back to that instance.
- Shared session store
- The cookie holds an identifier. The data is in a store every instance reads.
- Signed token
- The data travels with each request, and the server checks the signature.
Check your answers at least once before looking at the answers.
What happens if the autoscaler removes 10 of 14 instances?
Section titled “What happens if the autoscaler removes 10 of 14 instances?”In the autoscaling lab of the next lesson, the group runs 14 instances during a spike and is cut back to 4 when it is over. Suppose the sessions are sticky and held in each instance’s memory, and the users were spread evenly.
The 10 instances that are removed held 10 ÷ 14 = 71.4% of the sessions. The load balancer sends those users to the 4 that remain, but those instances have never heard of them, so about 71% of the signed-in users must sign in again, just after the busiest moment of the day. AWS’s deregistration delay, 300 seconds by default, gives requests that are in flight time to finish. It does not wait for sessions, which can last much longer.
With a shared store, the same scale-in loses no session. The 4 instances read the sessions from the store, which, if every request does one lookup, now serves 400 lookups per second instead of 1,600.
- A session is the state that ties a user’s requests together. A cookie makes it possible, and the design decides where the data is.
- Sticky sessions keep it in one instance: the load balancer must route by cookie, a removed instance takes its users’ sessions, and the Twelve-Factor App says never to rely on them.
- A shared store keeps it where every instance can read it: instances stay interchangeable, at the price of a lookup per request and a store to size and keep available.
- Sizing the store is Little’s Law: 50 sessions per second kept for 30 minutes is 90,000 sessions, 180 MB at 2 kB each.
- A signed token carries the data, so there is no lookup, but ending a session early takes a deny list, which is state again, and the data is readable unless encrypted.
- Cutting 14 instances to 4 loses 10 ÷ 14 = 71.4% of sticky sessions held in memory, and none held in a shared store.
Next: autoscaling, and adding instances without being asked.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.