Stateless Design and Scaling Patterns
Overview
Scaling out only works smoothly if any server can handle any request. That is the idea behind stateless design. This lesson explains what that word really means, where the data goes instead, and how to find the part of your system that limits growth.
Detailed Notes
What is stateless
State is any data a system must remember between requests: a login session, a shopping cart, a half-uploaded file.
A stateless server keeps no such data in its own memory between requests. Everything it needs either arrives with the request or is looked up from a shared place outside the server. A stateful server keeps that data locally.
Stateless does not mean "the system has no data". The data still exists; it simply lives somewhere designed to hold it, not inside the machines that handle the requests.
The problem it solves
An online store keeps each shopper's cart in the memory of the web server that first served them. With one server this works. With three servers sharing traffic, a shopper adds a jacket on Server A, the next request lands on Server B, and the cart is empty.
A quick fix is sticky sessions: always send a user to the same server. But then Server A can become overloaded while others idle, and if Server A crashes everyone on it loses their cart.
How it works
Make the servers stateless by moving the state out. There are three common places to keep a session:
- In the client. The server gives the browser a small, signed token (proof of who you are, which the server can verify but nobody can forge). The browser sends it with every request. Servers stay stateless.
- In a shared store. A separate fast database or cache holds sessions and carts. Each server looks them up by session id.
- On the server (sticky sessions). Easy but fragile. Avoid it.
Once servers are stateless they are interchangeable:
- Any request can go to any server.
- A crashed server is simply replaced; nothing is lost.
- You can add or remove servers at any time. Auto scaling does this automatically: it adds servers when a metric such as CPU or request rate is high, and removes them when it is low. It only works well with stateless servers that start quickly.
The trade-off: each request may need an extra lookup to the shared store, which adds a little latency, and the shared store itself must now be reliable and scalable. Shared state lives in databases and caches; scaling those is covered in later chapters.
Hunting bottlenecks. A bottleneck is the component that limits the whole system's throughput. To find it:
- Set a target load and latency.
- Trace one request and list everything it touches.
- Measure utilisation (CPU, memory, disk, network, connections) at each step.
- Fix the most saturated part, then re-test. The bottleneck will move; repeat.
A simple example
A stateless server is like a bank teller who keeps no private notes. Your whole history is in the bank's shared computer, so any teller can help you, and a new teller can start immediately. A stateful teller remembers you in their head; if they go home sick, nobody else can help.
System design example
A web tier of ten identical servers handles requests. Login sessions are kept in a shared store and the browser holds a signed token. Traffic is split between servers by a load balancer (covered later). During a sale, auto scaling grows the tier to thirty servers; afterwards it shrinks back. If a server crashes mid-sale, users do not notice, because nothing about their session lived on it. Meanwhile the team watches the shared store and database, since these stateful parts are now the likely bottleneck.
Summary
- State = data remembered between requests.
- Stateless servers hold none locally; state lives in the client token or a shared store.
- Stateless servers are interchangeable, so scaling out, replacing and auto scaling are easy.
- The stateful parts (databases, caches) become the hard part to scale.
- Find the bottleneck by measuring, fix it, and measure again.
Architecture Diagram
Any server can serve any request because sessions live in a shared store.
- 1A request arrives at any server.
- 2The server reads the user's session from the shared store.
- 3It handles the request and writes any changes back.
- 4A different server can handle the next request with no loss.
Common Interview Questions
Finished this lesson?
0 of 91 lessons done · 0% of the course

