AlgoViz

Horizontal scaling

easy 25 min

Horizontal scaling, or scaling out, means adding machines and sharing the load between them.

See it run

Many ordinary machines behind one address: how the fleet grows, follows the load, loses a member and shares its data.

Operations

Grow the fleet

From one server to six. Each can do 100 requests a second at its limit; the price is an example.

Step 1 of 5

One server

Our systemrequestsUsersLoad balancerServer 1
CPU each
70%

capacity

100 req/s

cost (assumed)

$0.05/h

70 requests a second on a server that can do 100. There is no second server, so a failure stops everyone.

Step 1: One server

def servers_needed(peak, per_server=100, use=0.7, spare=1):    return ceil(peak / (per_server * use)) + spareservers_needed(420)   # 6 + 1 = 7
Downtime
None
App must
Be stateless
Scale up instead
2-10 min restart

Do not plan for 100% use. At 70% a server still has room for its neighbour's share when one fails.

In the real world

The same horizontal scaling doing real jobs. Pick one and step through it.

A food ordering site triples its traffic every day at noon.

Step 1 of 4

11:30

Our systemrequestsUsersLoad balancerServer 1Server 2
CPU each
70%

capacity

200 req/s

cost (assumed)

$0.10/h

140 requests a second on two servers. 70% each.

Step 1: 11:30

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Scaling out with state in memory

    critical

    Sessions, uploads and counters kept in one server vanish for every request that lands elsewhere.

    Wrong
    sessions = {}                 # in this processsessions[sid] = user
    Right
    redis.set(f"session:{sid}", user, ex=3600)
  • Running with no spare

    critical

    Two servers at 80% cannot lose one: the survivor would need 160%. Both fall.

    Wrong
    n = ceil(peak / per_server)
    Right
    n = ceil(peak / (per_server * 0.7)) + 1
  • Scaling the wrong tier

    major

    If servers wait on the database, more of them only add connections and locks.

    Wrong
    if slow: add_web_servers(10)
    Right
    if waiting_on == "database":    fix_queries_or_add_replicas()else:    add_web_servers(2)
  • Killing a server without draining

    minor

    Requests in progress fail on every scale-in and every release.

    Wrong
    terminate(server)
    Right
    balancer.deregister(server)wait_until_idle(server, at_most=30)terminate(server)

Interview tips

  • How to explain it

    • Scaling out: more machines, one address, shared load.
    • It survives failures and grows online, if servers are stateless.
    • It moves the limit to the parts they share.
  • Memory trick: OUT

    • O is for Out: add more machines.
    • U is for Unlimited, almost: coordination sets the limit.
    • T is for Together: they work as one cluster.
  • Reach for it when

    • A single failure must be invisible.
    • Traffic moves through the day.
    • The largest sensible machine is in sight.
  • Check first

    • Where do sessions, files and jobs live?
    • Can the database take the connections?
    • Are retries safe?
    • Who spares the load balancer?