All lessons

How the lessons are tested

Checked against the real thing.

An animation can be internally consistent and still teach the wrong behavior. So the lessons' scenarios also run against the real software, configured with the exact snippet the lesson shows, and a test fails whenever the animation and the real program disagree.

Checks
323
Lessons covered
19 of 25
Status
All agree
Last run
2026-09-29

nginx 1.31.6HAProxy 3.4.6Redis 7.2.5PostgreSQL 14.15MySQL 9.2.0Node 22.22.0Chrome 154OpenSSL 4.0.2OpenJDK 18.0.2.1

  1. 01

    Same scenario, twice

    A test takes a lesson step, say “web-2 crashes under load”, and runs it through the lesson's own simulation, the code that draws the animation.

  2. 02

    Then on the real program

    The same step runs against real nginx, HAProxy, Redis, PostgreSQL, MySQL, Node or Chrome, using the config printed in the lesson. The only edits are mechanical: example IPs become local ports, certificates become a throwaway test one.

  3. 03

    Compare, and fix the lesson

    Status codes, ordering, timing, which server got what. When they disagree, the lesson changes, not the test. Snippets that pass carry a tested on … badge.

What the tests caught

Every check, lesson by lesson

Open a lesson to see each comparison: what the real program did, next to what the lesson shows.

SD-01

Load Balancing

32 checks · all agree
CheckReal program didLesson shows
Every config snippet in the lesson loads (9)nginx 1.31.6, HAProxy 3.4.69 of 9 accepted—
nginx accepts every config the playground generatesnginx 1.31.672 variants accepted—
Round robin ordernginx 1.31.6web-1 web-2 web-3 web-1 web-2 web-3 web-1 web-2 web-3web-1 web-2 web-3 web-1 web-2 web-3 web-1 web-2 web-3
The whole idea: upstream + proxy_pass, over HTTPSnginx 1.31.6web-1 web-2 web-3 web-1 web-2 web-3web-1 web-2 web-3 web-1 web-2 web-3
web-2 still learns who the visitor isnginx 1.31.6web-2 saw X-Forwarded-For: 127.0.0.1, Host: shop.exampleadds X-Forwarded-For with the visitor address
Round robin sends every heavy request to web-1nginx 1.31.6heavy requests went to web-1, web-1, web-1, web-1heavy requests went to web-1, web-1, web-1, web-1, web-1, web-1, web-1, web-1, web-1, web-1, web-1
Weighted order, request by requestnginx 1.31.6web-1 web-1 web-2 web-1 web-3 web-1 web-1 web-1 web-2 web-1 web-3 web-1web-1 web-1 web-2 web-1 web-3 web-1 web-1 web-1 web-2 web-1 web-3 web-1
Same IP, same server, every timenginx 1.31.6Alice (198.51.100.42) → web-1; Bob (192.0.2.7) → web-1; Carol (198.51.100.73) → web-1Alice → web-2 (toy sum-of-numbers hash); Bob → web-1 (toy sum-of-numbers hash); Carol → web-3 (toy sum-of-numbers hash)
ip_hash groups a whole /24 togethernginx 1.31.6Alice and Carol (same first three numbers) both went to web-1the aside notes that NGINX only hashes the first three numbers
Going from 3 to 4 serversnginx 1.31.6ip_hash moved 78% of 200 visitors; hash … consistent moved 27%toy hash moves 79% ("almost every user"); consistent hashing about 1 in N
What users get while the dead server is still in rotationHAProxy 3.4.61 requests failed with 503, each after about 3.0 s of retries503 (after showing the retries)
Failed checks before the server is pulledHAProxy 3.4.63 (DOWN 5.9 s after the crash)3 (pulled 5.5 s after the crash)
Passing checks before it gets traffic againHAProxy 3.4.62 (UP 3.1 s after the reboot)2 (back 4.1 s after the reboot)
Every server downHAProxy 3.4.6503503 for everyone
With option redispatch, a refused request goes to another serverHAProxy 3.4.6200 web-1, 200 web-3, 200 web-3, 200 web-1, 200 web-1, 200 web-1; slowest 1 msHAProxy retries on another server too if you add option redispatch
One server crashes, the others are finenginx 1.31.60 errors in 118 requests; the retry cost at most 3 ms0 errors in 45 requests (NGINX retries the next server)
Every server downnginx 1.31.6502502
How often NGINX tries a dead server againnginx 1.31.62 attempts in 13 s at 5 req/s, 11 s apart (the first at +-0 s)skipped for 10 s after each refusal
One connection, one serverHAProxy 3.4.6mode tcp: six requests on one connection all went to web-2; mode http: web-1, web-2, web-3, web-1, web-2, web-3all of Alice's requests follow her connection (web-1 ×13)
/api/ and /media/ go to different poolsnginx 1.31.6/api/cart → web-1, web-2, web-1, web-2; /media/hero.jpg → web-3, web-3/api/* → web-1, web-2; /media/* → web-3
New requests avoid the server that is still busynginx 1.31.6web-1 busy with a slow request; the next six went to web-2, web-3, web-2, web-3, web-2, web-3new requests flow to web-2 and web-3 until web-1 catches up
A request in progress when its server diesHAProxy 3.4.6502 after 0.4 s502
A GET in progress when its server diesnginx 1.31.6200, finished by web-2 after 2.4 s200 (retried on another server)
SD-02

Caching

42 checks · all agree
CheckReal program didLesson shows
redis-server starts with the redis.conf snippet and reads back every valueRedis 7.2.5maxmemory 2147483648, maxmemory-policy allkeys-lru, maxmemory-samples 5maxmemory 2gb, maxmemory-policy allkeys-lru, maxmemory-samples 5
redis-server starts with the redis.conf snippet and reads back every valueRedis 7.2.5maxmemory-policy allkeys-lfu, lfu-log-factor 10, lfu-decay-time 1maxmemory-policy allkeys-lfu, lfu-log-factor 10, lfu-decay-time 1
redis-server starts with every config the playground generatesRedis 7.2.5576 variants accepted; policies used: allkeys-lfu, allkeys-lru, allkeys-random, volatile-ttlthe "your settings · live" block is a redis.conf
Default eviction policyRedis 7.2.5noevictionnoeviction
How often the background expiry job runs (hz)Redis 7.2.510 times a secondabout 10 times a second
Default policy with memory fullRedis 7.2.5writes: 200000 | 200000 | OOM command not allowed when used memory > 'maxmemory'. | OOM command not allowed when used memory > 'maxmemory'.; reads still work, DEL still works; evicted_keys 0noeviction: when memory is full, writes fail with an OOM error
Redis has no FIFO policyRedis 7.2.5allkeys-fifo rejected; the only choices: volatile-lru, volatile-lfu, volatile-random, volatile-ttl, allkeys-lru, allkeys-lfu, allkeys-random, noevictionRedis has no FIFO policy
volatile-ttl when no key has a TTLRedis 7.2.5writes OK OOM OOM, evicted_keys 0: nothing it may evictFIFO without TTLs maps to allkeys-random
volatile-lru only evicts keys that have a TTLRedis 7.2.5writes OK OK OK OK OOM; left: plain:0, plain:1, plain:2, plain:3, plain:4, plain:5, plain:6 (all three TTL keys evicted, then OOM)volatile-lru (only keys that have a TTL)
Memory for 100,000 keys, with and without LRU evictionRedis 7.2.5dataset 7263480 bytes under noeviction, 7263480 under allkeys-lruit needs no extra memory per key
--latency outputRedis 7.2.5min: 0, max: 1, avg: 0.09 (209 samples) (Redis counted 209 PINGs)min: 0, max: 1, avg: 0.21 (1532 samples); the note: --latency pings Redis over and over
GET product:42 in redis-cliRedis 7.2.5"{\"name\":\"Trail boots\",\"price\":89}""{\"name\":\"Trail boots\",\"price\":89}"
Redis’s own time per GETRedis 7.2.50.13 µs per GET inside Redis (INFO commandstats); the keyspace is a hash table (Hash table 0 stats (main hash table):)about 1 ms, most of that is the network hop; Redis looks the key up in an in-memory hash table
SCAN outputRedis 7.2.51) "0" / 2) 1) "product:42" / 2) "product:13" / 3) "product:7"1) "0" / 2) 1) "product:42" / 2) "product:7" / 3) "product:13"
KEYS blocks, SCAN doesn’t (1,000,000 keys)Redis 7.2.5KEYS took 188 ms inside Redis and a PING sent meanwhile waited 177 ms; each SCAN COUNT 100 took 28 µs and returned 100 keys on averageAvoid KEYS in production: it blocks Redis while it scans everything
INFO stats | grep keyspace after the hit-or-miss readsRedis 7.2.5keyspace_hits:6, keyspace_misses:4the same reads in the animation: 6 hits, 4 misses; the snippet's math: 95.0%
SET … EX 60Redis 7.2.5OKOK
TTL counts downRedis 7.2.5(integer) 60 right away, (integer) 42 after 18.0 s(integer) 42 (18 s after the SET)
GET a minute laterRedis 7.2.5(nil)(nil)
TTL of a key with no expiry, and of a missing keyRedis 7.2.5(integer) -1, (integer) -2-1 and -2
How expired keys disappearRedis 7.2.5background job off: the expired key stayed (DBSIZE 1) until GET touched it (then 0); job on: 1,000 expired keys that nobody touched were gone 86 ms after expiringdeleted when something touches it; a background job cleans up the rest about 10 times a second
SET lock:product:42 <token> NX PX 5000 from two app serversRedis 7.2.5first OK, second (nil); after 5.1 s the second got OKthe winner runs the query; the others wait briefly and retry
Deleting a Redis keyRedis 7.2.50.02 ms per DEL, round trip included (localhost)You can delete a Redis key in a millisecond
GET and DEL replies in the race diagramRedis 7.2.5GET → (nil); DEL → (integer) 0 when not cached, (integer) 1 when cached(nil): a miss; DEL → 0: nothing to delete
Which keys LRU evicts, read by read (maxmemory-samples 10)Redis 7.2.510 of 10 runs: 7 hit, 99 miss→8, 42 hit, 3 miss→21, 13 hit, 64 miss→5, 5 miss→5, 17 miss→7, 42 hit, 11 miss→997 hit, 99 miss→8, 42 hit, 3 miss→21, 13 hit, 64 miss→5, 5 miss→7, 17 miss→99, 42 hit, 11 miss→3
Which keys LRU evicts, read by read (maxmemory-samples 5, the snippet)Redis 7.2.518 different outcomes in 30 runs; 1 matched the animation; 8 evicted product:5 the moment they wrote it; most common: 7 hit, 99 miss→8, 42 hit, 3 miss→21, 13 hit, 64 miss→5, 5 miss→13, 17 miss→7, 42 hit, 11 miss→99 (4×)7 hit, 99 miss→8, 42 hit, 3 miss→21, 13 hit, 64 miss→5, 5 miss→7, 17 miss→99, 42 hit, 11 miss→3 (the aside: real Redis doesn’t always agree, and sometimes evicts product:5 the moment it’s written)
Hits on these ten readsRedis 7.2.54 to 5 hits across all runs4 hits
OBJECT IDLETIMERedis 7.2.5a key just read: (integer) 0; product:3, last read 6 s earlier: 6seconds since a key was last read or written (0 right after a read)
Hit ratio over 12,000 reads (6 of 16 products fit)Redis 7.2.5maxmemory-samples 5: 65.7%; maxmemory-samples 10: 65.1% (reads placed 10 s apart on Redis’s LRU clock); evicted_keys 4133 / 4197exact LRU (the animation’s cache): 65.7%; the aside: within about two points of exact LRU’s 66%
How often the hot key product:42 gets evictedRedis 7.2.5Redis: 157× (samples 5), 146× (samples 10) in 12,000 readsexact LRU: 178× in 12,000 reads; the animation, 10 minutes: evicted 3×, at the very bottom 3.4% of the time, back on its next read: always
LFU on stable popularityRedis 7.2.5allkeys-lfu, six runs: 72.5%, 71.6%, 70.8%, 61.3%, 64.5% (product:42 evicted 12×), 72.7%; allkeys-lru: 65.7%LFU wins when popularity is stable (the readout’s exact LFU: 74.8%); Redis’s LFU scores below that, though it usually still beats LRU
FIFO in Redis: volatile-ttl with one TTL on every keyRedis 7.2.5volatile-ttl: 60.5% hits, product:42 evicted 595×, 72.1% of evictions the same key, read for read, as FIFOthe animation’s FIFO: 60.5%, product:42 evicted 594×; the note: volatile-ttl acts much like FIFO when every key has the same TTL
Without TTLs the playground falls back to allkeys-randomRedis 7.2.5allkeys-random: 58.0% hits, product:42 evicted 682× (the pick depends on the hash table's layout, so this varies from run to run)no FIFO in Redis; without TTLs, this evicts at random instead
LFU count after N reads (lfu-log-factor 10)Redis 7.2.5100: 12, 1,000: 20, 10,000: 50, 100,000: 141, 1,000,000: 255grow ever more slowly: about 10 after 100 reads, 255 (the max) by a million
An idle key’s count when a minute passes (lfu-decay-time 1)Redis 7.2.527 → 26 after 1 minute boundarydrops by 1 each minute
INFO memory: the fields shown, in order, "…" where lines are skippedRedis 7.2.5used_memory_human:2.50M, maxmemory_human:2.00G (maxmemory 2gb)used_memory_human:1.83G, maxmemory_human:2.00G
INFO stats | grep evicted_keysRedis 7.2.5evicted_keys:110 after 110 evictionsevicted_keys:48213
Out of the box: snapshots, and a crash after oneRedis 7.2.5save "3600 1 300 100 60 10000", appendonly no; after 10,000 writes a snapshot came 61 s after startup; killed with 100 more writes, it restarted with 10000 keys and 0 of the 100snapshots at most once a minute, and a crash loses every write since the last snapshot
Append-only file on, then kill -9Redis 7.2.5appendfsync everysec; 20000 writes acknowledged, 20000 there after the restarta killed Redis came back with every write it had acknowledged, though by default it syncs that file to disk only once a second
No snapshot to reloadRedis 7.2.5killed with 1,000 keys, restarted with 0Redis comes back cold, because this one has no snapshot to reload
SD-03

CDNs

22 checks · all agree
CheckReal program didLesson shows
Every config snippet in the lesson loads (4)nginx 1.31.64 of 4 accepted—
Alice misses, Bob hitsnginx 1.31.6MISS HIT; the origin was asked 1 time(s) for three requests1 miss then 1 hit; origin fetches: 1
public, max-age=31536000, immutable: keptnginx 1.31.6MISS HIT HITMISS HIT HIT
No Cache-Control: proxy_cache_valid decidesnginx 1.31.6MISS HIT HITproxy_cache_valid 200 10m applies "when the origin doesn't say"
s-maxage is for shared cachesnginx 1.31.6max-age=0, s-maxage=60: MISS HIT HITs-maxage: a max-age just for shared caches like CDNs
no-cache at an nginx edgenginx 1.31.6MISS MISS MISS: never stored, 3 full fetches (0 conditional)the animation's edge keeps it and revalidates (MISS REVAL REVAL); the aside says nginx won't store no-cache at all
private passes straight throughnginx 1.31.6MISS MISS MISS; orders of Carol / orders of Dave / orders of CarolBYPASS BYPASS BYPASS: only the browser may keep it
no-store: nobody keeps a copynginx 1.31.6MISS MISS MISSno-store: nobody keeps a copy, anywhere
A stale copy is checked with the origin, not fetched againnginx 1.31.6MISS HIT REVAL HIT; origin saw: GET → 200, If-None-Match "a1b2" → 304; the stale request took 153 msMISS HIT REVAL HIT: If-None-Match "a1b2", 304 Not Modified
stale-while-revalidate answers at once and refreshes behind the scenesnginx 1.31.6MISS HIT STALE HIT; the stale answer took 1 ms; origin saw: GET → 200, If-None-Match "a1b2" → 304MISS HIT STALE HIT: the stale copy goes out at once while the edge refreshes
private: Carol and Dave each get their own pagenginx 1.31.6orders of Carol (MISS), orders of Dave (MISS)0 leaks: every request goes to the origin
public, max-age=60: Dave gets Carol's ordersnginx 1.31.6orders of Carol (MISS), orders of Carol (HIT)7 leaks in 20 s: the edge serves the first page it saw to both
The edge keeps a warm connection to the originnginx 1.31.63 origin fetches over 1 connection(s)the edge keeps warm connections open to your origin
Default key ($scheme$proxy_host$request_uri): every tracking tag is a new copynginx 1.31.6MISS MISS HITeach variation misses (hit ratio 0% in the animation)
proxy_cache_key $scheme$proxy_host$uri: the variants share one copynginx 1.31.6MISS HIT HIT (judged by what reached the origin: 1 request(s) for 3)all variants share one key (hit ratio 96%)
Vary: User-Agent keeps one copy per browsernginx 1.31.64 user agents, twice each: MISS MISS MISS MISS HIT HIT HIT HITVary: User-Agent can split one file into thousands of copies
name.hash.js: cached for a yearnginx 1.31.6app.3f9a.js and app.8c1e.js: Cache-Control: public, max-age=31536000, immutable / public, max-age=31536000, immutablepublic, max-age=31536000, immutable
The HTML: always checknginx 1.31.6Cache-Control: no-cache; a check with its ETag got 304no-cache: checked with the origin on every visit
Requests that skip the CDN are refusednginx 1.31.6from 198.51.100.7 (the CDN): 200; from 203.0.113.99: 403; from 127.0.0.1: 403; the app saw 1only the CDN's published ranges get through
SD-04

Consistent Hashing

4 checks · all agree
CheckReal program didLesson shows
Remove one of five servers: hash $uri consistentnginx 1.31.6moved 19%, 21%, 17%, 19%, 23% of 600 URIs (one run per server removed)about 1/N = 20%; the lesson's one-seat ring moves 19%, 22%, 16%, 20%, 23% (average 20%)
Remove one of five servers: plain hash $uri (mod N)nginx 1.31.6moved 81%, 81%, 82%, 81%, 79%about 4 in 5; the lesson's hash mod N moves 80%
cache-b crashes but stays in the listnginx 1.31.6consistent: 21% moved, 0 of them not cache-b's; plain hash: 20% moved, 0 not cache-b's; errors: 0its keys continue to the next server; every other key stays (the note: a crashed server that stays listed only loses its own keys)
SD-05

Database Replication

39 checks · all agree
CheckReal program didLesson shows
PostgreSQL accepts every setting in the postgresql.conf snippetPostgreSQL 14.15wal_level = replica; max_wal_senders = 10; primary_conninfo = host=10.0.2.11 user=replicator application_name=db2wal_level = replica; max_wal_senders = 10; primary_conninfo = 'host=10.0.2.11 user=replicator application_name=db2'
PostgreSQL accepts every setting in the postgresql.conf · on the leader snippetPostgreSQL 14.15synchronous_standby_names = FIRST 1 (db2); synchronous_commit = on; synchronous_commit = remote_apply; synchronous_commit = localsynchronous_standby_names = 'FIRST 1 (db2)'; synchronous_commit = on; synchronous_commit = remote_apply; synchronous_commit = local
PostgreSQL accepts every setting in the replication settings snippetPostgreSQL 14.15synchronous_standby_names = FIRST 1 (db2, db3); synchronous_standby_names = ANY 1 (db2, db3, db4)synchronous_standby_names = 'FIRST 1 (db2, db3)'; synchronous_standby_names = 'ANY 1 (db2, db3, db4)'
PostgreSQL accepts every config the playground showsPostgreSQL 14.1519 configs, 11 distinct settings, all accepted (e.g. synchronous_standby_names = 'FIRST 1 (db2)'; synchronous_standby_names = 'FIRST 1 (db3)'; synchronous_standby_names = 'FIRST 1 (db1)')—
A follower set up with the lesson’s lines streams and replaysPostgreSQL 14.15db2 streams as application_name db2 (user replicator, streaming); the $50 deposit showed up on db2 (alice = 150); 2 WAL sender processes for 2 followersthe leader streams its log; each follower replays it in order; one sender process per follower
Followers answer reads but refuse writesPostgreSQL 14.15read alice = 150; UPDATE → ERROR 25006: cannot execute UPDATE in a read-only transactionFollowers can’t take writes, but they can answer reads.
standby.signal: "you are a follower"PostgreSQL 14.15db2 (with the file) is in recovery: t; the same kind of clone without it started as a leader (pg_is_in_recovery() = f)...plus an empty standby.signal file: "you are a follower"
What pg_basebackup -R writes for youPostgreSQL 14.15an empty standby.signal and primary_conninfo (host, port, user…); application_name only when passed with -d; without it the follower streamed as "walreceiver", and FIRST 1 (db3) left a commit waitingpg_basebackup -R writes the follower settings; the note: application_name only if you pass one, otherwise commits just wait
Every write moves the log position forwardPostgreSQL 14.15pg_current_wal_lsn() 0/175E098 → 0/175E108 after the UPDATE: 112 bytes of logLSNs are byte positions in the log, like 0/3000148
A follower held up by a long query falls behindPostgreSQL 14.15db3's replay waited for the report; worst replay_lag: db2 0.000 s, db3 2.31 s; the lesson's query returned 127.0.0.1 | streaming | ; 127.0.0.1 | streaming | 00:00:02.309829db3 lags (80 ms vs 15 ms for db2); lag grows to seconds while a long-running query holds up replay
Alice reads her balance from a lagging follower right after depositingPostgreSQL 14.15deposit UPDATE 1 on the leader; db-3 (behind) returned 100, db-2 returned 150"What does db-3 return?" → old: db-3 has only replayed up to #100. The deposit is #102, still on its way, so Alice sees $100.
Waiting for her LSNPostgreSQL 14.15pg_last_wal_replay_lsn() >= '0/4000128': f on db-3, true on db-2; once db-3 passed it (1 ms after replay resumed) the read returned 150stale reads: any follower 2/4, leader after a write 0/4, wait for her LSN 0/4
Monotonic reads: random followers vs. one followerPostgreSQL 14.15db-2, db-3, db-2: 125 → 105 → 125 (back 1×); always db-3: 105 → 105 → 130 → 135 (back 0×)random follower: back in time 3× in 40 s; the same follower: 0×
Asynchronous is the defaultPostgreSQL 14.15PostgreSQL: synchronous_standby_names = '', the follower's sync_state is async; MySQL 9.2.0: rpl_semi_sync_source_enabled defaults to FALSEAsynchronous, the default in Postgres and MySQL
synchronous_commit = onPostgreSQL 14.15with db2's replay paused the commit returned in 5 ms: db2 had flushed it (true) but a read there still showed 100on: wait until db2 has flushed the entry to disk
synchronous_commit = remote_applyPostgreSQL 14.15waited while replay was paused (true), returned after it resumed; right after, db2 showed bob = 81remote_apply: wait until db2 has replayed it (so reads there see it)
synchronous_commit = localPostgreSQL 14.15with db2 down, the commit returned in 0 mslocal: don’t wait for any standby at all
Copies that exist when COMMIT comes backPostgreSQL 14.15sync: only after db2 had the entry (2 copies); async: in 1 ms with db2 down (1 copy)sequence diagram: 2 copies at COMMIT when sync, 1 when async
The next writes after db-2 crashesPostgreSQL 14.15both still waiting after 1.5 s, no error; the lesson's query: 76551 | SyncRep | active; 76552 | SyncRep | activeprediction: "What happens to this write?" → wait (The leader promised never to confirm a write until db-2 has it. So it wrote the change locally and now it waits. Every write after it waits too.); commits stalled (1 → 1), 7 waiting, 0 errors
db-2 comes backPostgreSQL 14.15both commits returned (UPDATE 1, UPDATE 1) 117 ms after db-2 was startedthe stuck writes commit in a burst (8 commits right after db-2 revives)
Clearing synchronous_standby_namesPostgreSQL 14.15a stuck commit returned right after the reload (UPDATE 1)releases every stuck commit at once (5 waiting → 0)
Cancelling a stuck commitPostgreSQL 14.15WARNING: canceling wait for synchronous replication due to user request DETAIL: The transaction has already committed locally, but might not have been replicated to the standby. (the client still got UPDATE 1)The transaction has already committed locally, but might not have been replicated to the standby.
FIRST 1 (db2, db3) with every follower upPostgreSQL 14.15db2 sync, db3 potential, db4 asyncdb2 sync, db3 potential, db4 async
db-2 crashesPostgreSQL 14.15db3 sync, db4 async; the next commit took 10 msdb3 sync, db4 async; 4 commits in the next 5 s
db-2 and db-3 both downPostgreSQL 14.15a commit waited (true) until db-3 came back, then returned0 commits in 5 s: writes stall again
ANY 1 (db2, db3, db4)PostgreSQL 14.15db2 quorum, db3 quorum, db4 quorum; with db2 and db3 down a commit took 3 ms: db4 was enoughdb2 quorum, db3 quorum, db4 quorum; 4 commits in 5 s with db2 and db3 down
Which follower to promotePostgreSQL 14.15received/replayed: db2 0/40056B0 / 0/4005640, db3 0/40055D0 / 0/40055D0; promoted db2 (pg_promote → t); it replayed the entry it had received but not applied (alice 140, bob 100)promote the follower with the most complete log (the animation: "db-3 has #105, the newest log position. Promoting it throws away the fewest writes."); promotion applies everything it received (applied #109 of #109)
Async: confirmed writes that never left the leaderPostgreSQL 14.15db-1 confirmed 2 more writes (UPDATE 1, UPDATE 1; alice 200, bob 70) that no follower had; after the failover db-2 has alice 140, bob 100: 2 confirmed writes goneasync replay: 1 confirmed write(s) lost in the failover
The failover starts a new termPostgreSQL 14.15db-2 now runs on timeline 2 (was 1); db-3, re-pointed at it, caught up (alice 140, bob 100)every failover bumps the term (term 2 after the promotion)
The old leader reboots as a leaderPostgreSQL 14.15pg_is_in_recovery() = f; app-2's write → UPDATE 1; the promoted db-2 took a write too (UPDATE 1); db-1 has alice 200, bob 77; db-2 has alice 100, bob 1002 leaders, each with its own balances
pg_rewind without --write-recovery-confPostgreSQL 14.15rewound (exit 0) and started with pg_is_in_recovery() = f: a leader againnote: without --write-recovery-conf, the rewound node starts up as a leader again
pg_rewind needs wal_log_hintsPostgreSQL 14.15on a copy last run with wal_log_hints = off: pg_rewind: fatal: target server needs to use either data checksums or "wal_log_hints = on"note: pg_rewind needs wal_log_hints = on (or data checksums), set before the failover
The lesson’s pg_rewind commandPostgreSQL 14.15exit 0; db-1 came back with pg_is_in_recovery() = t, following db-2: alice 100, bob 100 (db-2: 100, 100); its own writes are gone; app-2's write → ERROR 25006 cannot execute UPDATE in a read-only transactiondb-1 steps down (follower), throws away what nobody else has, follows db-2 and refuses writes; 1 leader, 1 read-only error(s)
Sync to db-2: the failover keeps every confirmed writePostgreSQL 14.153 confirmed writes, all on db-2 after the failover (alice 130); the write caught mid-commit got an error (57P01: terminating connection due to immediate shutdown command)sync replay: 0 confirmed writes lost; 11 writes failed loudly with an error
Crash, then restartPostgreSQL 14.15while down: connect ECONNREFUSED 127.0.0.1:65335; after the restart all 200 committed deposits were there (alice 300)every query fails while it's down (8 errors); after a restart the data is intact, only downtime was lost
Two leaders inserting at the same timeMySQL 9.2.0us-east got 1, 3, 5, eu-west 2, 4, 6; after replicating both ways each had 1:us 2:eu 3:us 4:eu 5:us 6:eu, no duplicate-key error; later inserts continued from the highest ID seen (us-east 7, eu-west 8)us-east: 1, 3, 5… (eu-west uses 2: 2, 4, 6…)
A write conflict between two MySQL leadersMySQL 9.2.0us-east ended with 120, eu-west with 150: each applied the other's row over its own; replication kept running with no error on either sidenote: MySQL doesn’t resolve conflicts; the leaders can end up with different balances and nothing reports an error (the animation's generic leaders use last write wins: both → $120)
Seconds_Behind_Source while a long transaction holds up the replicaMySQL 9.2.0Seconds_Behind_Source rose to 4 while a 3.5 s transaction on the replica held the row, then went back to 0SHOW REPLICA STATUS; -- Seconds_Behind_Source: 2
MySQL semi-sync, then the timeoutMySQL 9.2.0semi-sync ON with 1 replica; a commit took 15 ms. Replica stopped: the next commit took 10.0 s, semi-sync switched OFF, the one after took 15 ms; ON again once the replica reconnected. Default timeout 10000 ms. Without plugin-load-add, mysqld refuses the lines: unknown variable 'rpl_semi_sync_source_enabled=1'.the leader waits until at least one replica has the entry; gives up after a timeout (10 seconds by default) and falls back to async
SD-06

Sharding

13 checks · all agree
CheckReal program didLesson shows
PostgreSQL accepts every setting in the postgresql.conf · on each replica snippetPostgreSQL 14.15primary_conninfo = host=10.0.2.10 user=replicator; hot_standby = onprimary_conninfo = 'host=10.0.2.10 user=replicator'; hot_standby = on
A replica holds all the data and replays every writePostgreSQL 14.15after 20,000 inserts on the primary, orders (heap + index) is 1640 kB on both, byte for byte the same size; the replica served SELECT count(*) = 20000 and refused an INSERT (25006)every replica holds all of the data, so its disk is as full as the primary’s, and replays every write
The slice on shard-0PostgreSQL 14.15min, max on shard-0: alice | frank; 12 + 12 + 11 + 10 users over 4 shards, each exactly once-- alice | frank
queries.sql on four hash partitionsPostgreSQL 14.15customer_id = 1042 → 1 of 4 partitions; created_at >= '2026-09-01' → 4 of 4-- 1 shard / -- every shard
How many shards each prediction query needsPostgreSQL 14.15customer_id = 42 → 1; order_id = 90017 → 4; created_at >= '2026-09-01' → 4; customer_id = 1042 AND status = 'paid' → 1; ORDER BY total DESC LIMIT 5 → 4customer_id = 42 → 1; order_id = 90017 → all; created_at >= '2026-09-01' → all; customer_id = 1042 AND status = 'paid' → 1; ORDER BY total DESC LIMIT 5 → all
Range of created_at insteadPostgreSQL 14.15the report → 1 partition, customer_id = 42 → 4; 500 orders created today all went to orders_y2026the report asks 1 shard, Alice’s orders all 4, and every new write hits the 2026 shard
Give acme its own shardPostgreSQL 14.15acme → 0; UPDATE 1; acme → 4; everyone else unchanged (birch→1 cove→2 dune→3 echo→0 fern→1 gale→2 halo→3)acme → 0, then one entry flips to acme → 4; no other tenant moves
Merging partial resultsPostgreSQL 14.15avg over all rows 27.2060; sum of sums / sum of counts 27.2060; average of the four averages 402.0000sum of the sums / sum of the counts; you can’t average the averages
Needs max_prepared_transactions > 0PostgreSQL 14.15with the default: 55000: prepared transactions are disabledNeeds max_prepared_transactions > 0.
The coordinator crashes after both votesPostgreSQL 14.15phase 1: BEGIN, UPDATE 1, PREPARE TRANSACTION, BEGIN, UPDATE 1, PREPARE TRANSACTION; after the crash both shards still hold transfer-7731 and transfer-7731; a write to alice waited; after restarting shard-0 it was still prepared (transfer-7731) and writes still waited (true)the shards wait in doubt, holding their locks (animation: both "in doubt: wait", locks held)
Reads during the doubtPostgreSQL 14.15a plain SELECT on alice returned 100 in 3 ms while an UPDATE waitedAny write to Alice’s or Bob’s row is stuck behind those locks. Plain reads still get the old balances.
The coordinator restarts and commitsPostgreSQL 14.15COMMIT PREPARED, COMMIT PREPARED; the waiting write went through (UPDATE 1); alice 51, bob 130it finishes the commit; nothing lost (animation: COMMIT sent, locks released)
What the psql command printsPostgreSQL 14.15on a small table it printed "120 kB"; pg_size_pretty of 3212 GiB prints "3212 GB"3212 GB
SD-07

Rate Limiting

24 checks · all agree
CheckReal program didLesson shows
nginx accepts the nginx.conf snippetnginx 1.31.6accepted (limit_req_zone moved to the http level; wrapped in server {}; stub upstream backend)—
nginx accepts the nginx.conf snippetnginx 1.31.6accepted (added the "api" zone the lesson defined earlier; wrapped in server {}; stub upstream backend)—
An idle client sends 20 requests at oncenginx 1.31.611 passed, 9 rejected (each burst reached nginx within 1 ms)11 (burst + 1); the lesson's token bucket on the same arrivals: 11
Then a steady 10 requests a secondnginx 1.31.615 of 31 passed, one every 200 ms on average: 5.0 per second5 per second; the lesson's token bucket: 15 of 31
Every decision, request by requestnginx 1.31.642 passed, 34 rejected of 76; after a 1 s pause 5 of 10, after 2.5 s 11 of 15token bucket of 11 refilling at 5/s: 0 disagreements (0 decided on the 1 ms clock difference)
Status of the rejected requestsnginx 1.31.6429limit_req_status 429
The default status for a rejected requestnginx 1.31.611 × 200, 4 × 503default: 503
15 requests at oncenginx 1.31.61 passed, 10 delayed, 4 rejected (each burst reached nginx in the same millisecond)leaky bucket with 10 slots: 1 passed, 10 delayed, 4 rejected
Held requests go to the server at a fixed pacenginx 1.31.623 releases, 199–201 ms apart (average 200.0 ms)one every 200 ms ("exactly the configured rate")
Every decision and release time, request by requestnginx 1.31.62 passed, 22 delayed, 13 rejected; every release within 1 ms of the lesson's0 disagreements (0 decided on the 1 ms clock difference)
Status when the bucket is fullnginx 1.31.6429429 (limit_req_status 429)
How many client IPs a 10m zone remembersnginx 1.31.6remembered the last 81,017 of 200,000 IPv4 addresses, forgetting the oldest first (a 1m zone: 7,863)about 80,000
The Lua script on the animation's own request timesRedis 7.2.5EVALSHA allowed 430 and denied 589 of 1019 requests from 5 steps (times in whole ms)the animation allowed 430: 0 disagreements; limiter.ts on the same times: 0
The Lua script on random arrivalsRedis 7.2.51200 requests into buckets of 5 @ 1/s, 11 @ 5/s and 5 @ 0.5/slimiter.ts: 0 disagreements
20 requests at the same moment from 4 gateways, bucket of 5Redis 7.2.5as one Lua script (EVALSHA): 5 allowed; as separate HMGET and HSET calls: 20 allowedRedis executes the script without interleaving any other command
GET, then SET, from two gateways at onceRedis 7.2.5both read 4 and 4, 2 allowed, the counter says 5both read 4 and 4, "both" get through, the counter says 5
INCR from two gateways at onceRedis 7.2.5replies 5 and 6: 1 allowed, 1 refused; counter 6replies 5 and 6: gw-1 allows, gw-2 sends a 429
Local counters: the most the scraper got through in one 10 s windowRedis 7.2.515 (gateway.js against a separate Redis per gateway)15 ("15 per window, three times the limit"); 0 of 131 decisions differ
Shared counter in Redis: the most the scraper got through in one 10 s windowRedis 7.2.55 (gateway.js, three gateways, one Redis); the counter's TTL was 10 s5 ("the limit holds at 5"); 0 of 129 decisions differ
The boundary attack against the sliding logRedis 7.2.56 bursts; the second of each pair got 0/5, 0/5, 0/5 throughthe second burst is rejected in full; 0 of 30 decisions differ from the animation
The two timestamps and the expiry in the snippetRedis 7.2.5ZREMRANGEBYSCORE cuts at 1790424590000, ZADD scores 1790424600000: 10000 ms apart; PEXPIRE 10000; ZCARD of 4 then allowed 3 times10 s window; "4 in the window: room for 1"
Two servers run the commands at once, 4 in the windowRedis 7.2.5both counted 4 and 4, the set now holds 6"two servers can both count 4 and both add" without a Lua script
Headers on a 429 from limit_reqnginx 1.31.69 × 429 with headers: connection, content-length, content-type, date, servera bare 429, no Retry-After or RateLimit headers
client.js waits for Retry-After in either form, else jittersNode 22.22.0 & nginx 1.31.6Retry-After: 4 → waited 4000 ms; an HTTP date 3 s ahead → 2064 ms; no header → 445 ms (random); against limit_req: 200 after 1 retrieshonor Retry-After (seconds or HTTP date); otherwise full jitter
SD-08

Message Queues

1 check · all agree
CheckReal program didLesson shows
parseCharge on a good and a poison messageNode 22.22.0good → {"orderId":7,"amount":12.5}; "amount": "12", three tries → TypeError: bad amount: 12a message with a bad amount throws, every time
NET-01

What happens when you hit Enter

3 checks · all agree
CheckReal program didLesson shows
Cache-Control on the cart and on the assetsnginx 1.31.6/cart: no-store; /app.3f9a1c.css: public, max-age=31536000, immutable; /app.js: public, max-age=31536000, immutableCache-Control: no-store on /cart; max-age=31536000, immutable on app.css and app.js
A minute later, the connection is still opennginx 1.31.6/cart: no-store; after 64 s idle, the same connection answered again: /app.3f9a1c.css with public, max-age=31536000, immutablekeepalive_timeout 75s: the server hasn't closed it yet
NET-05

TLS & HTTPS

22 checks · all agree
CheckReal program didLesson shows
Every config snippet in the lesson loads (3)nginx 1.31.63 of 3 accepted—
nginx accepts every config the playground printsnginx 1.31.618 variants accepted—
TLS 1.3 by default, TLS 1.2 still allowednginx 1.31.6Protocol: TLSv1.3; New, TLSv1.3, Cipher is TLS_AES_256_GCM_SHA384; a TLS 1.2-only client got TLSv1.2ssl_protocols TLSv1.2 TLSv1.3
Round trips before the request can leave (after TCP)nginx 1.31.6TLS 1.3: 1, TLS 1.2: 2 (at 200 ms per round trip)TLS 1.3: 1, TLS 1.2: 2
First byte of the response (after TCP)nginx 1.31.6TLS 1.3: 2 round trips, TLS 1.2: 3TLS 1.3: 2, TLS 1.2: 3
TLS 1.2 sends the certificate in the clear, TLS 1.3 encrypts itnginx 1.31.6server's certificate readable on the wire: TLS 1.2 yes, TLS 1.3 noCertificate is "clear" in TLS 1.2, "hs" in TLS 1.3
Every encrypted TLS 1.3 record says "application data"nginx 1.31.610 records from nginx, types 22, 20, 23; after the ServerHello: 8 records, all type 23: true, all version 0x0303: true17 03 03 …: type 23, and the real type is hidden inside
The SNI travels in the clearnginx 1.31.6"shop.example" readable in both ClientHellosserver_name shop.example # the SNI (in the clear)
Coming back: resumed, and no certificate this timenginx 1.31.6Reused, TLSv1.3; certificate sent: first visit yes, return noboth sides skip the certificate: that's resumption
0-RTT: the request rides along with the ClientHellonginx 1.31.6Early data was accepted; the app got GET /account with Early-Data: 1 0.5 round trips after connectingrequest leaves after 0 TLS round trips; the answer comes in the server's first flight
Only safe requests as early datanginx 1.31.6POST /buy as early data: the app saw Early-Data: 1 and answered 425the app answers 425 Too Early if unsafe
A replayed 0-RTT first flight runs the request againnginx 1.31.6replaying Alice's first flight: the app ran GET /account (Early-Data: 1)answers GET early; GET /account served again
Without early data, a replayed hello runs nothingnginx 1.31.6replaying a resumed hello without early data: the app ran 0 request(s)no keys, no data: nothing runs
Each live config against the diagram's round tripsnginx 1.31.6http/full: first byte after 1 round trip(s) (diagram: 0 TLS round trips); tls12/full: TLSv1.2, return visit resumed: false, handshake 2 round trip(s) (diagram: 2 TLS round trips); tls12/ticket: TLSv1.2, return visit resumed: true, handshake 1 round trip(s) (diagram: 1 TLS round trips); tls13/full: TLSv1.3, return visit resumed: false, handshake 1 round trip(s) (diagram: 1 TLS round trips); tls13/ticket: TLSv1.3, return visit resumed: true, handshake 1 round trip(s) (diagram: 1 TLS round trips); tls13/0rtt: Early data was accepted (diagram: 0 TLS round trips)New: tickets off; Resumed: tickets on; 0-RTT: ssl_early_data on
Plain http only ever redirectsnginx 1.31.6< HTTP/1.1 301 Moved Permanently, < Location: https://shop.example/loginreturn 301 https://$host$request_uri
Every HTTPS answer carries the header, even an errornginx 1.31.6< HTTP/1.1 404 Not Found with < Strict-Transport-Security: max-age=63072000; includeSubDomains; preloadadd_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always
Once seen, http:// becomes https:// before anything is sentnginx 1.31.6Switched from HTTP to HTTPS due to HSTS => https://shop.example/login; connections to the port-80 server: 0; remembered for 729 days, subdomains toothe browser rewrites every http:// to https:// before sending anything; max-age=63072000 is two years
The abridged s_client output, line by linenginx 1.31.6, OpenSSL 4.0.2all 17 lines, in order (OpenSSL 4.0.2)17 lines of OpenSSL output
Mallory's three certificatesnginx 1.31.6, OpenSSL 4.0.2verify error:num=18:self-signed certificate | verify error:num=62:hostname mismatch | verify error:num=10:certificate has expiredverify error:num=18:self-signed certificate | verify error:num=62:hostname mismatch | verify error:num=10:certificate has expired
With ECH, the wire shows only the front server's namenginx 1.31.6 (ssl_ech_file), OpenSSL 4.0.2with ECH: ech=SUCCESS outer=cdn.example name=shop.example, without: ech=NOT_TRIED; "shop.example" on the wire: with ECH no, without yesouter SNI cdn.example · real hello encrypted · TLS 1.3 · x25519 share (ECH off: SNI shop.example · TLS 1.3 · x25519 share)
NET-06

HTTP/1.1 vs 2 vs 3

12 checks · all agree
CheckReal program didLesson shows
nginx accepts every config the playground printsnginx 1.31.68 variants accepted—
curl --http2 gets HTTP/2, chosen in the TLS handshakenginx 1.31.6HTTP/2 200; ALPN: server accepted h2; the same port also answered HTTP/1.1 200 OKlisten 443 ssl + http2 on: HTTP/1.1 and HTTP/2 over TCP
The response advertises HTTP/3nginx 1.31.6alt-svc: h3=":443"; ma=86400alt-svc: h3=":443"; ma=86400
nginx listens on UDP for QUIC (HTTP/3 itself not tested: no HTTP/3 client here)nginx 1.31.6nginx holds the same port number over UDPlisten 443 quic reuseport; # HTTP/3 over UDP
Only one listen line per address and port may say reuseportnginx 1.31.6two servers both saying reuseport: nginx: [emerg] duplicate listen options for 127.0.0.1:18443; only the first: acceptedonly one listen line per address and port may say reuseport
HTTP/2 header names are lowercasenginx 1.31.6server, date, content-type, content-length, last-modified, etag, alt-svc, accept-rangesHTTP/2 header names are always lowercase
Twelve files, one connectionnginx 1.31.612 responses (200) over 1 TCP connection(s), ALPN h2HTTP/2: 1 connection; HTTP/1.1: 6
Slices of different files arrive interleavednginx 1.31.66 of 12 files had other files' data arrive in the middle of themthe server interleaves slices of all the responses on the same connection
The browser numbers its streams 1, 3, 5…nginx 1.31.61, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23odd numbers from the browser, even ones belong to the server
TLS takes one round trip, the request anothernginx 1.31.6tcp 0.000195 tls 0.203258 first byte 0.405293 at 200 ms per round trip: TLS 1, request 1 (the relay can't slow down TCP's own handshake)tcp 0.101 tls 0.203 first byte 0.305 at 100 ms; the animation opens the connection after 2 (TCP + TLS)
What each live config does with two requests from curl --http2nginx 1.31.6h1.0: HTTP/1.1 200 OK, two files over 2 connection(s) (animation: 2 handshake(s)); h1.1: HTTP/1.1 200 OK, two files over 1 connection(s) (animation: 1 handshake(s)); h2: HTTP/2 200, two files over 1 connection(s) (animation: 1 handshake(s)); h3: HTTP/2 200, two files over 1 connection(s), Alt-Svc h3, UDP open (animation: 1 handshake(s))h1.0 closes after each response; h1.1 stays on HTTP/1.1; h2 and h3 speak HTTP/2 over TCP; h3 adds Alt-Svc and QUIC
NET-08

WebSockets

20 checks · all agree
CheckReal program didLesson shows
What the browser sends, and when onopen runsChrome 154GET with Upgrade: websocket, Connection: Upgrade, a 16-byte key, version 13, Origin, and the page's cookie; "connected" logged after the 101, readyState 1Upgrade + Connection, 16 random bytes base64, version 13, cookies sent; onopen runs after the handshake
Sec-WebSocket-Accept for the RFC’s sample keyNode 22.22.0s3pPLMBiTxaQ9kYGzzhZRbK+xOo=s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
A wrong Sec-WebSocket-AcceptChrome 154open: false, error: true, close code 1006the browser refuses the connection
readyState during the handshakeChrome 154before: 0, in onopen: 1CONNECTING (0), then OPEN (1) when the 101 checks out
The connection is cut without a close frameChrome 154close code 1006the browser sees it close (code 1006)
A ping from the serverChrome 154browser answered with opcode 10 carrying "hb-1"; messages seen by the page: 0the browser answers with a pong (opcode 10), automatically, hidden from JavaScript
A WebSocket to another originChrome 154page on 127.0.0.1 opened ws://localhost: true, Origin sent: http://127.0.0.1:61367the same-origin policy doesn’t stop other sites from opening WebSockets; check Origin
chat.js end to endChrome 154server got {"type":"join","room":42} then {"type":"say","text":"Sounds good"}; render() got "Lunch at noon?"; console: closed with code 1000join on open, render each message, send on click, log the close code
Reconnect backoff against a server that is downChrome 154waits: 0.96 s, 1.51 s, 3.80 sabout 1 s, then 2, 4… with random jitter (the code waits 50–100% of 1, 2, 4 s)
EventSource: a GET, events, and a reconnect after a dropChrome 154GET with Accept: text/event-stream; render got "Lunch at noon?"; after the server dropped the stream, the browser came back with Last-Event-ID: 18 and got "Sounds good"; POST body {"room":42,"text":"Sounds good"}an ordinary GET; if the stream drops, the browser reconnects by itself and sends Last-Event-ID
Seven EventSources to one HTTP/1.1 server, across two tabsChrome 154the server saw 6 streams at once; readyStates 1, 1, 1, 1, 1, 1, 0about six connections per server, across all tabs
long-poll.js against a server that answers 204, then newsChrome 154requests with after = 17, 17, 18; after the 204 it asked again 1 ms later; render got id 18204: ask again immediately; then render and move the cursor
The handshake: 101 Switching Protocolsnginx 1.31.6101, Upgrade: websocket, Sec-WebSocket-Accept correctthe upgrade request passes through; the server answers 101
Messages both ways, and a ping answered by a pongnginx 1.31.6opened: true; server said "hello from ws-2", then "echo: hi (ws-2)"; pongs back: 1a two-way pipe; the browser answers pings with pongs on its own
Without the Upgrade and Connection linesnginx 1.31.6the handshake failed; the chat server got a plain GET (Upgrade header: none) and answered 426nginx drops them (they are hop-by-hop) and the handshake fails
Restart ws-1: its users move to ws-2 and stay therenginx 1.31.6before: ws-1: 2, ws-2: 2; ws-1 closed its sockets with 1001, 1001; they reconnected to ws-2, ws-2; after ws-1 came back: ws-1: 0, ws-2: 4before: ws-1 2, ws-2 2; after the restart: ws-1 0, ws-2 4
proxy_read_timeout 3600s keeps an idle socket opennginx 1.31.6still openthe load balancer must allow long idle times
Without it, nginx cuts an idle socket at 60 snginx 1.31.6closed after 60 s of silence, code 1006the 60-second default of nginx's proxy_read_timeout; the browser sees code 1006
Pings every 20 s keep it open under the 60 s defaultnginx 1.31.6still open; 3 pongs came backreal servers ping every 20 to 30 seconds, comfortably inside the 60-second default
CS-01

Processes vs Threads

3 checks · all agree
CheckReal program didLesson shows
worker_threads: the sum, and four at onceNode 22.22.0printed 333333283333717100000; one worker 202 ms, four at once 201 msreal threads, each with its own heap; the result comes back as a message
worker_processes autonginx 1.31.61 master (1 thread) + 15 workers on 15 cores, 1 thread each; event method kqueuea master and one single-threaded worker per core (the animation: 1 master + 2 workers on 2 cores)
CS-02

The Event Loop

49 checks · all agree
CheckReal program didLesson shows
greet.js: console outputChrome 154, Node 22.22.0Chrome ×3: Hi, Ada; Node: Hi, Ada ×8Hi, Ada
checkout.js: console outputChrome 154, Node 22.22.0Chrome ×2: Order placed | Show receipt | Email sent; Node: Order placed | Show receipt | Email sent ×8Order placed | Show receipt | Email sent
zero.js: console outputChrome 154, Node 22.22.0Chrome ×3: A | C | B; Node: A | C | B ×8A | C | B (quiz answer "A C B")
timers.js: console outputChrome 154, Node 22.22.0Chrome ×3: 3 | 2 | 1; Node: 3 | 2 | 1 ×83 | 2 | 1 (quiz answer "3 2 1")
busy.js: console outputChrome 154, Node 22.22.0Chrome ×3: Y | X; Node: Y | X ×8Y | X (quiz answer "Y X")
queues.js: console outputChrome 154, Node 22.22.0Chrome ×3: A | D | C | B; Node: A | D | C | B ×8A | D | C | B
classic.js: console outputChrome 154, Node 22.22.0Chrome ×3: script start | script end | promise1 | promise2 | setTimeout; Node: script start | script end | promise1 | promise2 | setTimeout ×8script start | script end | promise1 | promise2 | setTimeout
nested.js: console outputChrome 154, Node 22.22.0Chrome ×3: sync | m1 | m2 | t1 | t2; Node: sync | m1 | m2 | t1 | t2 ×8sync | m1 | m2 | t1 | t2
drain.js: console outputChrome 154, Node 22.22.0Chrome ×3: micro 1 | micro 2 | micro 3 | timeout; Node: micro 1 | micro 2 | micro 3 | timeout ×8micro 1 | micro 2 | micro 3 | timeout
animate.js: console outputChrome 154Chrome ×3: tasktask
checkout.js: console outputChrome 154, Node 22.22.0Chrome ×3: 1 · checkout starts | 2 · back in the script | 3 · after await; Node: 1 · checkout starts | 2 · back in the script | 3 · after await ×81 · checkout starts | 2 · back in the script | 3 · after await
cart.js: console outputChrome 154Chrome ×3: loading… | page is interactive | status 200loading… | page is interactive | status 200
order.js: console outputChrome 154, Node 22.22.0Chrome ×3: 1 | 5 | 2 | 4 | 3; Node: 1 | 5 | 2 | 4 | 3 ×81 | 5 | 2 | 4 | 3 (quiz answer "1 5 2 4 3")
nested-await.js: console outputChrome 154, Node 22.22.0Chrome ×3: a1 | b | end | a2; Node: a1 | b | end | a2 ×8a1 | b | end | a2 (quiz answer "a1 b end a2")
sleep.js: console outputChrome 154, Node 22.22.0Chrome ×3: f1 | s | m | f2; Node: f1 | s | m | f2 ×8f1 | s | m | f2 (quiz answer "f1 s m f2")
What late.js prints after the 300 ms busy loopChrome 154, Node 22.22.0Chrome: 304; Node: 301, 302 (always after "loop done")the animation prints "loop done", "timer 300"; the text says timer 300 or a few ms more, never timer 100
Quiz: a click 50 ms into the busy loopChrome 154loop done at 300 ms, click at 300 ms, timer at 301 msthe handler runs after the loop ends
animate.js (stage): three rAF frames move the box 50 px each; the timer vs the first frameChrome 154at page load: task, frame:50px, frame:100px, frame:150px ×10; from a click handler: frame:50px, task, frame:100px, frame:150px ×6; rAF every 16.65 msanimation: task, frame, frame, frame, 3 frames painted; aside: the timer usually runs first, but from a click handler Chrome paints the frame first
A click that arrives while the tick waits runs firstChrome 154quick clicks: click 1, click 2, click 3; tick came due during a busy 500 ms, a click arrived after it: click 4, tickclick 1, click 2, click 3, click 4, tick
The "long" strategy: frames, longest task, click delayChrome 154work 1500 ms, 0/90 frames painted, longest task 1502 ms, clicks waited 1052/650/253 mswork 1503 ms, 1/90 frames painted, longest task 1500 ms, clicks waited 1038/643/265 ms
The "chunk" strategy: frames, longest task, click delayChrome 154work 2040 ms, 122/122 frames painted, no task over 50 ms, clicks waited 12/11/6 mswork 2065 ms, 123/123 frames painted, longest task 12 ms, clicks waited 3/3/3 ms
The "micro" strategy: frames, longest task, click delayChrome 154work 1500 ms, 0/90 frames painted, longest task 1500 ms, clicks waited 1050/649/249 mswork 1518 ms, 1/91 frames painted, longest task 1515 ms, clicks waited 1054/659/280 ms
The "worker" strategy: frames, longest task, click delayChrome 154work 1505 ms, 91/90 frames painted, no task over 50 ms, clicks waited 0/0/0 mswork 1506 ms, 90/90 frames painted, longest task 5 ms, clicks waited 3/3/3 ms
Chunks with setTimeout take longer than one long taskChrome 1542040 ms vs 1500 ms (×1.36)about a third longer; the timeline shows ×1.37
yield.js: a microtask yield vs a task yield between 12 ms chunksChrome 154await Promise.resolve(); → 0 frames, clicks waited up to 1050 ms; await new Promise((r) => setTimeout(r)); → 122/122 frames, clicks within 12 msa microtask: no frame, no click in between; a task: the loop gets a turn
Yielding with a message instead of a timerChrome 154MessageChannel: work 1510 ms, 90/91 frames painted, no task over 50 ms, clicks waited 7/4/3 ms; setTimeout: work 2040 ms, 122/122 frames painted, no task over 50 ms, clicks waited 12/11/6 msa real task boundary (a timer, a message, or scheduler.yield()) gives rendering and input a turn
orders.js runs as one long taskChrome 154work 1500 ms, 0/90 frames painted, longest task 1500 ms, clicks waited 1051/651/250 ms3,000 × 0.5 ms = 1.5 s in one task: no frames, clicks wait
scheduler.yield(): front of the queue, clicks fast, fewer framesChrome 154work 1503 ms, 36/90 frames painted, no task over 50 ms, clicks waited 1/1/3 ms; setTimeout chunks: 122/122 frames in 2040 ms; order: scheduler.yield continuation → setTimeout queued first → setTimeout queued first → setTimeout continuationcontinuation goes to the front; clicks still get in and the work finishes sooner, but frames are painted less often
A worker crunches 3,000 orders; the main thread stays freeChrome 154work 1509 ms, 91/91 frames painted, no task over 50 ms, clicks waited 1/0/0 ms; result had 3000 items, first 0 although main set orders[0].id = -1 after posting; typeof document in a worker: undefinedframes keep painting, clicks handled instantly, data is copied, workers have no DOM
Compositor animations during a 1.5 s long taskChrome 15478 frames reached the screen during the task; in 13 of them the CSS transform box was at 10 different places and the CSS opacity box had 8 shades, the rAF-driven box at 1CSS animations of transform and opacity can keep running on the compositor thread; anything your code renders needs the main thread
Which tasks Chrome reports as long tasksChrome 154tasks of 40, 47, 55, 70 ms → long tasks reported: 55, 70 msany task over 50 ms
The stack trace DevTools showsChrome 154Uncaught Error: out of greetings at greet (greet.js:2:9) at main (greet.js:5:15) at greet.js:8:1Uncaught Error: out of greetings at greet (greet.js:2:9) at main (greet.js:5:15) at greet.js:8:1
setTimeout returns at once with an id; clearTimeout cancelsChrome 154id 1, both lines took 0.00 ms; after 1.3 s sendEmail ran: falsereturns at once, with an id; cancelled before it fires
Focus waits for the script; the 4 ms clamp after 5 levels of nestingChrome 154focused during the script: false, after: true; chained setTimeout(…, 0) delays: 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 4.5, 4.6, 4.5, 4.5, 4.6, 4.4 msafter the current code is done; after 5 levels of nesting the minimum becomes 4 ms
What is on the stack when the click handler runsChrome 154addToCart('A-42') with stack: addToCart ← HTMLButtonElement.<anonymous>runs later, as a task, on an empty stack
How late a 100 ms timer fires on an idle threadChrome 154, Node 22.22.0Chrome: 2.00, 1.70, 1.00, 1.80 ms; Node: 1.16, 1.11, 0.40, 0.98 msa minimum, never early
queueMicrotask, .then, await and MutationObserver all run before a task queued firstChrome 154script → queueMicrotask → then → after await → MutationObserver → task queued firstall four run their callbacks as microtasks
rAF runs tick once per frameChrome 15437 calls in 600 ms, 16.67 ms apart; transform set from the frame time each call: trueonce per frame
checkout.js (promises) vs the async checkout() on the stage, in 3 interleavingsNode 22.22.0same: 1 · checkout starts, 2 · back in the script, 3 · after await; same: 1 · checkout starts, sync, m1, 3 · after await, m2, p1, done, p2, p3, task; same: 1 · checkout starts, 3 · after await, p1, awaited, p2, p3, p4the ordering is exactly this
Two awaits: headers first, then the bodyChrome 154headers at 1 ms, a timer ran at 121 ms (stack depth 1), renderCart at 301 msthe body streams in later, so await again; while waiting the stack is empty and the loop is free
Node reads files on libuv’s thread poolNode 22.22.0a readFile queued behind a blocked FIFO read finished after 309 ms with a pool of 1 thread, 2 ms with 4libuv has a thread pool for file I/O
When the second .then joins the queueNode 22.22.0promise1 → queued inside promise1 → promise2promise2 isn’t queued until promise1’s callback returns
app.js on the stage: readFile callback schedules bothNode 22.22.0immediate | timeout ×60immediate | timeout
setTimeout(…, 0) is a 1 ms timer in NodeNode 22.22.0_idleTimeout = 1the timer card reads "setTimeout · 0 → 1 ms"
setTimeout 0 vs setImmediate from the main moduleNode 22.22.0immediate | timeout ×58; timeout | immediate ×2; after a 2 ms busy wait in the module: timeout | immediate ×30the order can flip: it depends on whether 1 ms has passed when the loop first checks its timers
process.nextTick vs a promiseNode 22.22.0ticks.cjs: nextTick | promise ×6; same code as .mjs: promise | nextTick ×6CommonJS prints nextTick, promise; in an ES module the promise prints first
Who goes first: process.nextTick or a promise?Node 22.22.0CommonJS main module: nextTick; ES module body: promise; setTimeout callback: nextTick; setImmediate callback: nextTick; fs.readFile callback: nextTick; inside a .then callback: promiseas soon as control returns to Node, ahead of promises
Microtasks drain between callbacks of the same phaseNode 22.22.0t1 p1 t2 p2 i1 q1 i2 q2between callbacks, Node drains microtasks
Block programs: console order, animation vs ChromeChrome 154195 programs (every 1–3 block mix of log, timeout, then, microtask and async, every pair with setTimeout 100 or fetch, 16 longer ones) printed the same ordersame order in every program
CS-03

Hash Tables

12 checks · all agree
CheckReal program didLesson shows
redis-server starts with the redis.conf snippet and reads back every valueRedis 7.2.5hash-max-listpack-entries 512, hash-max-listpack-value 64hash-max-listpack-entries 512, hash-max-listpack-value 64
Default listpack limits for hashesRedis 7.2.5512 entries, 64 bytes512 entries, 64 bytes ("these are the defaults")
When a hash stops being a flat listRedis 7.2.5512 fields: listpack, 513: hashtable; a 64-byte value: listpack, 65 bytes: hashtablehash-max-listpack-entries 512, hash-max-listpack-value 64
Flat list vs hash tableRedis 7.2.54 fields: 136 vs 368 bytes, 63 vs 58 ns per HGET inside Redis; 512 fields: 10296 vs 32880 bytes, 1260 vs 70 ns per HGET inside Redisthe flat list takes about a third of the memory, but lookups slow down as it grows
hash() vs Java's String.hashCode()Node 22.22.0, OpenJDK 18.0.2.1hash('bob') = 97717, 97717; 17 short keys: identical to Java; long strings: plain hash differs (no wrap); the note's (Math.imul(h, 31) + ch.charCodeAt(0)) | 0: matches Java on all; Math.imul without | 0 on "xfjfxtz": 2147483668 vs Java -214748362897717 every time; exactly Java's String.hashCode() for short keys; the note's expression reproduces Java's wrap-around
table.js: the values in the commentsNode 22.22.0hash('alice') = 92903040, bucket 092903040 % 10 = 0
phone.js: the values in the commentsNode 22.22.0get('dave') = 555-0108, get('zoe') undefined: true, has('alice') = true'555-0108', undefined, true
lazy.js: the values in the commentsNode 22.22.0jack → 6, jill → 66, 6
order.js: the values in the commentsNode 22.22.0["carol","alice","bob"]['carol', 'alice', 'bob']
V8 objects: fixed shapes, then dictionary modeNode 22.22.0new object fast: true; dictionary mode after 20 computed-key adds; after one delete fast: falseobjects start with fixed "shapes" and turn into hash tables after many adds and deletes
contacts.find() over the lesson’s 10 contactsNode 22.22.0dave: 9 comparisons, zoe: 10dave is 9th: 9 comparisons; zoe: all 10
ChainedTable: bucket 0 and the lookup for frankNode 22.22.0bucket 0: alice, erin, frank; get('frank') = 3 after 3 comparisons; set('erin') again updated it to 20erin and frank join alice’s chain; looking up frank: 3 comparisons
CS-04

Stack vs Heap

3 checks · all agree
CheckReal program didLesson shows
INFO memory: the fields shown, in order, "…" where lines are skippedRedis 7.2.5ok (mem_allocator:libc)… used_memory_human:1.02G … used_memory_rss_human:1.48G … mem_fragmentation_ratio:1.45 …
Deleting every other key of a millionRedis 7.2.5used_memory 177 MB → 93 MB, RSS 184 MB → 184 MB, ratio 1.04 → 1.98; 500,000 new keys: used 177 MB, RSS 184 MB1.02G used, 1.48G held, ratio 1.45: "45% overhead, mostly holes"
mem_fragmentation_ratio is RSS divided by used memoryRedis 7.2.51.98 reported, 1.98 computed1.45 = 1.48G / 1.02G (1.45)
CS-05

Garbage Collection

4 checks · all agree
CheckReal program didLesson shows
Heap after GC, per open and close of a ~5 MB reportChrome 154leaky: 0.6 → 5.7 → 10.6 → 15.6 MB; with the fix: 0.6 → 0.7 → 0.7 → 0.7 MBafter every open and close, the heap after GC is 5 MB bigger; removing the listener fixes it
mark() on a graph with two cycles, then sweep()Node 22.22.0marked user, profile, cart; freed draft, note in address order; marks cleared: truecycles can’t trap the marker; the unreachable draft ↔ note cycle is freed
node --trace-gc prints Scavenge and Mark-Compact lines in this formatNode 22.22.0printed Scavenge and Mark-Compact lines with pid:isolate, time, heap before (reserved) -> after (reserved) MB, pause, plus a "pooled: N MB," field the lesson’s lines don’t havethese fields; the format varies a little between Node versions
--max-semi-space-size=32Node 22.22.0new space grew to 64 MB (default 32 MB, starting at 1 MB); scavenges: 16 vs 20sets the maximum size of each half, in MB; they start small and grow
CS-07

Race Conditions & Locks

10 checks · all agree
CheckReal program didLesson shows
4 worker threads × 1,000,000 increments on one SharedArrayBufferNode 22.22.0count[0] = count[0] + 1 ended at 1,071,043 and 1,858,163; Atomics.add at 4,000,000 and 4,000,000; Atomics.add on 5 returned 5plain: lost updates; atomic: exactly 4,000,000; Atomics.add returns the old value
SharedArrayBuffer with and without COOP + COEPChrome 154without the headers: typeof SharedArrayBuffer = undefined; with them: function (crossOriginIsolated true)needs a cross-origin isolated page (COOP and COEP headers)
book.sql · racy: the same interleaving, step by stepPostgreSQL 14.15A:RD(free) B:RD(free) A:IF(yes) B:IF(yes) A:WR(alice) B:WR(bob) → {"seat":"bob"}A:RD(free) B:RD(free) A:IF(yes) B:IF(yes) A:WR(alice) B:WR(bob) → {"seat":"bob"} (Both got a confirmation. The seat says bob.)
book.sql · atomic: the same interleaving, step by stepPostgreSQL 14.15A:BOOK(ok) B:BOOK(no) → {"seat":"alice"}A:BOOK(ok) B:BOOK(no) → {"seat":"alice"} (One booking (alice). The other was told it's sold out.)
withdraw.sql · racy: the same interleaving, step by stepPostgreSQL 14.15A:SEL(100) B:SEL(100) A:IF(yes) B:IF(yes) A:UPD(20) B:UPD(20) → {"balance":20,"version":7}A:SEL(100) B:SEL(100) A:IF(yes) B:IF(yes) A:UPD(20) B:UPD(20) → {"balance":20,"version":7} (Paid out $160 from $100, balance says $20.)
withdraw.sql · pessimistic: the same interleaving, step by stepPostgreSQL 14.15A:SEL(100) B:SEL(wait) A:IF(yes) A:UPD(20) A:COM B:SEL(20) B:IF(no) B:COM → {"balance":20,"version":7}; plain SELECT meanwhile: 100 in 2 msA:SEL(100) B:SEL(wait) A:IF(yes) A:UPD(20) A:COM B:SEL(20) B:IF(no) B:COM → {"balance":20,"version":7} (Paid $80, balance $20. The books balance.)
withdraw.sql · optimistic: the same interleaving, step by stepPostgreSQL 14.15A:SEL(100, 7) B:SEL(100, 7) A:IF(yes) B:IF(yes) A:UPD(ok) B:UPD(retry) B:SEL(20, 8) B:IF(no) → {"balance":20,"version":8}A:SEL(100, 7) B:SEL(100, 7) A:IF(yes) B:IF(yes) A:UPD(ok) B:UPD(retry) B:SEL(20, 8) B:IF(no) → {"balance":20,"version":8} (Paid $80, balance $20. The books balance.)
Two conditional UPDATEs at the same momentPostgreSQL 14.15alice's UPDATE (not yet committed): UPDATE 1; bob's waited for it (true), then UPDATE 0; the seat is alice'sthe check and the write happen in one statement; the loser updates 0 rows
Isolation levelsPostgreSQL 14.15default_transaction_isolation = read committed; under REPEATABLE READ both read 100 and 100, the first withdrawal committed, the second UPDATE got ERROR 40001: could not serialize access due to concurrent update; the retry read 20 and would refuseREAD COMMITTED is PostgreSQL’s default and doesn’t stop it; under REPEATABLE READ the second update fails with a serialization error and the app retries
FOR UPDATE: lock-order deadlocksPostgreSQL 14.15after 1001 ms one got ERROR 40P01: deadlock detected; the other got its rowFOR UPDATE: watch out for lock-order deadlocks
CS-08

Big-O, Visually

8 checks · all agree
CheckReal program didLesson shows
One timed findMax over 10 million numbersNode 22.22.0console.timeEnd printed 47.4 ms (Apple M5 Pro)~50 ms on one laptop; yours will differ
constant.jsNode 22.22.08 bytes per number, 10,000,000 numbers; names.has('carol') = true8 bytes per number
search.jsNode 22.22.0includes true, indexOf 14, has trueindexOf(46) returns 14
binarySearch.jsNode 22.22.0correct on 1200 searches; a miss in 1,048,576 items read 40 elementshalves the range each step: about log₂ n looks
mergeSort, merge and V8’s Array.prototype.sortNode 22.22.0[5, 3, 9, 1] → [1, 3, 5, 9]; mergeSort matched sort on 200 random arrays: true; sorting 10,000 sorted numbers took 9999 comparisons (n − 1), random ones 119,911V8 uses TimSort: O(n log n)
fib(30) and fib(50): calls and timeNode 22.22.0fib(30) made 2,692,537 calls; counted calls for n = 30…35 all equal 2·fib(n+1) − 1; fib(40) ran at 1.70 ns a call, so fib(50)'s 40,730,022,147 calls take about 69 s; memoized fib(50) made 99 calls2,692,537 calls; 40,730,022,147 calls: over a minute, even on a fast machine; memoized: 99 calls
a-million.js: the step countsNode 22.22.0n = 1000000, n·log₂n = 19,931,569, n² = 1e+12, 2ⁿ has 301,030 digits (BigInt)1,000,000; ≈ 20,000,000; 1,000,000,000,000; a 301,030-digit number
hasDuplicate: all pairs, with a Set, and the one-liner agreeNode 22.22.0500 random arrays: same answer from all threesame idea, O(n) with a Set