Q21: What is the difference between Session-based and JWT-based authentication in Express?
- Session-based (
express-session): Stateful. Server stores session data in memory or Redis and issues asession_idcookie to the client. Easy to invalidate immediately on the server, but requires shared session storage when scaling horizontally. - JWT-based: Stateless. Server signs a cryptographic token containing user claims and sends it to the client. Server doesn't maintain session storage, but token revocation requires token blacklisting techniques.
Key Takeaways
Sessions are stateful and require centralized database lookup; JWTs are stateless.
JWTs scale horizontally easily across server nodes without shared state dependencies.