Skip to content
KhaiziNam Blog KhaiziNam Blog
Go back
Đọc bằng tiếng Việt

Session vs JWT Interview Guide: Everything You Need to Answer With Confidence and Depth

You know what Session and JWT are — but do you get stuck when the interviewer asks a follow-up? This guide breaks down the full mechanics, a direct head-to-head comparison, and the most common interview questions on Session vs JWT, so you can answer with genuine depth instead of reciting a definition.

In most backend junior interviews, authentication questions are almost guaranteed. “Session or JWT — which would you choose and why?” looks straightforward but is one of the clearest filters between candidates who truly understand the topic and those who’ve only memorized the surface.

Table of Contents

1. The core problem: why is authentication tricky?

2. How Session works — from request to response

3. How JWT works — anatomy of a token

4. Direct comparison: Session vs JWT across key criteria

5. When to use Session, when to use JWT

6. Real-world case study

7. Common mistakes when answering this question

8. Quick FAQ


The Core Problem: Why Is Authentication Tricky?

HTTP is a stateless protocol — every request is completely independent, and the server has no memory of who you are from the previous request. This is by design to make the web scalable, but it creates a real problem: how does the server know that this request is coming from an already-logged-in user?

The answer is that state must be stored somewhere — either server-side (Session), or client-side in a self-contained token (JWT). Each approach solves the same problem with entirely different trade-offs, and understanding those trade-offs is exactly what interviewers want to see.

This article is a companion to Backend Junior Interview Questions: The Complete Guide Covering APIs, Databases, Auth, and Caching. If you haven’t read that yet, start there to get the full picture of what to prepare.

How Session Works — From Request to Response

The login flow with Session

When a user logs in successfully, the server creates a session record stored in memory (RAM), a database, or Redis — containing information like user ID, role, and expiration time. The server then generates a random session ID (typically a long hex string), sends it back to the client via the Set-Cookie header. The client stores this cookie and automatically sends it with every subsequent request.

On each request, the server reads the session ID from the cookie, looks it up in the session store, finds the user data, and confirms the request is valid. If the session ID isn’t found or has expired, the server returns 401.

Session strengths
Session weaknesses

How JWT Works — Anatomy of a Token

The structure of a JWT

A JWT (JSON Web Token) consists of three parts separated by dots: Header.Payload.Signature — all Base64URL encoded.

The Header contains the signing algorithm (typically HS256 or RS256) and token type. The Payload contains claims — user information such as user_id, role, email, and expiration time (exp). The Signature is the result of signing the Header + Payload with a secret key — this is what prevents tampering.

Critical to understand: the Payload is NOT encrypted — it is only encoded. Anyone with the token can decode and read the payload. Only the Signature is cryptographically signed — if someone modifies the payload, the signature becomes invalid and the server will reject the token.

The login flow with JWT

User logs in → server creates a JWT, signs it with the secret key, returns it to the client → client stores the token (typically in localStorage or an httpOnly cookie) → every subsequent request sends the token in the Authorization header: Bearer → server receives the token, verifies the signature using the secret key, decodes the payload to get user information — all without any database or session store lookup.

JWT strengths
JWT weaknesses

Direct Comparison: Session vs JWT Across Key Criteria

State storage

Session: Stateful — server maintains session store. JWT: Stateless — client holds the token, server stores nothing.

Revocation capability

Session: Instant — delete the session record and it’s done. JWT: Cannot revoke before exp — requires a blacklist or access/refresh token pattern as a workaround.

Scalability

Session: Requires centralized store (Redis) when scaling to multiple servers. JWT: Natively stateless — horizontal scaling is straightforward with no shared store needed.

Payload security

Session: Client knows nothing about session data — only holds the ID. JWT: Payload is decodable by anyone — never store sensitive data there.

Architecture fit

Session: Monolithic apps, same-domain frontend/backend, use cases requiring instant revocation (banking, admin panels). JWT: Microservices, mobile apps, cross-domain scenarios, third-party API access.

When to Use Session, When to Use JWT

This is the question many interviewers actually want to hear your answer to — not “JWT is better” or “Session is outdated,” but genuine use-case reasoning.

Use Session when: Traditional web application on the same domain, need to log users out instantly across all devices (financial apps, banking, admin systems), no complex horizontal scaling requirement, or using a framework with strong session support like Laravel, Rails, or Django.

Use JWT when: Microservice architecture where multiple services need to verify the user, mobile apps (cookies don’t work well), Single Page Application with a separate API, need to grant tokens to third-party services, or system that needs horizontal scaling without managing a centralized session store.

Use both (hybrid): A common production pattern is to use Session for the main web app (because instant revocation is required) and JWT for mobile APIs or third-party integrations. This is a “senior-level” answer that junior candidates rarely think of — but mentioning it will make a strong impression.

Real-World Case Study

Hung was interviewing for a backend junior position at a fintech company building a mobile banking application. The interviewer asked: “Would you use Session or JWT to authenticate users in our mobile app, and why?”

Hung’s answer: “For a mobile app, I’d choose JWT with an access/refresh token pattern. Mobile clients don’t handle cookies as naturally as browsers do, and JWT is a better fit for stateless authentication over a REST API. However, since this is a financial application, I’d use short-lived access tokens — 15 minutes — combined with refresh tokens stored in the device’s secure storage. If suspicious activity is detected, the refresh token can be revoked server-side to force re-authentication.”

The interviewer followed up: “What if the user loses their phone?” — Hung responded: “Implement a revoke-all-tokens endpoint for that user — essentially maintaining a refresh token blacklist in Redis with appropriate TTL, or using a token version number in the database. This is a point where we accept adding some statefulness back in order to guarantee security.”

Result: Hung received strong positive feedback — not because he picked the “right” answer, but because he demonstrated trade-off reasoning and edge case handling. That’s exactly the mindset backend engineers need to show.

Common Mistakes When Answering This Question

Quick FAQ

Q: Is JWT the same as OAuth?

A: No — these are two different concepts that are commonly confused. JWT is a token format — a way to encode and sign information. OAuth 2.0 is an authorization framework — a protocol for granting access rights. JWT is frequently used as the format for access tokens within OAuth 2.0, but OAuth doesn’t require JWT — opaque tokens are a valid alternative. Understanding this distinction will set you apart from candidates who use the terms interchangeably.

A: This is one of the most debated questions in web security. localStorage is easy to implement but vulnerable to XSS attacks reading the token. httpOnly Cookie protects against XSS but requires CSRF protection and has cross-domain limitations. The most common production approach: httpOnly, Secure, SameSite=Strict cookie for web apps; platform secure storage (Keychain on iOS / Keystore on Android) for mobile apps. There’s no perfect answer — every approach has trade-offs.

Q: What’s the difference between HS256 and RS256 in JWT?

A: HS256 uses HMAC with a single shared secret key for both signing and verification — simple, but every server that verifies tokens must know the secret. RS256 uses an RSA key pair — the private key signs, the public key verifies — better suited for microservices because you only need to distribute the public key while keeping the private key secure on the auth server. For simple single-server systems, HS256 is fine. For microservices or third-party verification, RS256 is the better choice.

Q: What is a refresh token and why is it necessary?

A: JWT access tokens typically have a short lifespan (5–15 minutes) to minimize the damage window if stolen. But you can’t force users to re-login every 15 minutes — this is where refresh tokens come in. A refresh token is a separate, longer-lived token (7–30 days), stored securely. When the access token expires, the client uses the refresh token to obtain a new access token silently, without requiring the user to enter their password again. Refresh tokens are stored server-side and can be revoked at any time.

Q: Do Node.js and PHP interviews ask about Session/JWT differently?

A: The concepts are the same, but implementation details differ. For PHP/Laravel interviews, expect questions about Laravel Sanctum vs Passport, session driver configuration (file, database, Redis), and cookie encryption. For Node.js, questions typically focus on express-session, the jsonwebtoken library, and middleware patterns.

Final Thoughts

Session and JWT are not opponents in a competition — they are two tools solving the same problem in different ways, each appropriate for a different context. Interviewers don’t want to hear you pick the “right” one — they want to see that you know why you’re picking it and why the other approach could also be right in a different situation.

Before your backend interview, implement both from scratch — a mini app using Session, and one using JWT with the full access/refresh token flow. Hands-on experience will help you answer every follow-up question naturally, in a way that no amount of memorization can replicate.

21/04/2026

See more: 


Share this post:

Related Posts

IT Fresher & Junior Salary 2026: PHP, Node.js, React, Flutter - Real Market Data

Real salary benchmarks for IT freshers and junior developers in Vietnam in 2026, broken down by tech stack - so you know exactly what number to quote in interviews, or whether you're being underpaid right now.

Junior Developer Portfolio: What to Include to Get Interview Calls

A junior developer portfolio is the collection of real projects, skills, and professional information you present so employers can evaluate your abilities — substituting for the work experience section of your CV that's currently empty. For freshers and junior developers, a portfolio isn't something that's "nice to have" — it's the only evidence you can offer to prove you can actually build things

How to Write a Junior Developer Cover Letter With No Experience

Learn how to write a junior developer cover letter with no experience, including practical templates, real examples, common mistakes, and tips to pass the CV screening round.

Body Language in Tech Interviews: 7 Mistakes That Cost You the Offer

Body language in tech interviews refers to the non-verbal signals — posture, eye contact, hand gestures, and vocal tone — that recruiters observe alongside your technical answers. Mastering these signals helps you project confidence and professionalism from the very first second you walk into the room.

Questions to Ask Your Interviewer as a Junior Dev (Beyond Just Salary)

A practical guide to asking questions in IT job interviews — why the "do you have any questions?" moment matters more than most candidates realize, 20+ real questions organized by purpose and interview round, what never to ask, and how to choose the right questions for each interviewer you face.

Why Did You Choose IT? An Interview Answer That Actually Impresses Recruiters

A practical guide to answering "Why did you choose IT?" in job interviews — what HR is actually evaluating, a 3-part framework for building your answer, script templates for freshers and career changers, and the common mistakes that make this answer sound hollow and unconvincing.

'What Is Your Greatest Weakness?' — IT Interview Answer That Won't Get You Rejected

A practical guide to answering "What is your greatest weakness?" in IT job interviews — why the question is a trap for most candidates, a 3-step framework for freshers and junior developers, ready-to-use script templates, real case studies, and the six most common mistakes that get candidates rejected on the spot.

ReactJS Junior Interview Questions: 30+ Real Questions With Answers 2026

30+ of the most commonly asked ReactJS junior interview questions in 2026 — covering Virtual DOM, hooks, state management, and performance optimization — with detailed answers and code examples to help you confidently pass any technical interview. You've been learning React for a few months, you've built projects, but every time you walk into an interview you get asked things no tutorial ever cove

Refresh Token in Node.js and Laravel: Complete Production Implementation Guide 2026

A complete guide to implementing Refresh Tokens in both Node.js (Express) and Laravel PHP — covering database design, API endpoints, rotation logic, and common pitfalls — helping developers build a production-ready JWT authentication system in 2026.

Session vs JWT: Which Should Developers Choose? A Practical Comparison for 2026

A detailed comparison of Session and JWT across 6 practical technical criteria — architecture, revocation, scaling, performance, security, and complexity — helping developers make the right choice for each type of project in 2026.


Previous Post
ReactJS Junior Interview Questions: Hooks, Virtual DOM, and State — Complete Guide With Answers
Next Post
Cách Tạo CV IT Cho Fresher Bằng LaTeX Và AI