Horizontal Scaling & Stateless Services
Definition
Horizontal scaling
- In plain English
- Horizontal scaling means adding more machines that share the work.
- Think of it like
- It is like opening more checkout lanes instead of hiring one super-fast cashier.
Stateless service
- In plain English
- A stateless service keeps no user-specific memory between requests, so any copy can handle any request.
- Think of it like
- It is like a bank where any teller can help you because your details live in the shared system, not in one teller's head.
Session
- In plain English
- A session is the information a server keeps about a user between requests, such as who is logged in or what is in their basket.
- Think of it like
- Like a cloakroom ticket and the coat it belongs to.
Load balancer
- In plain English
- A load balancer receives incoming requests and spreads them across several servers.
- Think of it like
- It is like a restaurant host who seats each new group at a table with room.
Why it exists
Your ticket sale needs forty browsing servers at 10 am. One larger machine is no longer enough.
A basket stored only on Server A disappears from view when your next request reaches Server B. More servers need shared user data.
How it works
Sticky sessions
- In plain English
- Sticky sessions send each user back to the same server every time.
- Think of it like
- Like always being sent to the one cashier who remembers your order.
Autoscaling
- In plain English
- Autoscaling automatically adds or removes servers as traffic rises and falls.
- Think of it like
- Like a supermarket opening extra lanes when queues grow.
Shared store
- In plain English
- A shared store holds data outside application servers so every application copy can read it.
- Think of it like
- It is like a shared notebook that every cashier consults.
- Move sessions
Put session data in a shared store that every server reaches.
- Make copies identical
Run the same application on every server.
- Route requests
Put the copies behind a load balancer.
- Adjust capacity
Add or remove copies as demand changes.
Users reach a load balancer. It routes to servers A, B or C, which all access one shared session store.
| Question | Session in one server | Stateless application server |
|---|---|---|
| Where does the session live? | Local memory | Shared store |
| Can any copy answer? | Only the copy with the session | Yes, while the shared store works |
| What happens when a copy dies? | Its local session is lost | Another copy reads the saved session |
Examples
Turn browsing capacity up
Browsing servers grow from four to forty when the sale opens at 10 am. Every basket lives in a shared store.
Any healthy browsing server retrieves your basket. Add servers before the rush so startup time does not delay customers.
- Measure peak browsing traffic.
- Keep basket data outside disposable application copies.
The insight
Shared sessions let you adjust application capacity without moving each user.
Keep progress across restarts
A routine restart erased in-memory study progress for 300 learners. Saving sessions in a shared store fixed that loss.
Save each completed step before confirming it to the learner. A replacement server reads the latest saved step.
Before, restarting Server A loses local progress. After, Server A and its replacement both access saved progress in a shared store.
The insight
Data kept only in one server’s memory dies with that server.
Route to a connected kitchen
A kitchen screen keeps a WebSocket connection open to one server. Orders arrive through a shared message channel.
Shared message channel
- In plain English
- A shared message channel delivers messages to servers that have asked to receive them.
- Think of it like
- It is like a radio station heard by every tuned-in receiver.
Both servers receive the order notification. Only the server holding that kitchen’s open connection sends it to the screen.
The order service publishes to a shared channel. Servers 1 and 2 receive it, but only Server 1 holds this kitchen screen’s connection.
The insight
An open connection stays on one server, so messages must reach that server.
Trade-offs
Pros
- Grow beyond one machine’s capacity.
- Keep serving after an application copy fails.
- Adjust copies with demand.
Cons
- Shared user data needs careful storage.
- More machines require more management.
- Connections still belong to individual servers.
Use when
- Use for web servers with variable traffic.
- Use when identical copies handle requests independently.
Avoid when
- Avoid extra copies for tiny workloads without a reliability need.
- Avoid splitting a strictly ordered stream without a plan.
Quick check
Takeaways
- Horizontal scaling means more machines sharing the work.
- Stateless application servers keep sessions in a shared store.
- That shared store needs protection too.