Skip to content
Engineering Bible
System Design

System Design on scaling and load balancing

Scale up first, then scale out

  • Scaling up (vertical) means a bigger machine. It is easy, but there is a biggest machine.
  • Scaling out (horizontal) means more machines. It goes further but adds moving parts.
  • Take each step only when measurements show the current one is full.
  1. 1. Bigger serversimple, but has a limit
  2. 2. More serversbehind a load balancer
  3. 3. Cache and read replicastake reads off the database
  4. 4. Shardingsplit the data: last resort
Climb only when the step below is full. Each step adds parts that can fail.

Find the bottleneck before adding machines

  • Measure which resource is full: CPU, memory, disk, or database connections.
  • More app servers do nothing if the database is the slow part.

Stateless servers are easy to multiply

  • A stateless server keeps nothing between requests that another server would need.
  • Put sessions and data in a shared store, such as Redis or the database.
  • Then any server can answer any request, and losing a server loses no data.

A load balancer spreads requests across servers

  • Clients talk to one address. The load balancer forwards each request to a server.
  • Adding capacity means adding servers behind it. Clients notice nothing.
  • It must not be a single point of failure: run more than one, or use a managed one.
  1. Client
  2. Load balancerone address, picks a server
  3. App serversstateless, as many as needed
  4. Databaseshared by all servers

How it picks a server matters

  • Round robin takes servers in turn. It is simple and fine when requests cost the same.
  • Least loaded sends each request to the server with the fewest in progress.
  • Google saw up to 2x difference in CPU use between servers under plain round robin.

Health checks take bad servers out

  • The load balancer probes every server and stops sending traffic to ones that fail.
  • Check the server itself: the process answers and its disk works.
  • A check that queries the database fails on every server at once if the database is slow.
  • AWS load balancers fail open: if every server fails the check, all get traffic anyway.
System Design in seven pages2 / 7