Remembering Users in a Stateless World
"HTTP is stateless by design — each request is completely independent. The server has no built-in memory of previous requests. Every login, shopping cart, and personalized experience exists because developers explicitly manage state."
CSE 135 — Full Overview | Review Questions
HTTP is stateless — the server forgets you between every single request.
Client-side, server-side, or hybrid — three fundamental options, each with trade-offs.
?q=search&page=2Seven mechanisms — each with different persistence, size limits, and security profiles.
| Mechanism | Location | Persistence | Size Limit | Security |
|---|---|---|---|---|
| URL Parameters | URL (visible) | Per-request only | ~2KB | Visible in logs, history, Referer |
| Hidden Fields | HTML form | Form submission | Unlimited | User can modify via DevTools |
| Cookies | Browser | Configurable | ~4KB per cookie | Sent with every request; HttpOnly option |
| localStorage | Browser | Permanent | ~5MB | JS access only; same-origin policy |
| sessionStorage | Browser | Tab lifetime | ~5MB | JS access only; per-tab isolation |
| Server Sessions | Server | Configurable | Unlimited | Only session ID exposed to client |
| JWT | Client (token) | Token lifetime | ~8KB practical | Signed but not encrypted |
Choosing the right mechanism depends on what you're storing, who needs it, and how long it should live.
Shareable, bookmarkable state
/search?q=web/products?page=3?category=books&sort=priceAutomatically sent to server
Persistent client-side data the server doesn't need — cached API responses, offline data, UI preferences
Temporary, tab-scoped state
Secure user-specific data
Hands-on examples for each state mechanism.
Every state mechanism has attack vectors — know the risks and mitigations.
| Mechanism | Risks | Mitigations |
|---|---|---|
| URL Params | Logged in server logs, history, Referer | Never put sensitive data in URLs |
| Cookies | XSS can steal; CSRF can abuse | HttpOnly, Secure, SameSite |
| localStorage | Any JS can read (XSS) | Never store tokens; sanitize inputs |
| Sessions | Session fixation, hijacking | Regenerate ID on login; use HTTPS |
| Hidden Fields | User can modify via DevTools | Never trust for auth; validate server-side |
| JWT | XSS if in localStorage; hard to revoke | Store in httpOnly cookie; short expiry |
HttpOnly, Secure, and SameSite on session cookies.
A quick decision tree for choosing the right state mechanism.
7 sections of state management fundamentals in one table.
| Concept | Key Points |
|---|---|
| The Fundamental Problem | HTTP is stateless by design. Each request is independent. Statelessness enables scalability but requires explicit state management. |
| Where State Lives | Client-side (URL, cookies, localStorage), server-side (sessions, DB, Redis), or hybrid (cookie holds ID, server holds data). |
| Mechanisms Comparison | 7 mechanisms differ in location, persistence, size limits, and security. No single mechanism fits all use cases. |
| When to Use What | URLs for shareable state. Cookies for server-sent data. localStorage for persistence. sessionStorage for tab-scoped. Server sessions for security. |
| Demos | Hands-on examples for each mechanism. Inspect with DevTools to see state in action. |
| Security | XSS reads localStorage. CSRF abuses cookies. Session fixation steals sessions. Always use HttpOnly, Secure, SameSite. |
| Decision Guide | Start with the question: who needs this data and how sensitive is it? Auth → server sessions. Shareable → URL. Temporary → sessionStorage. |