Code Labs
Build the pieces yourself
22 small builds of the parts that appear in system designs. Read the idea, run it in your browser, change it, and extend it. Plain TypeScript, with a check that proves it works.
0 of 22 finished
- 1Learn the idea. Each lab says in plain words what the piece does and why systems need it.
- 2Watch it, then run it. Follow a guided demo, play with the picture, then run the real code in your browser and change it.
- 3Extend it. Try the extra challenges that make it closer to what real systems do.
Every lab has a guided demo and runs in your browser, except those marked “On your computer”, which read and write files.
Traffic
Decide who gets in, and where each request goes.
0 of 3 finished
- Load balancerStarter25 minPick a backend for each request with round-robin or least-connections, and stop sending traffic to unhealthy servers.Used in: Nginx, HAProxy and cloud load balancers
- Token bucketStarter25 minA bucket that fills with tokens at a steady rate. Each request spends a token, so short bursts are allowed but the long-run rate is capped.Used in: Amazon API Gateway throttling: the rate is the refill and the burst is the bucket size
- Sliding window rate limiterStarter25 minAllow at most N requests per key in any window of the last T milliseconds, and say how long to wait when refused.Used in: API gateways and public APIs
Caching
Keep hot data close, and know when to let it go.
0 of 2 finished
- LRU cacheStarter20 minA fixed-size cache that evicts the least recently used entry, using the insertion order of a Map for O(1) operations.Used in: Redis and memcached eviction
- TTL cache with a reaperStarter25 minEntries expire at an absolute time. Reads expire lazily, and a background sweep reclaims entries nobody reads again.Used in: Redis expiry (it uses both lazy and active removal)
Data
Clever structures that answer big questions in small memory.
0 of 6 finished
- Bloom filterStarter25 minA compact set that never forgets an item but sometimes claims to have seen one it has not, sized from the item count and error rate you choose.Used in: Databases such as Cassandra and HBase, to skip disk reads for missing keys
- Keyset paginationStarter20 minCompare offset paging with 'after this key' paging on a list that changes between requests.Used in: Feeds and timelines
- Reservoir samplingStarter20 minKeep k uniformly random items from a stream of unknown length using only k slots, and check the fairness empirically.Used in: Sampling logs and traces
- Count-Min sketchIntermediate30 minCount how often each item appears in a fast stream using a small fixed table. Estimates can be too high but never too low.Used in: Frequency estimates inside Caffeine's TinyLFU cache admission, using a compact 4-bit counter table
- HyperLogLogIntermediate35 minEstimate how many different items you have seen using about a kilobyte, however many there are. Duplicates are free.Used in: Redis: PFADD and PFCOUNT, using at most 12 KB with a standard error of about 0.81%
- Geofence enter and exit eventsIntermediate35 minTest points against polygons with ray casting, prefilter with a bounding box, and turn noisy location pings into stable enter and exit events.Used in: Delivery and ride apps: 'driver arrived' and 'entered the airport zone'
Storage
How databases keep data safe and find it fast.
0 of 3 finished
- Write-ahead logIntermediate40 minRecord every change in an append-only file before applying it, so a crash can be replayed, and a half-written last record is detected and ignored.Used in: PostgreSQL, MySQL and SQLiteOn your computer
- Bitcask in miniatureIntermediate40 minAn append-only data file plus an in-memory key directory: one-seek reads, tombstone deletes, merge and recovery on restart.Used in: Riak's original storage engineOn your computer
- B-treeIntermediate45 minA balanced tree with many keys in each node, so finding any key takes only a few steps. Nodes split as they fill and the tree stays level.Used in: Database indexes: the default index type in most relational databases is a B-tree or a close variant
Distributed
Many machines that must agree, or at least cope.
0 of 5 finished
- Consistent hash ringStarter30 minRoute keys to nodes so that adding or removing a node moves only a small share of the keys.Used in: Dynamo-style databases and Cassandra
- Snowflake-style ID generatorIntermediate35 minPack a timestamp, a worker id and a per-millisecond sequence into one sortable 64-bit number, and handle a clock that moves backwards.Used in: Twitter's Snowflake and variants used by other social and chat products
- Gossip protocolIntermediate35 minNodes spread an update by repeatedly exchanging state with a few random peers; the whole cluster converges in about log N rounds.Used in: Cassandra and Consul for cluster membership
- Lease leader election with fencingIntermediate45 minA leader holds a time-limited lease; fencing tokens stop a slow, stale leader from corrupting data after its lease expired.Used in: Lock and coordination services such as ZooKeeper and etcd
- Raft leader electionIntermediate45 minNodes elect a leader by majority vote with random timers. A crashed leader is replaced, and without a majority nobody can lead.Used in: etcd, the store behind Kubernetes, which runs Raft
Search
Finding the right thing quickly, and measuring whether it was right.
0 of 3 finished
- Autocomplete with a trieStarter25 minStore words in a tree of letters so every word with a given prefix sits under one branch, then suggest the most popular ones.Used in: Search boxes and address bars that suggest as you type
- Search evaluation metricsStarter30 minPrecision, recall, reciprocal rank and NDCG on judged results, checked against the worked example in the lesson.Used in: Evaluating Elasticsearch, Solr and in-house rankers
- Tiny search engineIntermediate45 minAn inverted index with tokenising and BM25 ranking: the core of a search engine in about fifty lines.Used in: Lucene, Elasticsearch, OpenSearch and Solr