Scaling: up, out and stateless¶
What you'll be able to do¶
- Explain why one big server eventually stops being the answer, and what "scale out" actually requires.
- Spot the hidden state that stops a service from scaling horizontally, and move it out.
- Say where the bottleneck goes next once the app tier scales.
Start with one server¶
Every system starts as one machine running the app and the database. It is simple, cheap and fast to build. It also has two built-in limits: it can only get so big, and when it dies, everything dies.
Scaling up (vertical)¶
Buy a bigger machine: more CPU, RAM, faster disk.
- Good: no code changes, no distributed-systems problems. Often the right first move.
- Bad: there is a hardware ceiling, price grows faster than capacity at the top end, you usually need downtime to resize, and it is still a single point of failure.
Scaling out (horizontal)¶
Run many identical machines and spread traffic across them with a load balancer.
- Good: capacity grows by adding machines, and losing one machine is survivable.
- Bad: now you have a distributed system. Requests land on different machines, so anything stored on a machine becomes a problem.
The key idea: make the app tier stateless¶
A server is stateless if any request can be handled by any instance, because nothing the request needs lives only on that instance. Typical hidden state and where it goes:
| Hidden state on the server | Move it to |
|---|---|
| Login session in memory | Shared store (Redis) or a signed token (JWT) |
| Uploaded files on local disk | Object storage (S3 and similar) |
| In-process cache everyone depends on | Shared cache (Redis, Memcached) |
| Background jobs in a thread | A queue plus workers |
| Scheduled tasks ("run every hour") | One scheduler, or a lock, so they don't run N times |
Once the app tier is stateless you can add and remove instances freely, which is exactly what autoscaling does.
Autoscaling in practice¶
- Scale on a metric that tracks load: CPU, requests per instance, or queue depth for workers.
- Always set a minimum (for availability) and a maximum (for cost and to protect the database).
- Add a cooldown so the group does not flap up and down.
- New instances need time to boot and warm up. Autoscaling reacts to load, it does not predict it, so sudden spikes still hurt.
The bottleneck moves¶
After the app tier scales, the shared database is next. Ten app servers can open ten times the connections and send ten times the queries to the same database. This is why the next lessons exist: caching takes reads off the database, replication adds read capacity, and partitioning splits the data itself.
Interview angle
Don't say "we'll scale horizontally" and stop. Say what has to be true for that to work (stateless, shared session store, shared storage) and name the next bottleneck.
Trade-offs at a glance¶
| Scale up | Scale out | |
|---|---|---|
| Complexity | Low | Higher (distribution, consistency) |
| Ceiling | Hard limit | Practically very high |
| Failure | One big failure domain | Many small ones |
| Cost curve | Steep at the top | Roughly linear |
For AI engineers¶
For AI engineers
A notebook can keep a loaded model, a preprocessing cache, and a conversation session in Python variables. A service can also do that while it has one process. With two replicas, each process has its own variables. A user may start a conversation on replica A, then send the next message to replica B, which has no record of it. Put durable session data in a shared store, or make each request carry the context it needs. Do not depend on a notebook's execution history when turning it into a service.
A stateless inference endpoint can accept a request on any replica, but stateless does not mean free to scale. If each replica loads a 4 GB model, four replicas need roughly 16 GB just for model copies, before runtime overhead. A new replica may also take time to load weights and become ready. Plan for that warm-up when deciding how many instances to keep running.
Batch preprocessing has a different shape: split the dataset into independent partitions and let workers process separate partitions. For GPU inference workers fed by a queue, queue depth and wait time may describe demand better than CPU use. Measure the resource that actually limits throughput, then check the next shared dependency, such as a database or object store.
Common mistakes¶
- Keeping a module-level session or cache dictionary and assuming every replica sees it. Each process owns a separate copy; use shared storage for data a later request must find.
- Scaling an I/O-bound inference service on CPU alone. Workers can be waiting on a remote model or datastore while CPU stays low; choose a metric that follows incoming work and response time.
- Having every new replica download weights on startup without budgeting for download time and shared storage load. Several replicas starting together can all become ready late.
- Using sticky sessions as the only fix for local state. A user may stay on one replica until that replica fails, then lose the session; shared state or a self-contained request survives rerouting.
- Adding app replicas without watching database connections, query load, and storage capacity. The app tier can grow while a shared dependency becomes the new limit.
Check yourself¶
Your app keeps users logged in with an in-memory session map. You add a second server behind a load balancer and users get randomly logged out. Why, and what are two fixes?
Each server has its own map, so a request routed to the other server finds no session. Fixes: (1) move sessions to a shared store such as Redis, (2) use signed tokens (JWT) so the server holds no session at all. "Sticky sessions" also work but make scaling and failover worse because a user is pinned to one instance.
Why is scaling up usually the right first move for a small product, even though it doesn't scale forever?
It needs no code changes and avoids distributed-systems problems entirely. The cost of complexity is paid only once the traffic actually demands it.
You scale the app tier from 4 to 40 instances and the site gets slower. What is the most likely cause?
The shared database: 10x the instances means 10x the connections and queries hitting it. The bottleneck moved down a tier. Look at connection limits, slow queries, and read load first.
Lab¶
Build a small two-replica service in code/scaling/ and observe why a process-local login disappears after the load balancer switches replicas. Move sessions into an injected shared store, then implement a service whose response does not depend on local session state. Calculate how many replicas a traffic peak needs with spare capacity, and make an autoscaler respect bounds and cooldown. The five tests move from the session bug to capacity and scaling decisions; the starter fails until you complete the tasks. Run: cd code/scaling && PYTHONPATH=starter python -m pytest tests -q
Go deeper¶
- Interactive: Designed to Scale – vertical vs horizontal, stateless services
- Your handbook: Software Engineering Handbook, ch. 4 "Từ một server đến hệ thống scalable"
See it in the wild¶
Open these real architectures and find the idea in the diagram. Predict how it is used before you read the analysis.


