S3 - Build a Redis Server
Tap the board to zoom inClick the board to zoom in
Lessons that use this board
- S3 · Build a Redis Server (that real clients can talk to)lesson
- S6 · Build a Replicated Key-Value Store (Dynamo-style)lesson
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.