Reading — step 1 of 3
RESP Wire Format
~1 min readRESP Protocol Fundamentals
The RESP Wire Format
You've been using RESP already — now let's understand it fully.
The Five RESP2 Types
| Prefix | Type | When Used |
|---|---|---|
+ | Simple String | Short status replies (+OK) |
- | Error | Error messages (-ERR ...) |
: | Integer | Counts, boolean results (:42) |
$ | Bulk String | Binary-safe data ($5\r\nhello) |
* | Array | Collections, command responses |
Why Two String Types?
Simple strings (+) can't contain \r or \n — they're terminated by CRLF. Fine for OK and PONG.
Bulk strings ($) use length-prefixing, so they can contain any bytes — including newlines, nulls, binary data. This is how Redis is "binary safe."
The Null Bulk String
$-1\r\n means "this value doesn't exist" — like null/nil/None. You'll return this for missing keys.
Your Serializer
Build functions for all five types:
encode_simple_string(s)→+s\r\nencode_error(msg)→-msg\r\nencode_integer(n)→:n\r\nencode_bulk_string(s)→$len\r\ns\r\n(or$-1\r\nfor null)encode_array(items)→*count\r\n+ items
Unknown Commands
When your Redis receives a command it doesn't recognize, return:
-ERR unknown command '<cmd>'\r\n
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…