Scaling: Vertical, Horizontal and Stateless Servers
Overview
Every successful product eventually gets more traffic than one machine can handle. Scaling is how you grow capacity. There are two directions: buy a bigger machine (vertical) or add more machines (horizontal). Horizontal scaling is how large systems work, but it only works if your servers keep no private state. This lesson explains both directions, what "stateless" means, and how to move user state out of the server so that any machine can serve any request.
Detailed Notes
What Is It?
Scalability is a system's ability to handle more load by adding resources, without a redesign. Load can mean requests per second, data volume or number of users.
Vertical scaling (scale up): give one machine more resources: more CPU cores, more memory, faster disks.
Horizontal scaling (scale out): add more machines and spread the work across them.
A server's state is any data it remembers between requests. A stateful server remembers (for example, who is logged in, held in its own memory). A stateless server remembers nothing between requests: every request carries or points to everything needed to handle it, and durable data lives in a shared store such as a database. Stateless servers are interchangeable.
Why Do We Need It?
A single machine has hard limits: the biggest machine you can buy has a ceiling, the price rises faster than the capacity at the high end, and one machine is one point of failure (if it dies, everything is down). Traffic also changes through the day, so fixed hardware is either too small at peak or wasteful at night.
Horizontal scaling removes the ceiling (keep adding machines), improves availability (losing one of 20 machines costs 5% of capacity) and allows autoscaling, adding and removing machines automatically as load changes. But it brings new difficulties: you need a way to spread requests (load balancing, next lesson), data must be shared consistently, and failures become normal events.
How Does It Work?
Vertical vs Horizontal ScalingComparison
Vertical (scale up)
one bigger machine
1
Traffic grows
2
Upgrade CPU, RAM, disk
often needs a restart
3
Same single server serves all
No code changes
No distributed-system problems
Hard ceiling and rising price
Single point of failure
Downtime to upgrade
Great first step, poor long-term plan.
Horizontal (scale out)
many machines
1
Traffic grows
2
Add identical servers
behind a load balancer
3
Load is spread across them
No hard ceiling
Survives single failures
Autoscaling possible
Needs stateless design
Data consistency and operations are harder
Standard for large systems.
Key point. Start simple with a bigger machine; plan for scale-out once you need headroom beyond one machine or more than one failure domain.
The same traffic growth handled in two ways.
Vertical scaling
You upgrade the server: 8 cores to 64, 32 GB memory to 512 GB. Advantages: no code changes, no distributed problems, simple to operate. Limits: a ceiling, rising cost per unit, downtime during the upgrade, and the single point of failure remains. It is a good first step, and databases are often scaled vertically longer than application servers because splitting data is hard.
Horizontal scaling
You run many identical copies of the application behind a load balancer (a component that distributes incoming requests across servers). Capacity is roughly the number of servers times the capacity of one. Costs grow roughly linearly, there is no hard ceiling, and one failure is survivable. The price is complexity: coordination, data consistency, and operations.
Making servers stateless
Suppose a user logs in and the server keeps the session (the record that this user is logged in) in its own memory. The load balancer then sends the next request to a different server, which knows nothing about the user, so they appear logged out. There are two fixes.
Sticky sessions: the load balancer always routes the same user to the same server. It works, but that server holds state: if it dies the sessions vanish, load becomes uneven, and scaling down is hard because you cannot remove a server holding live sessions.
Externalize the state (preferred): move sessions to a shared fast store (an in-memory store such as Redis) or put the state in a signed token the client sends with every request (for example a JWT, a JSON Web Token carrying the user id and an expiry, signed so it cannot be forged). Servers become identical and disposable.
Stateless Servers with Shared Session StoreArchitecture
Key point. If a server can be killed at any moment without losing user data, it is stateless.
Any server can handle any request because the session lives in one shared place.
Other places state hides, and how to move it out:
Uploaded files saved to a server disk: use object storage.
In-memory caches private to a server: use a shared cache so servers do not hold different versions.
Scheduled jobs that run on one server: use a job queue or a single elected leader so jobs do not run twice.
Local counters such as rate limits: use a shared store.
Scaling each layer
Different layers scale differently:
Web and app servers: stateless, so scale out easily.
Caches: add nodes and split keys across them.
Databases: hardest because data has state. Typical path: scale up first, add read replicas (extra copies that serve reads), then partition (split data across machines). These get their own lessons in Chapter 2.
Slow work: move it off the request path into a queue processed by background workers.
Autoscaling
Autoscaling watches a metric (average CPU, requests per server, queue length) and adds or removes servers when it crosses a threshold. It needs stateless servers, fast startup and a cooldown period to prevent flapping (adding and removing repeatedly). It cannot react instantly: starting a server may take a minute, so keep headroom for sudden spikes.
Interview tip. Common mistakes: saying "just add more servers" without addressing the database, keeping sessions in server memory, and ignoring that scaling out multiplies the number of things that can fail. Always say what the bottleneck is before choosing how to scale.
Interview discussion. "I'll keep the application servers stateless, with sessions in a shared store, so I can scale them horizontally behind a load balancer. The database is the harder part, so I'd begin with a bigger primary and read replicas, then partition when write volume or data size demands it."
Real-World Example
An online shop runs on one server. Before a holiday sale the team first upgrades it (vertical), which buys a week. Then they move sessions into a shared store, add two more identical servers behind a load balancer, and add a read replica for the database. During the sale, autoscaling grows to 12 servers; at night it shrinks to 3. One server crashing during the sale is a non-event, because the load balancer stops sending it traffic and the others carry on.
System Design Example
For a news feed API handling 60,000 peak QPS with 2,000 QPS per server, you need about 30 servers, so horizontal scaling is mandatory. Make the service stateless with the user's identity in a token. Put a cache layer between servers and the database. Beyond that, the interesting question is the database: 60,000 reads per second with a 90% cache hit leaves 6,000 reads per second for the database, which a few replicas can carry, while writes are far fewer and can go to a single leader at first.
Trade-off thinking. Vertical scaling is cheap in engineering time but limited and risky; horizontal scaling is cheap in hardware but expensive in complexity. Reasonable answer: scale up until simple, scale out when you need the headroom, availability, or are at the machine ceiling. The trade-offs chapter revisits this decision in depth.
Key Interview Takeaway
Scale up for simplicity, scale out for capacity and availability. Make application servers stateless by moving sessions, files and shared counters out of the server, so any server can serve any request. The database, which holds state, is where scaling gets hard.