Token vs Session Authentication
Authentication is the process of verifying if a user is who they are claiming to be. The most common way to verify a user is by using a password only the user would know, or by additionally having a third party verify their identity, also known as 2-factor authentication. So, when the user initially sends their username and password to the server, essentially 2 things happen: the server will receive their request and verify their identity based on if the username and password match its records, and if they do, the server establishes an authenticated session or issues a token. Only then will the client (or application) store some form of proof of authentication, in the form of a cookie or token, so that in subsequent requests it can be sent to the server rather than prompt the user to keep sending their credentials.
What we want to discuss, are the different ways the backend server handles checking the authentication, the types and their trade-offs in terms of security and performance
Stateless vs Stateful Authentication
These 2 methods are Stateful and Stateless
Stateful authentication simply means that once you've logged in the server keeps track of a the current authentication status and keeps track of the users that are currently logged in until log out. The users' statuses are usually stored server-side (often in a shared store such as Redis or a database), so when a user makes a request from his browser, the cookie including his authentication details is stored client-side and will be sent alongside it. The server will then check the database to see if the user is A. logged in (active) or logged out (inactive), and B. and if the sent data matches that of the active session details Alternatively, the application can handle the authentication in a stateless way, meaning the server doesn't track the user's state. The data being sent by the user alone to verify their identity is enough for the server to validate them.
These approaches have their trade-offs but in general …
- Stateful = easier revocation and control
- Stateless = harder revocation, simpler distribution
So, now that we know what the 2 types of authentications are, lets delve into the tools and protocols that an application uses to complete the process of authentication.
Authentication Methods
There are many concrete protocols and techniques wildly used globally to authenticate users like:
- Biometric: authenticating a user using their fingertips using a scanner
- Single Sign On: a username and password that gives the user access to multiple application or services
- 2 Factor/ Multifactor: like we mentioned earlier, using a third-party device and or service ei: email, sms, windows authenticator, etc.
- Certificates: a collection of cryptographic keys and info about the identity of the holder provided by a authority over a network
But the ones we are going to be focusing on are:
- Session
- And Token
Session Authentication
A session is a server-side record representing a user's authenticated state. This record exists until the user's authentication expires whether by logging out, or set expiration rules session authentication is where once a user logs in, the server stores information about the user's authentication identity on a db, making it stateful. This data is then returned to the user in the form of a cookie and in subsequent requests the generated cookie is sent alongside them.
So, let's say for example, a user with a private twitter wants to see their posts, they enter their credentials and login, once the server verifies the credentials, it will generate a session record and store it server-side which may look like this:
{
"session_id": "7f9c2e4a9b...",
"user_id": 12345,
"username": "alice",
"authenticated": true,
"roles": ["USER"],
"created_at": "2026-01-07T10:12:00Z"
}
The important value to note here is session_id, which will now be sent back to the client as a cookie. Cookie:
Set-Cookie: session_id=7f9c2e4a9b...;
The user now has their authentication information stored in a cookie. When the user opens their account to see their posts, a request is sent to the server containing that cookie. Cookie sent alongside request:
GET /my/private-posts
Cookie: session_id=7f9c2e4a9b...
The server logic will see that the data is private and can only be accessed by a specific user (which it tracks using user_id). It will see if the session id sent by the client:
- A. matches that of an existing record
- B. the session id's match that of the user that is allowed to see the private posts.
Finally, the server responds with the requested resources.
Advantages:
- Easier to use in web-based application since cookies are supported on the browser
- Session cookies are naturally small, making it efficient for storage on client-side
Limitations:
- Harder to scale since the cookies are stored in a database server-side
- Multiple Domains become problematic since cookies typically work on a single domain
- Security risk: cookies are susceptible to Cross-Site Request Forgery
Token Authentication
Tokens (such as JWTs) are cryptographically signed data that is stored by the user and sent alongside requests so servers can verify their authenticity A good analogy is to think of tokens as a ticket to a movie resource. Your ticket gains you access to your movie and no other, so when the cashier hands you the ticket and you hand it to the theatre guy, he doesn't have to authenticate that you are the buyer – he just validates your ticket.
In other words, when a user logs in using their credentials, the server will verify the credentials are returned a signed token. The client receives this token and stores it locally on the client side. The token is then placed in the "authorization header" of subsequent requests and all the server does is verify if the token is valid.
Typical web api token authentication request headers:
> GET /api_path HTTP/1.1
> Host: yourdomain.com
> Authorization: Bearer <AUTHGEAR_ACCESS_TOKEN>
Finally, if the user logs out of the application and the client deletes the token from local storage.
Advantages:
- Mobile devices are far easier to implement
- Avoiding database lookups for sessions is faster and easier to scale
- Supports multiple complex infrastructures with multiple domains
- CSRF risks are reduced when JWTs are sent via Authorization headers rather than cookies.
Limitations:
- JWT are significantly larger than cookies since they store a lot more information
- If a token is hijacked by hackers, the server has a harder time invalidating them
When to implement Session vs Token
There are practical decision rules engineers follow when deciding whether their service should handle authentication via session-based auth or token-based auth, session-based authentication is recommended when …
- The application is a browser-first applications, which are traditional webapps that could server-rendered pages
- You want to have the ability to admin-force logouts or instantly revoke sessions and kill access to comprised accounts for example
- The application has high risk data access, and you wish to control the sessions and, for instance, allow only 1 active session
On the other hand, token-based authentication is recommended when …
- You wish to scale to Mobile applications, since session cookies aren't native, tokens can be implemented more seamlessly
- The application is distributed in a Microservice where trust must cross domains or organizations.
- Implementing single sign-on and wanting to give a single user identity access to multiple organizations or independent systems