Index

The Map

100%Open SVG ↗
CODE: teaching/code/from-scratch/redis/ -> go run ./01-socket ... ./07-persistence | Go, stdlib net only. run it, break it - that is the whole point.BUILD YOUR OWN REDIS - FROM SCRATCH00 - THE MAP a single-threaded, network-attached hashmapTHE HINGERedis is a single-threaded network-attached hashmap."Single-threaded" is not a limit you tolerate - it ISthe design: no locks, cache-friendly, dead simple.Everything else (expiry, eviction, persistence) existsto answer ONE problem each. We build each mechanismin the order its problem actually appears.THE RUNTIME PATH - one command, start to finishredis-cliany real clientTCP socket+ event loop01 . 04RESP parserdecode bytes02dispatchSET GET DEL03the dictTHE CORE10k clients, 1 threadmap[str] -> value (+ expiry)<- RESP reply serialized back to the client01 THE SOCKET THAT ANSWERSPROB: raw TCP is a byte stream with no edges - where does one command end?FIX: accept loop; reply PING -> PONG. framing is the open question.02 RESP - THE WIRE PROTOCOLPROB: need the framing real clients speak, so redis-cli itself connects.FIX: parse *N $len arrays of bulk strings. type = the first byte.03 THE MAP: SET / GET / DELPROB: the store is 'just a dict' - until many clients touch it at once.FIX: dict + a command table. now it is a real key-value server.04 ONE THREAD, MANY CLIENTSPROB: thread-per-client dies at 10k conns (RAM, context switches).FIX: I/O multiplexing (epoll/kqueue): the reactor loop. why 1 thread wins.05 KEYS THAT DIE (EXPIRY)PROB: can't rescan a million keys every second to find expired ones.FIX: lazy (check on read) + active (sample 20 random keys) sweep.06 EVICTION - MEMORY IS FULLPROB: maxmemory reached. WHICH key do you drop?FIX: approximated LRU by sampling (not true LRU) + a 24-bit access clock.07 SURVIVING A CRASHPROB: power cut - RAM is gone. durability vs speed.FIX: RDB (fork + copy-on-write snapshot) vs AOF (append every write).08 EVERYTHING + WHAT WE SKIPPEDPROB: full mini-redis on one canvas.FIX: real Redis adds replication, cluster hash-slots, pub/sub, 9 data types, Lua - and WHY we left them out.

Lessons that use this board

BUILD YOUR OWN REDIS - FROM SCRATCH 00 - THE MAP a single-threaded, network-attached hashmap THE HINGE Redis is a single-threaded network-attached hashmap. "Single-threaded" is not a limit you tolerate - it IS the design: no locks, cache-friendly, dead simple. Everything else (expiry, eviction, persistence) exists to answer ONE problem each. We build each mechanism in the order its problem actually appears. THE RUNTIME PATH - one command, start to finish redis-cli any real client TCP socket + event loop 01 . 04 RESP parser decode bytes 02 dispatch SET GET DEL 03 the dict THE CORE 10k clients, 1 thread map[str] -> value (+ expiry) <- RESP reply serialized back to the client 01 THE SOCKET THAT ANSWERS PROB: raw TCP is a byte stream with no edges - where does one command end? FIX: accept loop; reply PING -> PONG. framing is the open question. 02 RESP - THE WIRE PROTOCOL PROB: need the framing real clients speak, so redis-cli itself connects. FIX: parse *N $len arrays of bulk strings. type = the first byte. 03 THE MAP: SET / GET / DEL PROB: the store is 'just a dict' - until many clients touch it at once. FIX: dict + a command table. now it is a real key-value server. 04 ONE THREAD, MANY CLIENTS PROB: thread-per-client dies at 10k conns (RAM, context switches). FIX: I/O multiplexing (epoll/kqueue): the reactor loop. why 1 thread wins. 05 KEYS THAT DIE (EXPIRY) PROB: can't rescan a million keys every second to find expired ones. FIX: lazy (check on read) + active (sample 20 random keys) sweep. 06 EVICTION - MEMORY IS FULL PROB: maxmemory reached. WHICH key do you drop? FIX: approximated LRU by sampling (not true LRU) + a 24-bit access clock. 07 SURVIVING A CRASH PROB: power cut - RAM is gone. durability vs speed. FIX: RDB (fork + copy-on-write snapshot) vs AOF (append every write). 08 EVERYTHING + WHAT WE SKIPPED PROB: full mini-redis on one canvas. FIX: real Redis adds replication, cluster hash-slots, pub/sub, 9 data types, Lua - and WHY we left them out. CODE: teaching/code/from-scratch/redis/ -> go run ./01-socket ... ./07-persistence | Go, stdlib net only. run it, break it - that is the whole point.

Saved in this browser only.

The Socket That Answers →