We use analytics cookies.We use cookies to analyze site traffic and improve your experience.
Loading...
System Design Masterclass · Security Architecture
Free preview: the first 2 parts of this 8-part lesson.
Already bought the course? Sign in to read the whole lesson.
this lesson is part of
System Design Masterclass
Unlock this lesson and the 751 other paid lessons
$25 once, by PayPal
See the System Design courseread the rest of this lesson in
System Design Masterclass
Unlock this lesson and the 751 other paid lessons
$25 once, by PayPal
See the System Design coursein Security Architecture
This lesson is part of the full System Design Masterclass. Unlock it along with 769 other lessons for a one-time payment.
One-time payment by card or PayPal
Get Lifetime AccessOne-time payment: UPI, card or netbanking
Get Lifetime Access18 Foundation lessons are free, no payment needed. Try them now
It is easy and good
“It is really nice and good Please join the course”
Cookie-based sessions work well until you have 10 servers behind a and every one of them needs access to the same session store. You add Redis. Redis goes down at 3 AM and every user is logged out simultaneously. You add Redis Sentinel for failover. Now you are managing infrastructure for something that was supposed to be simple.
Token-based authentication eliminates the server-side session store entirely. Instead of creating a record on the server and giving the client a reference key (the session ID), the server encodes the user's identity directly into a signed token and hands the entire thing to the client. The client sends the token with every request. The server verifies the signature and reads the user data from the token itself. No . No database lookup. No shared state.
This is the stateless model. The server holds no session state between requests. Every request carries everything the server needs to identify the caller. Any server in the cluster can handle any request because verification is a local CPU operation, not a network call to a session store.
Mobile apps drove token adoption faster than anything else. iOS and Android apps do not have a native cookie jar the way browsers do. Setting Authorization: Bearer <token> in a header is natural for mobile. Cookies are not.
The flow:
{userId: 42, role: "admin", exp: 1712345678}Authorization: Bearer eyJhbG...Stateful vs stateless, a concrete comparison:
| Aspect | Stateful (Cookie Session) | Stateless (Token) |
|---|---|---|
| Server stores | Session data in /DB | Nothing |
| Validation cost | Redis GET: 0.2ms + network | Signature check: 0.01ms CPU |
| Horizontal scaling | Shared Redis needed | No shared state needed |
| Revocation | Delete from Redis (instant) | Wait for expiry or maintain blocklist |
| Replay on server crash | Sessions may be lost (unless persisted) | Tokens survive (client holds them) |
| CSRF risk | Yes (cookies auto-sent) | No (header manually set) |
| Mobile-friendly | Awkward (cookie management in native apps) | Natural (Authorization header) |
Why tokens are NOT inherently more secure than sessions: Tokens eliminate CSRF (because they are not auto-attached) but introduce token storage challenges. If you store a token in localStorage, any XSS payload can steal it. A session cookie with HttpOnly is immune to XSS theft. The security trade-off is real.