Cookie-Based Sessions: Server-Side State, Client-Side ID

Think of a session cookie as a coat check ticket, not the coat itself. The server stores your data and gives you a unique ID to carry in a cookie. This is how Express.js tracks user state across requests.
Why it exists
HTTP is a stateless protocol, meaning each request is independent and has no memory of past requests. For features like user logins or shopping carts, an application needs a way to remember a user across multiple page loads. Cookie-based sessions solve this by creating a persistent identity for a user's browser.
The mental model
A session managed by Express.js is like a coat check. The server is the coat check room, holding all the user's data (the "coat"). The user's browser just holds a small, unique ticket (the session ID cookie). When the user makes a new request, they present the ticket, and the server retrieves their specific data. The actual session data never leaves the server; only the identifier does.
How it works
When a user first interacts with a site using express-session, the middleware generates a unique session ID. It creates a session object on the server (in a session store) and sends the session ID to the browser via a Set-Cookie header. On every subsequent request from that browser, the cookie is automatically sent back. The middleware reads the ID, retrieves the corresponding session data from its store, and attaches it to the request object, typically as req.session.
When to use it
Use this for managing any state that is specific to a user and needs to persist across multiple requests. It's the standard for implementing user authentication, shopping carts, and multi-step forms in Express. It's secure because sensitive session data remains on the server.
When not to use it
Avoid this pattern for fully stateless APIs that rely on tokens (like JWTs) in headers for authorization. The biggest footgun is using the default server-side session storage, MemoryStore, in a production environment. It is not designed for production, will leak memory, and does not scale beyond a single process. You must configure a robust, external session store like Redis or a database for any real application.
One canonical example
A user logs into an e-commerce site. The server validates their password, creates a session, and stores { userId: 42, role: 'customer' } in its Redis session store. The server sends a cookie containing only the session ID back to the browser. When the user tries to access their account page, the browser sends the cookie, express-session uses the ID to fetch the session data from Redis, confirms the user is logged in, and renders the page.
Interview question
What information does an Express.js cookie-based session typically store on the client-side?
- a.An encrypted version of the user's sensitive session data
- b.The complete session object, including shopping cart items
- c.A unique identifier that references server-side session dataCorrect
- d.The user's full profile details and authentication status
Why? this is the answer
The card states that the user's browser holds only a "small, unique ticket (the session ID cookie)" and that "The actual session data never leaves the server; only the identifier does." Options A, B, and D are incorrect because they suggest sensitive or full session data is stored on the client, which is contrary to the server-side nature of cookie-based sessions.
Just read this? Test yourself on what you have been reading.
Read the original → expressjs.com
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on express — each one lists the topics its interview covers.
See open roles