Backend

Sessions vs JWTs: Understanding Authentication in Modern Web Applications

In modern web development, most applications require users to log in to access personalized features. But have you ever wondered: Why can’t the server simply “remember” me after I’ve logged in once?

This article explains the root cause of that problem — the stateless nature of HTTP — and introduces two popular approaches to solving it: Session-Cookie (Stateful) and JWT (Stateless) authentication.

Post image
Photo by ByteByteGo

HTTP Is Stateless — The Core Problem

The HyperText Transfer Protocol (HTTP), the backbone of the web, is stateless. This means:

  • Each request sent from the client (browser) to the server is independent.
  • The server does not automatically retain any information about previous requests.

Example scenario:

  • Request 1: “Hi server, I’m user_A. Please add product X to my cart.”
  • Response 1: “OK, product X has been added.”
  • (Immediately after, the server forgets who user_A is)
  • Request 2: “Hi server, I’d like to checkout my cart.”
  • Response 2: “Sorry, who are you? Which cart?”

This illustrates the consequence of being stateless: the server has no memory of previous interactions. That’s why we need an authentication mechanism to “link” multiple requests to the same user.

Two Solutions to “Remember” Users

There are two widely used authentication models:

How it works:

  1. Login: The user provides credentials. The server validates them, creates a unique Session ID, stores user info on the server (in memory or a database), and returns the Session ID as a cookie.
  2. Subsequent requests: The browser automatically attaches the cookie. The server looks up the Session ID to retrieve user information.

Pros:

  • Easy to invalidate sessions (e.g., logout).
  • Strong security when configured properly (against XSS, CSRF).
  • Sensitive data remains on the server.

Best suited for:

  • Traditional monolithic web apps.
  • When client and server share the same domain.
  • Use cases where you need strict session control (e.g., forced logout).

2. JWT – JSON Web Token (Stateless Authentication)

How it works:

  1. Login: The user submits credentials. The server validates them and returns a JWT — an encoded string containing user information plus a digital signature.
  2. Subsequent requests: The client stores the JWT (commonly in Local Storage) and attaches it to requests (usually via the Authorization header). The server verifies the signature and decodes user info, without needing server-side storage.

Pros:

  • No server-side session storage required → scales well for microservices.
  • Easy to integrate with third-party authentication (OAuth, OpenID).
  • Works without cookies.

Best suited for:

  • Distributed systems and microservices.
  • Applications requiring third-party logins (Google, Facebook, etc.).
  • Lightweight, stateless authentication needs.

Can JWTs Be Revoked?

A common challenge with JWTs is revocation: once issued, a token remains valid until it expires. However, there are strategies to handle this:

  • Blocklist: Store revoked tokens (e.g., in Redis, Memcached, or a database).
  • Token versioning: Increment a “version” field on login; old tokens become invalid.
  • Short-lived Access Tokens + Refresh Tokens: Improves security.
  • Rotating Refresh Tokens: Issue a new refresh token each time to reduce risks.

Conclusion

Because HTTP is stateless, the server cannot inherently “remember” users across requests. To solve this, developers rely on authentication mechanisms:

  • Use Session-Cookie for traditional web apps where you control the full stack and need strong session management.
  • Use JWT for distributed systems, microservices, or third-party integrations where scalability and statelessness are essential.

Ultimately, the choice depends on your system architecture and scalability needs.