Backend & Node.js · APIs, data and failure
How do session-based and token-based authentication compare?
Short answer
Session auth stores state on the server and gives the client an opaque session id. Token auth sends a self-contained signed token and keeps less server state, trading easier horizontal scaling for harder revocation.
Both approaches identify a caller, but they differ in where the authority lives. With sessions, the server looks up the session record on every request. With tokens, the server verifies a signature and trusts the claims. The first is easy to revoke; the second is easy to scale.
app.use(
session({
store: new RedisStore({ client: redis }),
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: {
httpOnly: true,
secure: true,
sameSite: "lax",
maxAge: 1000 * 60 * 60 * 8,
},
}),
);Trade-offs
- Sessions require a shared store such as Redis or a sticky load balancer
- Session revocation is immediate: delete the record
- Tokens avoid a lookup per request but cannot be revoked without extra state
- Cookies need CSRF protection for state-changing requests
- Mobile and third-party clients often prefer bearer tokens
- Both need HTTPS, rotation, short lifetimes and secure cookie flags where applicable
A hybrid is common: an opaque session cookie for the browser, plus short-lived access tokens for service-to-service calls. The important question is not which is modern, but where revocation, scale and client type push the complexity. If you need instant logout across devices, server-side state is a feature.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1How do you revoke a session versus a token?
- 2Why do sessions need a shared store in a cluster?
- 3What CSRF protections does a cookie-based session need?
- 4When would you choose tokens for a browser app?
Say it in the interview
Session auth keeps authority on the server and gives the client an opaque id, so revocation is immediate but every instance needs access to a shared store. Token auth is self-contained and scales well, but revocation requires extra state and access tokens must be short-lived. I choose based on client types, revocation requirements and operational complexity, and I secure both with HTTPS, rotation and appropriate cookie flags.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Backend & Node.js mock interview and get scored feedback on depth, clarity and confidence.