InfraSim system design guide

Load balancer vs CDN

They both sit in front of your servers, so they look interchangeable. One shares the work out. The other makes most of it disappear.

A load balancer and a CDN both sit between users and your servers, which is why they get mixed up. They do different jobs.

A load balancer lives next to your servers and spreads incoming requests across them. It checks which servers are healthy and stops sending traffic to the ones that are not. It does not answer anything itself: every request still travels all the way to your region and is handled by one of your servers.

A CDN (content delivery network) is a set of servers spread around the world, close to your users. When it has a copy of what a user asked for, it answers from nearby and the request never reaches you. When it does not, it forwards the request to your origin, usually through your load balancer, and may keep the answer for next time.

So they are not alternatives. A load balancer shares the work out. A CDN makes a chunk of the work disappear. Most sites that use both put them in this order: users, then CDN, then load balancer, then servers.

The experiment

A shop with customers on two continents, 1,400 requests a second and growing. 60% are page reads (most of them cacheable), 30% are 150 KB static files like images, and 10% are writes. Eight medium app servers sit behind a load balancer, with one database. Forty seconds in, it is evening on the second continent and traffic jumps 60%, much of it from far away.

We ran three versions with the same seed.

In front of the serversp95 before the jumpp95 afterApp servers busy (after)Simulated cost / month (after)
Load balancer only197 ms542 ms64%$3,494
CDN, 1-minute TTL, pages not cached118 ms409 ms57%$2,695
CDN, 24-hour TTL, pages cached67 ms300 ms17%$1,967

Availability was 100% in all three. Nothing was overloaded; people were just waiting. InfraSim, Advent of Outages door 17, seed 261217.

What the load balancer could not do

With only a load balancer, the p95 was 197 ms before anything happened. The servers were fine. The time went on distance: a user on the other continent pays for an ocean crossing on every request, and spreading requests evenly across eight servers does nothing about that. Adding servers would not have helped either.

What the CDN did

Even a timid CDN, keeping files for a minute and not caching pages, cut the p95 before the jump from 197 ms to 118 ms, because the images were now answered close to the user.

Letting it keep things for a day and cache the cacheable pages too took the hit rate from 18% to 72%. The p95 dropped to 67 ms, and the app servers went from 64% busy to 17% during the jump: about three requests in four never reached them. Those servers did not get faster; they got less to do.

The bill fell too, from $3,494 to $1,967 a month. Most of that is data transfer. In InfraSim's cost model, bytes sent from the edge cost less than bytes sent from your own servers, and a 150 KB image answered at the edge never leaves your servers at all.

Why the p95 still rose during the jump

Even the best version went from 67 ms to 300 ms when the far continent arrived. The misses still cross the ocean, and there were a lot more of them. A CDN hides distance for what it can cache. For the rest, you need the data closer to the user, which is a much bigger project.

What a CDN will not do

It will not cache writes, and it should not cache anything personal unless you are very careful about who gets which copy. A long TTL also means users can see old content until it expires. The load balancer is still needed behind it, to spread the misses across your servers and route around dead ones.

About these numbers

They describe InfraSim's simulation, which models traffic as flows per tick, with a fixed seed so each run repeats exactly. Costs are the game's simulated monthly figures, tuned to rank trade-offs the way real bills do, not to match any provider's prices.

Break it yourself

This is Advent of Outages door 17, "Across the Ocean": the same shop and the same evening rush, free in your browser with no account. The door opens on 17 December and stays open after.

In the InfraSim app, Closer to the User (chapter 1, Requests & Routing, free) is the first level where nothing is overloaded and users are still waiting, on distance alone.

New to InfraSim? The first five campaign levels play in your browser right now. No install, no account.

Get InfraSim: System Design Game

InfraSim is a system-design game: drag load balancers, caches, databases and queues onto a canvas, deploy, and watch simulated live traffic flow through what you built. Then survive traffic spikes, zone outages and bot swarms across a guided campaign, endless Survival runs and a Daily Challenge. Tutorial and 15 levels free, with optional InfraSim Pro.

More about InfraSim