CodingNodes Space play · ←→ step · R restart

The problem

Every large system started as one small one. A single machine runs the web server and the database together, and for the first few thousand users that is enough. The lesson of this chapter is not any single trick — it is the order in which the standard moves get pulled, and the failure mode that forces each one.

Latency creeps up, so the box gets bigger. A crash takes everything down, so the reads move to a replica. The one app server maxes out, so a load balancer spreads traffic across many. The database becomes the wall, so a cache absorbs the hot reads and the writes are finally split across shards. By the end, the same request that once hit one box has a whole machine behind it.

We will fix a few numbers first, then walk the machine growing step by step — each part adding exactly one new piece and showing the pressure that demanded it.

Requirements

Functional — what it must do

  • Serve the same product — pages, API calls, data — as the number of concurrent users grows from thousands to millions without changing the product.
  • Keep reads fast under load: the common case is a hot read that should not touch the slowest component in the system.
  • Survive the loss of any single component: no machine, disk or process may be the only copy of anything the user can see.

Non-functional — how well it must do it

  • A single box should be able to absorb roughly 10,000 requests per second before we are forced to spread the load — that is the wall this design runs into, on purpose.
  • Replication lag on the read path stays under about 100 ms, so a replica read is still 'fresh enough' for the product.
  • A cache in front of the database should absorb roughly 80% of reads, leaving the primary to handle writes and the cold tail.

Scale — how much it must take

  • From a single machine at a few thousand users, to a load-balanced fleet with a cache, to a sharded database — targeting on the order of 100,000 requests per second at peak.

Assumptions — the numbers this design is sized for

A rate limit means nothing without a scale. These are the figures every later part relies on: how much traffic arrives, how much work the limiter has to do per second, and how much latency and memory it may spend doing it.

Starting point
one machine
Web tier and database on the same box. Simple, and correct until it is not.
Target peak
~100,000 req/s
The 'millions of users' bar. Sizing every later move against this number.
One box ceiling
~10,000 req/s
A rough per-box throughput. Ten times below target — the reason we must go horizontal.
Failure cost
1 box = 100% down
With everything on one machine, a single crash is a total outage. The first reason to split.
Replica lag
< 100 ms
How stale a read replica may be before we route it around the replica for that read.
Cache absorption
~80% of reads
If the cache takes four of every five reads, the database only sees the cold one.
Write wall
one primary
The single primary is the last bottleneck left. Sharding splits it.

Takeaways

Part 1

One box does it all

The starting point is deliberately tiny: one machine, one database, no load balancer, no cache. It works — right up to the first pressure.

  1. 1
    The whole product on one machine

    This is where every system of this size begins. The web server and the database live on the same box, and a user's request goes through exactly one hop into it and one hop back out. There is nothing to route, nothing to replicate, nothing to scale yet.

  2. 2
    Why it is enough, for now

    At a few thousand users the box has headroom, there is one owner to patch, and the data is local so reads are fast. Simplicity is a real feature here — the cost of premature distribution is exactly the complexity this chapter is about to add, one necessity at a time.

  3. 3
    The first pressure shows

    Traffic climbs. Response times stretch, the CPU sits pinned, and a single restart or disk failure now takes down the entire product. These three symptoms — latency, saturation, total failure — are the three forces that will drive every move in this chapter.

Part 2

Buy a bigger box

The honest first answer to 'one box is not enough' is to make that box bigger. It is the cheapest fix and it buys time for the real work.

  1. 1
    More CPU, more RAM

    Vertical scaling means swapping the machine for a larger one — more cores, more memory, faster disk. The application code does not change, there is nothing new to deploy, and for a while the latency problem simply gets smaller.

  2. 2
    Where it runs out

    But the ceiling just moved. A bigger box is still one box: it still has a single power supply, a single disk, a single process. Vertical scaling buys a multiple of the old throughput and none of the redundancy, so it is a delay, not a solution.

  3. 3
    So we split the reads

    The database is the piece that gets slow first, and its reads are the bulk of the traffic. The first structural move is to give those reads their own copy of the data — a replica — so the primary is free to do what only it can do, which is the writes.

Part 3

Split the reads

A read replica follows the primary's writes and serves the reads. The primary finally gets to do only writes, and the read path can grow on its own.

  1. 1
    Primary writes, replica reads

    Every write still lands on the primary — it is the source of truth. But the reads now go to the replica, which keeps a copy of the same tables. The load balancer in front can route the two kinds of traffic to the two different databases.

  2. 2
    The replica is a copy, not a twin

    The replica receives the primary's changes and applies them in order. That is a pipeline, and pipelines have a delay: a write that just landed on the primary may take a moment to be visible on the replica. That gap is replication lag, and it is the one cost of this design.

  3. 3
    Route around the lag

    A user who writes and then immediately reads would see their own write missing on the replica. The fix is to keep 'read your own write' on the primary and only send the anonymous, tolerant reads — the feed, the lists, the searches — to the replica, where the lag does not matter.

Part 4

Add servers behind a balancer

The database got help; now the app tier does too. A load balancer spreads the traffic across any number of identical, stateless servers.

  1. 1
    One app server is the new wall

    The database can now split its load, but there is still exactly one app server in the request path, and it is the same size as day one. When it saturates, adding a bigger database does nothing — the bottleneck moved up to the tier in front of it.

  2. 2
    A balancer and a fleet

    We put a load balancer in front and hang three identical app servers behind it. The balancer hands each request to whichever server is free. Because the servers hold no per-user state, adding a fourth is just adding a box and a route — no migration, no downtime.

  3. 3
    Stateless is the whole trick

    This only works because we moved all the things a request needs out of the server's memory and into the shared databases. A server that remembers a login in its own RAM cannot be dropped and replaced by its neighbor. Statelessness is what makes 'add a box' a legal move at all.

Part 5

A cache and a CDN in front

The reads are now split across replicas, but they still cross the network to a database. A cache keeps the hottest reads in RAM, and a CDN keeps the static ones at the edge.

  1. 1
    The hot reads deserve a home

    Even across replicas, a large share of reads keep asking for the same handful of things — the front page, the top items, the config. Those are the hot reads. A cache in RAM sits in front of the database and answers them in a fraction of the time, so the database only sees the cold, long tail.

  2. 2
    Static things go to the edge

    Images, stylesheets and scripts are the same bytes for every user, so they do not belong on the origin at all. A content delivery network keeps a copy of them at edge locations near the users, and most of the bytes never travel to our data centers at all.

  3. 3
    The cost: freshness

    A cache is a deliberate lie about how fresh the data is. When the source changes, the cached copy must be invalidated or it expires, or users keep seeing the old value. That freshness trade is the price of the speed — and it is why we size the cache to the genuinely hot, rarely-changing slice.

Part 6

Split the writes across shards

The reads are now cached, replicated and edge-served. The one thing left with a single hard ceiling is the write path — the single primary. Sharding is how we remove that last wall.

  1. 1
    The primary is the last wall

    Every write in the system still lands on exactly one database. No amount of caching or app servers removes that fact, because the source of truth is one place. Once the primary saturates, the only move left is to divide the data itself.

  2. 2
    Pick a key, partition by it

    Sharding means choosing a key — usually the user — and using it to decide which of several databases owns each row. A router in front of the shards reads the key on every write and sends the row to the one shard that owns it. The total write capacity is now the sum of the shards.

  3. 3
    The price: the shard key is a promise

    Once a row is on a shard, moving it is expensive, so the key you pick is a long-term decision. Pick a key that spreads the traffic evenly and rarely changes — a user id, not a timestamp — and the shards stay balanced. Pick badly and the whole system inherits a hot shard.

★ Finale

End to end

The full machine, built up from that one box: a request enters at the edge, is balanced across a stateless fleet, is answered from cache where it can be, and only then touches the sharded data.

  1. 1
    The whole machine

    Every piece from the chapter in one frame. The request path runs from the user through the edge, the balancer and the app fleet, while the data path fans out to a cache, a primary and a set of shards. This is the same product that started as one box.

  2. 2
    A hot read, answered at the edge

    The common case. A request arrives, the edge hands the static parts to the user, and the dynamic part is answered from the cache in RAM. The database fleet never sees this read at all — this is the read the whole design was shaped to absorb.

  3. 3
    A write, split across shards

    A user changes their data. The write goes through the app to the primary, which is now the router for the shard set: it reads the user's key and sends the row to the shard that owns it, then replicates the change to the read replica for the next tolerant read.

  4. 4
    Millions of users, one request at a time

    Run that at a hundred thousand requests a second and each request still sees the same small path: edge, balancer, app, then either cache or shard. The scale is not in any single box — it is in the fact that no single box is on the critical path for the hot case anymore.

🌐 In the wild

In the wild

This arc is not a classroom story — it is the documented growth path of a large number of public systems. Here the boxes are anchored to the real systems that are known to have made exactly these moves.

  1. 1
    The shape, as companies describe it

    The monolith-to-distributed arc is the standard story in engineering write-ups: start simple, then add a load balancer, a cache, replicas and sharding as each bottleneck appears. This is an illustrative mapping to well-known systems, not a claim about any one company's internals.

  2. 2
    Why the order matters in production

    Teams that shard before they add a cache, or add a balancer before they make the servers stateless, pay for the mistake in incidents and rework. The order in this chapter — make it fast, make it redundant, make it divisible — is the order that keeps each step reversible.