System Design Fundamentals

Lesson 2 of 12 · 20 min

x
17%

Scalability: Vertical vs Horizontal

Vertical scaling means adding more CPU, RAM, or disk to a single machine. It is simple — no code changes, no coordination — but has a hard ceiling. The largest cloud instances today have hundreds of vCPUs and tens of TB of RAM. When you hit that ceiling, or when cost becomes prohibitive, you must scale horizontally: add more machines and distribute the load across them. Horizontal scaling introduces a key problem: stateful applications break because each request can land on a different server. The fix is to move all session state out of the server — into a database or Redis — so every server is identical and any request can go to any instance. This stateless server pattern is the prerequisite for everything else in distributed systems.

Before
Stateful server (breaks horizontal scaling)
// Session stored in server memory
app.post('/login', (req, res) => {
  req.session.userId = user.id; // lives on THIS server only
  res.json({ ok: true });
});
// Load balancer sends next request to Server B:
// Session is gone — user gets logged out
After
Stateless server (scales horizontally)
// Session stored in Redis — shared across all servers
app.post('/login', async (req, res) => {
  const token = generateToken();
  await redis.setex(`session:${token}`, 3600, user.id);
  res.json({ token });
});
// Any server can validate any token — all servers identical

Key Takeaway

Make servers stateless before scaling horizontally — shared-nothing is the foundation of distributed systems.

PreviousNext Lesson