I'm building a web application and need to choose an authentication and authorization approach. I'm comparing signed JSON Web Tokens (JWTs), encrypted JSON Web Encryption tokens (JWEs), and database-backed sessions. Which option is generally the best fit for a typical web application? I'm particularly interested in security, implementation complexity, scalability, session invalidation, and overall ease of maintenance.
3 Answers
JWTs and sessions solve related but different problems, so the right choice depends partly on your architecture. A signed JWT lets a service validate claims without looking up session state, which can be useful for distributed services or when integrating with an external identity provider. The tradeoff is that JWTs are difficult to revoke immediately, so they usually need short expiration times and a refresh-token system. A JWE additionally encrypts the token contents, but that is often unnecessary for ordinary authentication; keep sensitive data out of tokens unless you have a specific reason to protect it.
I would not choose JWT simply because it sounds more scalable. For a single web application, a normal session cookie backed by a database or shared cache is usually easier to implement correctly. Use secure cookie settings, protect against cross-site request forgery where appropriate, rotate or expire sessions, and provide a way to revoke them. Consider JWTs when you genuinely need stateless verification across multiple services or have an established identity-provider integration; otherwise, sessions are a better fit.
For most traditional web applications, database-backed sessions are the simplest and safest default. Store a random session ID in a secure, HttpOnly cookie and keep the session data on the server. Logging out, revoking a session, or ending all sessions becomes as easy as deleting a database or cache entry. The lookup on each request is usually negligible, especially if you use a cache, and it avoids having to build token refresh and revocation logic yourself.

An external identity provider can issue JWTs while your application still creates its own local session cookie. You don't have to expose or manage bearer tokens directly in a browser just because the identity provider uses them.