Statelessness
One-Liner
A design principle where a server does not store any information about the client’s session between requests.
What It Is
A system design where each request from a client to a server contains all the information needed to understand and fulfill the request. The server itself does not retain any client context or session state across multiple requests.
Why It Exists
To significantly improve the scalability, reliability, and resilience of distributed systems, especially in horizontally scaled architectures.
How It Works
- All session-specific data (e.g., user authentication, shopping cart contents) is either stored on the client-side (e.g., in a cookie or JWT) or in a centralized, highly available, and scalable data store (e.g., a distributed cache like Redis).
- Each request is self-contained.
Tradeoffs
Pros
- Scalability: Any server can handle any request, making it easy to add or remove servers (horizontal scaling).
- Reliability: If a server fails, other servers can pick up requests without losing user state.
- Simplicity: No need for complex session management logic on individual servers.
Cons
- Can increase the size of each request if all state is passed in the request.
- Requires a separate, highly available state store if session data is not client-side.
Failure Modes
- If client-side state (e.g., JWT) becomes too large, it can increase request latency.
- If the centralized state store becomes a bottleneck or fails, it can bring down the entire system.
Interview Traps
- Confusing statelessness with the inability to store any data. Stateless refers to server-side state between requests.
- Not being able to explain how to manage session state in a stateless architecture.
Real-World Usage
- Most modern web services and microservices are designed to be stateless.
- RESTful APIs inherently promote statelessness.
Anti-Patterns
- Storing user session data directly on the application server’s local memory, making it impossible to scale horizontally or recover from server failures without session loss.
Related Concepts
- Horizontal Scaling
- Distributed Caching
- Load Balancing
- Microservices