Module 4 - Scaling
Stateless Services
The key to horizontal scaling: services that don't hold state.
1The Restaurant Analogy
Simple Analogy
Stateful: One waiter remembers your order in their head. If they leave, your order is lost.
Stateless: Your order is written on a ticket. Any waiter can serve you. Easy to add more waiters.
Stateless: Your order is written on a ticket. Any waiter can serve you. Easy to add more waiters.
A stateless service doesn't store client session data between requests. Each request contains all needed information.
2Stateful vs Stateless
| Aspect | Stateful | Stateless |
|---|---|---|
| Scaling | Hard (sticky sessions) | Easy (any instance) |
| Failure | State lost | Just retry elsewhere |
| Load Balancing | Complex | Simple round-robin |
| Deployment | Rolling update tricky | Easy blue-green |
3Where Does State Go?
Database
Persistent data goes here. PostgreSQL, MongoDB, etc.
Cache
Session data, temp state. Redis, Memcached.
Client
JWT tokens, local storage. Client holds state.
Message Queue
Work in progress. Kafka, RabbitMQ.
4Making Services Stateless
1Store sessions externally (Redis, DB)
2Use JWTs-client holds the token
3Pass context in request headers/body
4Externalize file uploads (S3, GCS)
5Use distributed cache for temp state
5Key Takeaways
1Stateless services don't store session data locally
2Any instance can handle any request-easy horizontal scaling
3Externalize state to databases, caches, or clients
4JWTs let clients carry their own session
5Stateless enables simple load balancing and fault tolerance
?Quiz
1. A server stores user cart in memory. What happens if it crashes?
2. How does JWT help make services stateless?