Blog Interactive Lab
System Design on a Bench: One Idea, One Machine
Big software systems are built from a few simple ideas. A load balancer shares work between servers. A cache keeps answers that are asked for again. A queue lets a fast part go on without waiting for a slow one. These ideas are easy to explain, but hard to see.
So I built each idea as a small machine. Balls roll, presses work, and queues fill up. In every lab you:
- Watch the machine and play with it. Everything runs in your browser, on a phone too.
- Follow short lessons. Press “Next lesson”, and the machine sets itself up to show one point.
- Read one live sentence under the picture that says what is happening right now.
- Open the details when you want them: the numbers, real systems, and the words to know.
It is for students, junior developers, people preparing for a system design interview, and anyone who wants the idea without the code. The order below is the suggested one, but each lab also works on its own.
1. Load balancer
Big sites run on several servers, and a load balancer decides which server answers each request. In this machine, every ball is a request, a shuttle hands the balls out, and three presses are three servers. One press is four times slower.
What you will see: “take turns” (round robin) gives the slow server as much work as the fast ones, so its queue fills up and requests are lost. “Least busy first” (least connections) loses nothing with the same servers. But when a fast server goes off, no rule can make up for the lost capacity.
2. Cache
Getting an answer from the database takes time. A cache keeps a copy of popular answers close to the server, so most requests never reach the database. This machine is a clerk’s desk: a small shelf at hand (the cache) and a tall cabinet at the far end (the database).
What you will see: the shelf starts empty and slowly fills with popular items. When every item is asked for equally often, the cache barely helps. A small shelf with repeated requests beats a big shelf with scattered ones. So learn your traffic before you buy more memory.
3. Rate limiter
A broken program or an attacker can send more requests than a server can handle. A rate limiter stands at the door, lets normal traffic in, and turns the rest away with code 429. This machine is a gate with a token tray: each request must take a token, and a tap adds one new token every second. This method is called a token bucket.
What you will see: calm traffic passes without trouble. A short burst passes up to the number of tokens in the tray. A flood is held to one request per second, however big the tray is.
4. Message queue
When you sign up on a site, the site does not need to send your welcome email before it answers you. It puts the job in a queue as a short message and answers at once. Other programs, the workers, take the messages a little later and do the work. This machine is a conveyor belt with 12 places and three workers.
What you will see: when work arrives faster than one worker can handle, the queue grows and so does the wait. More workers shorten the queue, and each message goes to only one of them. When a worker stops in the middle of a job, its message goes back to the front of the queue, and nothing is lost.
5. Database index
A database keeps rows roughly in the order they arrived. To find one row, it may have to read them all. An index is a sorted copy of one column, like the index at the back of a book. In this machine, a reader moves along a bridge, and each stop is one check.
What you will see: without an index, three times the rows means three times the work. With an index, the reader starts in the middle and drops half of the rows at every step, so 24 rows need at most 5 checks. The cost: every new row must also be written into the index, so writes get slower.
6. Replication and consensus
Any server can fail. So important data is kept on several servers. One of them is the leader: it takes every write and copies it to the others. A write that is on a majority of the servers is committed. This is the idea behind Raft, which etcd, Consul and CockroachDB use. This machine has three servers, and you can turn each one off and on.
What you will see: a write commits with 2 of 3 servers, so one server going off does not stop the system. A server that comes back gets everything it missed. When the leader goes off, the others vote, and the new leader goes on with every committed write. With only one server on, nothing commits: the system waits rather than risk losing data.
7. Circuit breaker
When a service does not answer, the program that called it waits, and holds a thread while it waits. Soon all its threads are waiting, and it stops working too. This is called a cascading failure. A circuit breaker works like the fuse box in a house: after a few failures it trips, and calls fail at once instead of waiting.
What you will see: without a breaker, a broken service traps all 6 threads of the caller. With a breaker, it trips after 3 failures and the threads stay free. When the service is fixed, one test call succeeds and the breaker closes again by itself.
8. Backpressure
When one part makes work faster than the next part can handle it, the extra work must wait somewhere. If that place fills up and the fast part is not told, the extra work is silently thrown away. Backpressure is the signal that tells the fast part “wait”. This machine is a water tank on a roof: a pump sends up 3 litres a second, the tank holds 40 litres, and a float valve shuts the inlet.
What you will see: with a working float valve, the tank fills, the pump rests, and no water is wasted. With a broken float valve, the pump never learns that the tank is full, and every extra litre runs out of the overflow pipe. The tank only covers short gaps between making and using; if use is always higher, the tank empties in the end.
9. Retries, backoff and jitter
When a request fails, the program tries again. The trouble starts when thousands of programs try again at the same moment: a server that has just come back falls over again under this thundering herd. In this machine, 30 users are connected to a server that accepts 10 new connections a second and falls over at 18 attempts in one second. The server goes down for 3 seconds, and everyone must reconnect.
What you will see: if everyone retries after a fixed wait, they all come back together and the server falls over again and again. Doubling waits (backoff) reduce the rush, but the attempts still arrive together. When a little randomness (jitter) is added to each wait, the attempts spread out and everyone is back within seconds.
10. Consistent hashing
When data is split between servers, every key needs a known server. The simple way is “hash the key, take the remainder after dividing by the number of servers”. But add one server, and almost every key moves. Consistent hashing puts keys and servers on a ring. This machine is a turntable with 24 keys and up to five servers.
What you will see: with the remainder method, adding server D moves 16 of the 24 keys. With the ring only a few keys move, but the servers get very uneven loads. With virtual nodes, several places on the ring for each server, the loads become almost even, and few keys still move.
11. A partitioned log, Kafka style
A message queue deletes a message once it is read. A log keeps the messages, and each reader only remembers its own place. For speed, Kafka splits every topic into several partitions. This machine has three rails, three partitions, and the messages of one user always go to the same rail.
What you will see: each user’s messages stay in order, because they are all in one partition. With one consumer, the backlog grows; with three, each reads one partition and keeps up. A fourth consumer sits idle, because a partition has only one reader in a group. A second group reads the same messages on its own, from its own place.
12. Write-ahead log: surviving a power cut
A money transfer has two steps: take money from one account, and add it to another. If the power goes off exactly between the two steps, money disappears. So databases first write the whole change to a write-ahead log, before they touch the data. This machine has four accounts as stacks of coins, and a power switch you can turn off at any moment.
What you will see: without the log, a power cut in the middle of a transfer loses a coin for good, and the accounts no longer add up. With the log, the database reads it when the power comes back, finishes the half-done transfer, and the accounts add up again.
13. Quorums: when does a read see the latest write?
The data is kept on three replicas. A write counts as done when W replicas have it, and a read asks R replicas. This machine has three replicas, one near and one far away, and you can change W and R or turn a replica off.
What you will see: with W=1 and R=1, writes are fast, but about one read in three gets an old value. With W=2 and R=2, W+R is more than 3, so every read reaches at least one up-to-date replica and no read is stale; the price is slower writes. With W=3 and one replica down, no write can complete.
14. Unique IDs in a distributed system
Every order needs a unique ID. On one server, a counter is enough. But when several servers create orders at once, two IDs must never be the same. This machine has three servers and three methods: a central counter, random numbers, and Twitter’s Snowflake (time + server number + counter).
What you will see: the central counter gives sorted IDs, but every ID costs a round trip, and if the counter goes down, everyone stops. Random numbers are fast, but only about half the IDs come in time order. Snowflake is both fast and almost sorted; only when a server’s clock jumps back does that server have to wait a moment.
15. The last ticket: race conditions and locks
Three tickets are left and six people press “Buy” at almost the same moment. If the app reads the count, thinks a little, then writes “what I read, minus one”, several people can see the same “1 left” and all get a ticket. This is a race condition. This machine has six kiosks talking to one database.
What you will see: with “read, then write”, all six buyers get a ticket and the database still says 1 left (a lost update). With a lock, exactly three are sold, but the last buyer waits about 4.5 seconds in line. With a version check (optimistic locking), three again, after 7 retries and not in arrival order. With one atomic step inside the database, three tickets to the first three buyers, and everyone has an answer in about 1.2 seconds.
16. Saga: one order, three services
An order needs three services: payment, stock and shipping, each with its own database. No single transaction covers all three. What if shipping fails after the money is taken? This machine has an order service and three services, and shipping can fail.
What you will see: with no plan, 9 customers are charged in 20 seconds and nothing ships. With a saga, every done step is undone in reverse (release the stock, refund the payment), and no order is left broken. With two-phase commit, all commit together, but if the coordinator crashes between the two phases, the locks stay held and orders pile up behind them.
17. Cache problems: stampedes and stale data
A cache makes reads fast, until a popular copy expires and everyone runs to the database at once, or the data changes and the cache keeps giving the old answer. This machine has readers, a cache, and a database with a load dial.
What you will see: when every reader asks the database itself, up to 17 queries arrive at once at every expiry (a cache stampede). With single flight there is one query, and with early refresh nobody waits. When the price changes and the cache is left alone, 97 readers get the old price; with delete-on-write, none.
18. Bloom filter: “definitely not” or “maybe”
A web crawler has seen a billion addresses, and keeping all of them costs a lot of memory. A Bloom filter keeps only a row of bits and answers “have I seen this?” with “definitely not” or “maybe”. This machine stores keys by switching bits on, then asks about unknown keys.
What you will see: “no” is always right, but “maybe” is sometimes wrong (a false positive). With 5 keys in 32 bits, about 1 question in 20 gets a false “maybe”; with 20 keys, about 2 in 3. With 64 bits for 10 keys, it drops to about 7%.
These labs are now part of a full, free course, with readings and case studies: System Design, Hands On.
Which machine helped you most, and what should the next one be about? Tell me in the comments.