Index

S3 - Build a Redis Server

100%Open SVG ↗
S3 · BUILD A REDIS SERVERthe RESP wire protocol · TCP · persistent (backed by your S1 store) · in GoREAL CLIENT$ redis-cli -p 6379> SET foo barOK> GET foo"bar"the actual Redis CLI —it has no idea it's yours.TCP :6379RESP · what SET foo bar becomes on the wire*3<- array of 3 elements$3<- next bulk string is 3 bytesSET$3<- 3 bytes...foo$3bar(every line ends in \r\n = CRLF framing)+OK<- the reply: a simple-stringRESP TYPES · the 1st byte IS the type+ simple string +OK\r\n- error -ERR unknown cmd\r\n: integer :1\r\n$ bulk string $3\r\nfoo\r\n (len-prefixed)$-1 the nil bulk string (key missing)* array *2\r\n ... ...one byte tells the parser exactly what's coming next. that's the whole protocol.THE SERVER LOOP (one goroutine per connection)accept TCPconnectionparse theRESP arraydispatch thecommandwrite RESPreplyloop until the client disconnectsSET / GET / DEL hit yourS1 append-only store ->a PERSISTENT Redis(survives restart -- realRedis needs RDB/AOF for this)COMMANDS YOU IMPLEMENTPING -> +PONGSET key val -> +OK (append record to the log)GET key -> $<len> bulk / $-1 if the key is goneDEL key -> :1 / :0 (tombstone write)~4 commands and real redis-cli, redis-benchmark, and any Redis client library will talk to you.JARGON, DECODEDRESP = REdis Serialization Protocol: a tiny text protocol; type is the first byte, strings are length-prefixed.CRLF framing = every part ends in \r\n, so the parser always knows where a token stops -- no guessing.bulk string = a binary-safe string sent as $<byte-count>\r\n<bytes>\r\n (can hold nulls, newlines, anything).README / RESUME LINE“Implemented a Redis-compatibleserver in Go -- RESP protocol parser,TCP, concurrent connections, backedby a persistent log-structured store.”

Lessons that use this board

S3 · BUILD A REDIS SERVER the RESP wire protocol · TCP · persistent (backed by your S1 store) · in Go REAL CLIENT $ redis-cli -p 6379 > SET foo bar OK > GET foo "bar" the actual Redis CLI — it has no idea it's yours. TCP :6379 RESP · what SET foo bar becomes on the wire *3 <- array of 3 elements $3 <- next bulk string is 3 bytes SET $3 <- 3 bytes... foo $3 bar (every line ends in \r\n = CRLF framing) +OK <- the reply: a simple-string RESP TYPES · the 1st byte IS the type + simple string +OK\r\n - error -ERR unknown cmd\r\n : integer :1\r\n $ bulk string $3\r\nfoo\r\n (len-prefixed) $-1 the nil bulk string (key missing) * array *2\r\n ... ... one byte tells the parser exactly what's coming next. that's the whole protocol. THE SERVER LOOP (one goroutine per connection) accept TCP connection parse the RESP array dispatch the command write RESP reply loop until the client disconnects SET / GET / DEL hit your S1 append-only store -> a PERSISTENT Redis (survives restart -- real Redis needs RDB/AOF for this) COMMANDS YOU IMPLEMENT PING -> +PONG SET key val -> +OK (append record to the log) GET key -> $<len> bulk / $-1 if the key is gone DEL key -> :1 / :0 (tombstone write) ~4 commands and real redis-cli, redis-benchmark, and any Redis client library will talk to you. JARGON, DECODED RESP = REdis Serialization Protocol: a tiny text protocol; type is the first byte, strings are length-prefixed. CRLF framing = every part ends in \r\n, so the parser always knows where a token stops -- no guessing. bulk string = a binary-safe string sent as $<byte-count>\r\n<bytes>\r\n (can hold nulls, newlines, anything). README / RESUME LINE “Implemented a Redis-compatible server in Go -- RESP protocol parser, TCP, concurrent connections, backed by a persistent log-structured store.”

Saved in this browser only.

← S2 - Build a Load Balancer