Video summary
Implementing Redis' Wire Protocol - RESP | Redis Internals
Main summary
Key takeaways
What the video covers
- An exhaustive walkthrough of the Redis Serialization Protocol (RESP) by implementing a RESP wire-protocol decoder in Go inside a re-implementation project (described as a Golang based re-implementation of Redis).
- The core technical theme is:
- Parsing RESP-encoded values from a byte slice
- Decoding them into Go types
- Returning how many bytes were consumed so subsequent values can be decoded correctly
Go module / file structure
- A module named
coreis created. - A file
resp.gocontains “anything and everything around RESP,” especially encoding/decoding.
Key API functions and design
1) Decode(data []byte) -> (interface{}, error)
- Public entry point.
- Takes a byte slice containing RESP data.
- Decodes RESP data and returns:
- the decoded Go object (generic via
interface{}) - an optional error
- the decoded Go object (generic via
- When the input contains multiple RESP values back-to-back, decoding focuses on the first value.
- It relies on a helper function:
decode1to decode just one RESP value.
2) decode1(data []byte) -> (value interface{}, delta int, err error?)
- Acts as the helper that decodes exactly one RESP value.
- Returns:
- the decoded value
- a delta (the number of bytes consumed/processed)
- an error if parsing fails (error handling referenced in the description)
Critical low-level decision: “delta”
Because RESP messages can contain multiple values concatenated, the decoder always returns the byte count consumed so the next decoder call can start at the correct offset.
- This is especially essential in arrays, where nested/back-to-back values must be iterated correctly.
How decoding dispatch works (by RESP type prefix)
The decoder checks the first byte of the value (at offset 0) and switches on it:
+→ Simple String-→ Error:→ Integer (int64)$→ Bulk String*→ Array
Data type decoding walkthrough (5–6 types)
1) Simple String (+<string>\r\n)
- Starts parsing at
pos = 1(sincedata[0]is+). - Scans until it encounters
\r(the carriage return before\r\n). - Returns:
- the parsed string
delta = pos + 2(accounts for\r\n)
Example format:
+ok\r\n
2) Error (-<error-message>\r\n)
- Starts with
-, then reads until\r\n. - The implementation notes the difference from Simple String is only
+vs-. - Reuses logic similar to the “read simple string” approach.
3) Integer (:<digits>\r\n)
- Starts with
pos = 1(after:). - Reconstructs an
int64by reading digit-by-digit until\r. - Converts ASCII bytes to numeric value using:
value = value * 10 + int64(data[pos] - '0')
- Returns:
- the
int64 delta = pos + 2
- the
4) Bulk String ($<len>\r\n<string>\r\n)
- Starts with
pos = 1(after$). - First reads the length using a helper:
readLen(data, pos)(described as “read length”)
- After length is known:
- reads exactly
<len>bytes as the string payload - returns
delta = pos + len + 2(payload +\r\n)
- reads exactly
While parsing length:
- iterates digits (
0–9) - stops on
\r/ non-digit - reconstructs length with base-10 accumulation
5) Arrays (*<count>\r\n<elem1><elem2>...)
- Starts with
pos = 1(after*). - Reads
<count>viareadLen. - Allocates a Go array/slice with that many elements, each element being a generic type (
interface{}). - For each element:
- calls
decode1on the current position - stores the returned decoded value
- advances the offset using
delta:
- calls
pos = pos + delta
- Supports nested arrays because elements can themselves be arrays; since
decode1works recursively, nesting is handled (described as potentially “infinite” in principle).
Example described:
*2*3 ...with arrays containing integers, strings, and errors
Testing / correctness claim
- The implementation uses “examples given in the Redis official specification” and:
- “passed the entire day” / “passed the entire … very beautifully”
- notably for nested arrays and mixed element types
What’s next (tutorial progression)
- The next video will:
- implement the first Redis command(s): PING and PONG
- so a client like redis-cli can send
pingand receivepong - implies wiring the protocol decoding to command handling
Main speakers / sources
- Speaker: The video’s host/author (single narrator credited implicitly by “i” throughout the subtitles).
- Primary source referenced: Redis official RESP specification (used for examples).