Session ID vs Session Token

 

In this post  we will see what is a session token and session ID.

Imagine, you are want to travel is a train and you buy a ticket. The ticket is valid for a certain amount of time. You can travel any number of times as long as the ticket is valid. 

Ticket is the "Session Token".

Let's compare with distribute systems.



When you login to the banking website, it prompts the user to enter with "Username: and "Password".

Upon successful authentication, the authentication server generates a session token.

This session token is valid for a certain amount of time.

With this token, the user can make future requests in the banking site like checking balance, fund transfer. Once the token is expired, the user must "Re-Authenticate".

Then, What is Session ID?

When a user log to the banking website, server create a session token post verifying credentials.

Then, the server creates a session ID.

Server stores the token inside the session data.

Server gives the client the session ID(usually in a cookie).

session_id = "session123"

Redis["session123"] = {
user_id: 100, session_token: "f9a2c1e4b8d9a7c3e12f4ab9c88d1aa", expires: "2026-09-11T18:00Z" }

When the client makes a request, the cookie (Session ID) must be passed.

So, when the server receives the request. It validates in a specific and predictable sequence.

Client send a GET request with Session ID

GET /welcome/<username>

Cookie: session_id=session123

Server extracts the cookie header.

session_id = "session123"

Then, the server looks up the session ID in the session store (Redis/Memcache/Hazzlecast)

If the session_id exist, it checks the "expires". If valid, the request is processed by the application that serves /welcome/, else session is invalid.

I created a simple flask application to demonstrate the same.

When the user logs in, they need to pass the credentials.

On successful authentication, server create a session ID.


Now, When I make calk to the /welcome/alice, the session ID is passed and validated.


Session token mapped to this ID is valid for 1 min. After 1 min,









Comments

Popular posts from this blog

Agentic AI - Series 3

Agentic AI - Series 4

S3 - Directory Bucket