Software-Engineering

Sessions, Cookies, LocalStorage & SessionStorage

HTTP is Stateless

HTTP is a stateless protocol, which means each request is independent and the server does not automatically remember the client. To maintain user identity across requests, state management mechanisms are required.


Sessions (Server-Side State)

A session is a server-side mechanism used to remember user identity across multiple HTTP requests.

How Sessions Work

  1. User logs in with credentials
  2. Server creates a session
  3. A unique session ID is generated
  4. Session ID is sent to the browser via a cookie
  5. Browser sends this cookie with every request
  6. Server validates the session ID and processes the request

Key Points

Drawbacks


Cookies

Cookies are small key-value data stored in the browser.

Characteristics

Common Uses

Security Flags


SessionStorage

SessionStorage is temporary browser storage.

Characteristics

Use Cases


LocalStorage

LocalStorage is persistent browser storage.

Characteristics

Drawbacks


Comparison Table

Feature Cookies SessionStorage LocalStorage Sessions
Stored in Browser Browser Browser Server
Sent with request Yes No No Via cookie
Max size ~4KB ~5–10MB ~5–10MB Depends
Persistence Expiry-based Tab lifetime Manual Server-defined
Security High (HttpOnly) Medium Low High

When to Use What