Skip to content
The RESP Wire Format
step 1/3

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

PrefixTypeWhen Used
+Simple StringShort status replies (+OK)
-ErrorError messages (-ERR ...)
:IntegerCounts, boolean results (:42)
$Bulk StringBinary-safe data ($5\r\nhello)
*ArrayCollections, 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\n
  • encode_error(msg)-msg\r\n
  • encode_integer(n):n\r\n
  • encode_bulk_string(s)$len\r\ns\r\n (or $-1\r\n for 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…

The RESP Wire Format — Build Redis from Scratch