Skip to content
SEE Exam Prep

Question 9 of 11 · Set 2 · 12 marks

Open in ChatGPT

Explain the role of HTTP cookies, sessions, and state management in distributed web applications. Analyze the limitations of maintaining client state across multiple servers.


Introduction

HTTP is stateless: each request arrives with no memory of the previous one. A web application that must remember a logged-in user needs state, and cookies, sessions and server-side storage are the mechanisms that add it back: each with a cost that grows as the number of servers grows.

Cookies

  • A cookie is a name-value pair the server sets with Set-Cookie and the browser returns on every matching request.
  • Attributes: Domain, Path, Expires/Max-Age, Secure, HttpOnly, SameSite.
  • Good for: a small identifier: a session id, a preference, a theme.
  • Limits: 4 KB per cookie, sent on every request, visible to the browser (so not a place for secrets), and exposed to CSRF unless SameSite is set.

Sessions

  • The server keeps the session state and gives the client only a session id in a cookie.
  • Sticky sessions: a load balancer pins a client to one server, so the state stays in that server's memory. Simple, but a server failure loses the session and the balancer becomes stateful.
  • Central session store: session data lives in Redis or a database, so any server can serve any request. This is the standard fix.

State management across servers

Three approaches:

  1. Client-side state: a signed or encrypted token (JWT) carries the claims; the server validates a signature and keeps nothing. Scales perfectly, but cannot be revoked before expiry and grows with the data it carries.
  2. Server-side shared store: a session id plus Redis. Revocable and small on the wire, but adds a network hop and a new single point of failure unless replicated.
  3. Sticky sessions: no shared store, but no failover and unbalanced load.

Limitations of client state across multiple servers

  • Consistency: two servers may see different versions of the same state; a session written on one must be visible on the next.
  • Revocation: a JWT cannot be withdrawn without a blocklist, which reintroduces the shared store.
  • Size: cookies are capped at 4 KB, and every request carries the state.
  • Security: a token stored in localStorage is exposed to XSS; a cookie needs Secure, HttpOnly and SameSite.
  • Clock and expiry: tokens expire by time, so skewed clocks cause spurious logouts.
  • Cost: a shared store on the hot path adds latency to every request and must be as available as the application.

Conclusion

Cookies carry the identifier, sessions hold the state, and the choice is where the state lives: on the client (scales, cannot be revoked), in a shared store (revocable, one more dependency), or on a sticky server (simple, fragile). Most systems combine a short-lived signed token with a small server-side revocation list.