Meet Fanline
Nine days before the biggest ticket sale your company has ever run, nobody on the call cares whether your boxes and arrows look tidy. They want you to say, out loud, what breaks first and why you are fine with that. Real designs are a pile of trade-offs: consistency against availability, speed against cost, a clean idea against the calendar you actually have. This track is about how to move through those trade-offs.
The track uses one running example, so you never start from a fresh toy problem. You are the third engineer at Fanline, an invented ticket-booking startup. At the start it is one server selling seats to a 300-person room called The Locksmith. By the last chapters it is handling a stadium tour that sells out before most fans finish loading the page. Every figure and every incident in this track is made up to teach with.
The same example carries two kinds of design. Chapters 1 to 4, and chapter 9, are about the whole system: servers, caches, databases, queues, and how storage scales. People call that high-level design. Chapters 5 to 8 are about the code inside the system: how classes are cut and how they depend on each other. People call that low-level design. Fanline needs both, and you will see that a choice at one level often shows up at the other.
How a design discussion works
A common mistake is to assume there is a hidden correct diagram, and that your job is to guess it. Two companies solving the same problem, such as a ticketing platform, a chat app or a dispatch system, often end up with very different designs. Both can be sound, because their traffic, team size and deadline were different. What matters is whether you can turn a vague request into real requirements, back a decision with a number instead of the word "scale", and explain why you chose one trade-off over another.
There is a sequence that fits almost any problem. It is the one that got Fanline's small team through a launch with nine days of runway:
- Clarify requirements. Separate what the system must do (functional) from the conditions it must do it under (non-functional): how many users, how much traffic, how fast, and whether a number on screen must be exactly right or only close. Skip this and everything after answers a question nobody asked.
- Estimate, roughly. Turn the requirements into numbers you can hold in your head: requests per second, data growth per year, how lopsided reads are against writes. This tells you whether one machine can carry the load or whether the talk has to be about spreading it.
- Sketch the high-level shape. Clients, a load balancer, app servers, a cache, a database, maybe a queue. Draw it coarsely on purpose. The aim is a picture everyone agrees on before anyone zooms into a box.
- Go deep on the part that matters. Pick the piece that is genuinely interesting for this problem, such as an ID scheme that cannot be guessed or a rate limiter that works across several servers, and work it through: the data, the algorithm, where it breaks. Going deep on every box is its own failure.
- Say what you did not solve. Name the bottleneck, the single point of failure, and the thing that breaks first at ten times today's traffic. A design presented as airtight is the warning sign. Every real design has a soft spot, and naming yours shows you know where it is.
Here is step 2 with Fanline's numbers, because estimating is the step people skip. A rough night at The Locksmith is about 200 requests spread over an hour. That is 200 divided by 3,600 seconds, or about 0.06 requests a second. One server is far more than enough, and nothing in the numbers asks for more. A stadium sale is different. If 50,000 tickets sell in the first ten minutes, that is 50,000 divided by 600 seconds, about 83 purchases a second, sustained, before counting everyone who tries and loses the race. The monthly average hides that shape completely. Size for the burst, not for the smooth average, or the design looks fine on paper and fails when it matters.
The process matters more than any single diagram. A cache in front of a database, or a leader with read replicas, will not surprise anyone who has seen these shapes before. What shows your thinking is the path: whether a number from your own estimate ever comes back into the design, and whether you named your own single point of failure before someone asked. Two people can hand in different diagrams and both be reasonable, if each is honest about the requirements it started from. Two people can also hand in the same diagram and differ completely, if one reasoned their way there and the other recited it.
Common mistake: treating the component you studied hardest as the subject of every discussion, and steering every answer back to it whatever was asked. It sounds rehearsed.
Scaling up and scaling out
Fanline runs on one application server and one database for most of its first year, and that is the right call. The Locksmith seats 300 people, shows go on sale weeks ahead, and a rough night is a couple of hundred requests spread over an hour.
Scalability means a system can absorb growth, in users, data or traffic, by adding resources instead of being rebuilt. There are only two directions, and the one you pick first shapes much of what follows.
Vertical scaling means making one machine bigger: more cores, more memory, a faster disk. It is the easiest path, because the application does not change. When Fanline signs a second venue, a 900-seat hall, bumping the server's size is a five-minute change that buys months. The ceiling arrives sooner than the invoice suggests. There is a largest machine you can buy. Well before you reach it, price stops growing in step with capacity, because the top of any range costs a premium. And one machine is still one machine. It goes down, and there is nothing behind it.
Horizontal scaling means adding more machines and sharing the work. There is effectively no ceiling, and you gain something vertical scaling cannot give: lose one server out of five and the other four keep going, slower but alive. It is not free. Something in front of the fleet must decide where each request goes, which is load balancing, covered next. More importantly, every server behind that balancer must be swappable for any other.
Why horizontal scaling needs stateless servers
A stateless server can take any request and answer it correctly, because it is not privately holding something the rest of the fleet does not know about. Fanline learned this the hard way.
Traffic began to outgrow one box, so the team added a second app server behind a load balancer. It shipped on a Thursday. By Saturday, support had an angry email. A customer added three seats to her cart for a weekend show, stepped away to look up the venue address, and came back to an empty cart. Nothing had been removed. Her first request had reached Server A, which kept carts in its own memory because that was the quickest thing to build. Her second request reached Server B, which had never heard of her. The seats were safe in Server A the whole time. From her side the cart had vanished, and she bought from a competitor before anyone traced the bug.
That is a stateful design breaking its own promise. A follow-up request has to reach the same server as the first, or what that server held is nowhere to be found. If you leave it, the balancer has to pin each customer to one server (called session affinity or "sticky sessions"), and when that server dies, every cart pinned to it dies too. That is the single-point-of-failure problem again, hidden behind a fleet.
Fanline's fix was to stop keeping carts on the app server and move them into a store every instance reaches the same way:
Before (fragile):
Customer -> Load Balancer -> Server A (cart lives only in its memory)
Her next click MUST reach Server A, or the cart is gone.
After (scalable):
Customer -> Load Balancer -> any server (A, B or C, it does not matter)
|
v
Shared cart store (Redis)
Every server can answer every request, because no single server is
the only place that holds what she put in her cart.
With carts in Redis, the servers are interchangeable. The balancer can send traffic to whichever box is least busy, and losing a server costs only the request that was in flight on it.
Common mistake: calling a fleet stateless while it quietly keeps something that matters in local memory, such as a per-process cache of a hot event's remaining seats, or a short queue of confirmation emails. It looks fine on the diagram until two requests from one person reach two boxes. That is the bug that cost Fanline a sale.
Load balancing
Something has to sit in front of the fleet and decide which box takes each request. The rule it uses matters more than it looks. A poor rule leaves half the fleet idle, or sends traffic to the servers least able to take it.
| Algorithm | Decision rule | Good when | Falls short when |
|---|---|---|---|
| Round robin | Go through the fleet in order, one request each | Every request costs about the same | It ignores load: a box stuck on one slow checkout still gets the next request |
| Least connections | Send to the box with the fewest open connections now | Request cost varies a lot (a quick browse beside a full checkout) | Slightly more bookkeeping, usually worth it |
| Weighted variants | Same idea, but bigger boxes get a bigger share | The hardware is mixed | Weights go stale when the hardware changes |
| Consistent hashing | Put servers and keys on a ring; a key belongs to the nearest server clockwise | The same key must keep reaching the same server | Harder to build correctly |
When one server is as good as another, round robin or least connections is enough. Consistent hashing solves a different problem: a repeated request for the same key should keep finding the same server, usually because that server holds something useful for the key in memory.
What consistent hashing fixes
Fanline met this when it began keeping a hot show's seat map in each app server's memory, to skip a database trip on every click during a rush. With plain round robin in front, two clicks on the same seat map from one person reach different servers about four times in five, so the in-memory cache barely helps.
The obvious first try is to route by hash(event_id) % number_of_servers. It works until the fleet size changes. Add a sixth box for the next big sale and the divisor changes, which changes the result for most events, not only the few that needed to move. Every cache that was warm a second ago now serves the wrong show. It looks like wiping the cache at the very moment you added capacity to relieve pressure.
We tested it. With 20,000 event keys, going from 5 servers to 6 under the modulo rule moved about 84% of them to a different server. That matches the arithmetic: a key stays only when its remainder is the same for 5 and for 6, which happens about one time in six.
Consistent hashing places the servers at positions around a fixed circle, and places each event on the same circle. An event belongs to the first server you meet walking clockwise from the event's own position:
Positions climb clockwise, wrapping to zero at the top:
Server 1 (pos 15)
/ \
Server 4 (pos 310) Server 2 (pos 95)
\ /
Server 3 (pos 210)
"marlow-vance-msg" hashes to 40 -> Server 2 owns it (first clockwise from 40)
"locksmith-jazznight" hashes to 260 -> Server 4 owns it (first clockwise from 260)
Put a new server at position 65. The only slice that changes hands is the one between the previous server and the new position: positions 16 to 65, which belonged to Server 2. Everything else on the ring stays where it was, and those caches stay warm. Remove a server on a quiet Tuesday and the reverse happens: its slice passes to the next server clockwise and nothing else moves.
In the same test, the ring moved only about 13% of the keys when the sixth server joined. The ideal for six servers is one in six, about 17%. So resizing the fleet no longer scrambles nearly every cache entry. It touches only the slice that belonged to the server that joined or left.
Real systems give each physical server several positions on the ring ("virtual nodes"), not one. The reason shows up in the numbers. With one position per server, the five servers in our test held 3%, 54%, 8%, 18% and 18% of the keys, because one unlucky server got a very wide arc. With 100 positions each, the six servers held between 13% and 20%. Without that spread, Fanline's busiest machine could easily be the one that landed the widest arc.
Layer 4 and layer 7 balancing
There is a second axis: which layer of the network the balancer reads when it decides.
- A layer 4 balancer sees only an IP address and a port. It forwards a connection without looking inside, which makes it fast and simple. It has no opinion about what the request asks for.
- A layer 7 balancer understands the protocol on top, HTTP in this case, and can decide from a URL path, a header, a cookie or even the body. Pulling an event ID out of
GET /events/marlow-vance-msg/seatsso it can be hashed only works here. A layer 4 balancer works one level below where a URL even exists.
Layer 4 costs less, because there is less to examine per packet. Fanline needs layer 7 as soon as a routing decision depends on anything beyond which IP reached which port. Once seat-map caching enters the picture, that is true immediately.