Index

The Socket That Answers

100%Open SVG ↗
01 - THE SOCKET THAT ANSWERSthe felt problem: bytes have no edgesredis-cli (a client)PINGPINGPINGPINGPING...(no gaps between words)P I N G P I N G P I N G P Ia stream of raw bytesyour server :6379where does ONEcommand END?a socket never tells me.REAL LIFE:Someone reads their shopping list with NO pauses - "milkeggsbreadmilkeggs". You can't tell the items apart. TCP hands you bytes in exactly that shape: no commas, no edges.WHAT ONE recv() ACTUALLY HANDS YOU - never a clean commandcommand #1 (split across 2 reads)command #2 starts mid-read"PIN"read #1"G\r\nPI"read #2"NG\r\n"read #3The kernel doesn't know what a "command" is. It ships whatever bytes have arrived. Reassembling them into commands is 100% YOUR job.v01: we cheat (and it's fine for now)PING [enter]+PONGA human at nc presses ENTER after each word -> one clean line -> we get away with reading line-by-line.THE WALL: a real client speaks RESP*1\r\n$4\r\nPING\r\nwhat redis-cli actually sendsour line-readersees garbage-> -ERRThe fix: a real protocol with lengths built in. That is board 02: RESP.THE ONE THINGA socket gives you a STREAM of bytes, never messages. Framing - deciding where one command ends - is your job, not the network's. That one problem is the entire reason RESP exists.

Lessons that use this board

01 - THE SOCKET THAT ANSWERS the felt problem: bytes have no edges redis-cli (a client) PINGPINGPI NGPINGPING... (no gaps between words) P I N G P I N G P I N G P I a stream of raw bytes your server :6379 where does ONE command END? a socket never tells me. REAL LIFE: Someone reads their shopping list with NO pauses - "milkeggsbreadmilkeggs". You can't tell the items apart. TCP hands you bytes in exactly that shape: no commas, no edges. WHAT ONE recv() ACTUALLY HANDS YOU - never a clean command command #1 (split across 2 reads) command #2 starts mid-read "PIN" read #1 "G\r\nPI" read #2 "NG\r\n" read #3 The kernel doesn't know what a "command" is. It ships whatever bytes have arrived. Reassembling them into commands is 100% YOUR job. v01: we cheat (and it's fine for now) PING [enter] +PONG A human at nc presses ENTER after each word -> one clean line -> we get away with reading line-by-line. THE WALL: a real client speaks RESP *1\r\n$4\r\nPING\r\n what redis-cli actually sends our line-reader sees garbage -> -ERR The fix: a real protocol with lengths built in. That is board 02: RESP. THE ONE THING A socket gives you a STREAM of bytes, never messages. Framing - deciding where one command ends - is your job, not the network's. That one problem is the entire reason RESP exists.

Saved in this browser only.

← The MapRESP The Wire Protocol →