Identity and access
What is a Session?
Definition
A session is state that links a series of requests from the same user, usually identified by a session ID the browser sends back in a cookie.
Also known as: User session, Login session
Session in more detail
Because HTTP is stateless, being logged in has to be reconstructed on every request. The classic arrangement: when you authenticate, the server generates a long random identifier, stores whatever it needs to remember against it, and sends the identifier back as a cookie. Every later request arrives carrying that identifier, the server looks it up, and the request is handled as you.
Where the data lives is the main design decision. Server-side sessions keep the contents in memory, Redis or a database and give the browser only an opaque id — revocation is immediate, and the browser learns nothing. Client-side sessions put signed (sometimes encrypted) data in the cookie itself, which removes the lookup but means invalidating a session before it expires needs extra machinery. In-memory sessions on a single server also break the moment you add a second one, which is why a shared store beats sticky sessions.
Session security is mostly about the identifier. It must have enough entropy that guessing is hopeless, travel only over HTTPS, be hidden from JavaScript with HttpOnly, and — critically — be regenerated at login. Keeping the pre-login id afterwards allows session fixation, where an attacker plants a known id and inherits the session once the victim signs in. Sessions should also expire, both after inactivity and at an absolute limit, and should be invalidated server-side on logout rather than merely dropped from the browser.
Session example
1. POST /login (correct password)
server: id = random_bytes(32)
store[id] = { user_id: 12, created_at: now }
Set-Cookie: session=<id>; HttpOnly; Secure; SameSite=Lax
2. GET /account
Cookie: session=<id>
server: store[id] -> user 12 -> render their account
3. POST /logout
server: delete store[id] <- the session really ends
Set-Cookie: session=; Max-Age=0
Server-side session cookie holds an opaque id, data on server
Client-side session cookie holds signed data, no lookup neededSession — common questions
- What is the difference between a session and a JWT?
- A session is looked up on the server, so it can be revoked at any moment and the browser holds only an id. A JWT is verified by signature with no lookup, which scales across services but stays valid until it expires unless you add a deny-list.
- How long should a session last?
- Long enough to be usable, short enough to limit damage. A common pattern is an idle timeout of minutes to hours plus an absolute maximum of days, with re-authentication required for sensitive actions such as changing a password.
- What is session fixation?
- An attack where the attacker sets or learns a session identifier before you log in, then reuses it afterwards. Generating a brand-new session id at the moment of successful authentication prevents it entirely.
Keep reading
Every definition in the set is indexed A-Z in the developer glossary, and the cheat sheets cover the syntax once you know what you are looking for.
Browse the A-Z glossary