Alex Xu · System Design Interview · built from scratch

The Machine
Room

Six chapters, six working machines in Go and Rust. Every machine below is live: four run the real libraries compiled to WebAssembly in your tab, two talk to deployed services. Push them until they break.

6 built · 19 queuedGo · Rustbenchmarks or it didn't happen
↓  SCROLL. START MASHING.
CH 1.4

Rate Limiter

rate-limiter-go · Go + Redis

Three algorithms, one question: how hard can you hit it before it says no? Set the budget, then hammer the button. Watch the bucket drain, the window slam shut, the log age out.

Allowed 200
0
Blocked 429
0
X-RateLimit-Remaining
5
Retry-After
—
Implemented in the repoWhat it costs you
Lua-atomic Redis counterskills the INCR/EXPIRE race — one round trip, no lost writes
In-memory fallback on Redis outageRedis down ≠ service down; limiting continues per instance
Per-key limitingIP · API key · user ID, same interface
net/http + Gin middlewarePrometheus counters wired in behind both
CH 1.5

Consistent Hashing

consistent-hashing-go · consistent-hash-rs

240 keys live on this ring. Kill a node and watch what moves — then look at what plain hash % N would have moved instead. That gap is what the chapter is about.

Nodes
4
Keys moved
—
hash % N would move
—
Worst node load
—

Ring positions from CRC-32 hashing · owner = first virtual node clockwise · O(log n) binary search on a sorted ring · sync.RWMutex in Go, RwLock in Rust for read-heavy traffic.

CH 1.7

Unique ID Generator

uid-generator-go · uid-generator-rs

64 bits, no coordination, no database round trip. Hold the button down and watch the sequence counter climb inside a single millisecond — that's the only thing standing between you and a collision.

↑ hold it down
BIT LAYOUT — live
Generated
0
Collisions
0
Peak seq / ms
0
Throughput
—

Machine ID from env var, hostname or IP hash · custom epoch · clock-skew guard refuses to move backwards · sequence overflow spins to the next millisecond rather than repeating.

CH 1.8

URL Shortener

url-shortener-go · Postgres shards + Redis cache

Paste anything long. It comes back as a random six-character Base-62 code, stored on a Postgres shard picked by the ring from Chapter 5. Then resolve it and see the round trip.

THE SQUEEZE
ShortShardClicksLast lookup
Nothing shortened yet — squeeze something.

6 chars from crypto/rand over 0-9A-Za-z · consistent-hash sharding across Postgres shards (the Ch. 5 ring) · read-through Redis cache · click analytics with GeoIP · expiry + a token-bucket limiter on writes.

CH 1.13

Search Autocomplete

autocomplete-rs · Rust → wasm

50,000 English words, weighted by real Google web-corpus counts. The radix trie answers exact prefixes in microseconds. Then misspell one on purpose and watch what forgiveness costs.

↑ then break the spelling
TOP 10 BY FREQUENCY — exact prefix
Query time
—
Terms
—
Trie nodes
—
Edge labels
—

 

Radix trie over bytes · every node caches max_subtree_freq, so top-K skips any subtree that cannot beat the heap cutoff · one Levenshtein DP row carried down the walk, abandoned once its minimum exceeds the budget. Plain Levenshtein, not Damerau — so a transposition like recieve → receive costs two edits, and at a budget of one the trie hands you relieve instead.

Query path only. The RocksDB write-behind and the multi-tenant engine need a server and are not demoed here — see the chapter repo.

CH 2.10

Live Leaderboard

leaderboard-go · Redis sorted sets + Postgres

Pick a name, then click. Every click is a point, and everyone is ranked by how many they've racked up — today, this week, all time. Your rank is pushed to you over a WebSocket, not polled, and every rank is one ZREVRANK: O(log n), at twenty players or a million.

TOP 10 BY CLICKS — global · daily · connecting…
RankPlayerClicks
waiting for the first snapshot…
Your rank
—
Your clicks
—
Top
—
Write → push
—
Pushes received
0

 

One Lua script writes all six boards a click lands on — daily, weekly, all-time × global, regional — atomically · midnight resets by key name + EXPIREAT, no cron · ties go to whoever got there first, packed into the float64 score · Postgres event log is the truth, Redis is rebuilt from it · pushes coalesce per board every 100 ms · writes rate-limited by the Ch. 4 limiter.

Clicks are counted in your tab and sent as one write every 1.5 s — the Ch. 4 limiter allows 60 writes a minute, and a click per request would trip it within seconds. Friends-only boards exist in the API but are not exposed here.