Skip to content
System Design Fundamentals

Lesson 2 of 12 · 20 min

x
2/12

Lesson position in the course — not completion. Use Mark Complete to track finished lessons (saved in this browser).

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)
1// Session stored in server memory2app.post('/login', (req, res) => {3  req.session.userId = user.id; // lives on THIS server only4  res.json({ ok: true });5});6// Load balancer sends next request to Server B:7// Session is gone — user gets logged out
After
Stateless server (scales horizontally)
1// Session stored in Redis — shared across all servers2app.post('/login', async (req, res) => {3  const token = generateToken();4  await redis.setex(`session:${token}`, 3600, user.id);5  res.json({ token });6});7// Any server can validate any token — all servers identical

Check your understanding

  • What breaks when you scale stateful servers horizontally?Show answer

    Answer

    In-memory sessions — the next request may hit another instance that does not have the session.
  • What is the usual fix?Show answer

    Answer

    Externalize session/state (DB or Redis) so every instance is interchangeable.
  • Vertical vs horizontal in one line?Show answer

    Answer

    Vertical: bigger machine. Horizontal: more machines behind a balancer.
Previous

Progress is saved in this browser.

Next Lesson