RESP Typed Responses: RESP2 vs. RESP3
RESP2 — only 5 wire types, everything else is "stringly-typed"
| Prefix | Type | Example on the wire |
|---|---|---|
+ | simple string | +OK\r\n |
- | error | -ERR bad command\r\n |
: | integer | :42\r\n |
$ | bulk string (raw bytes) | $3\r\nbar\r\n |
* | array | *2\r\n$3\r\nfoo\r\n$3\r\nbar\r\n |
That's the entire vocabulary. Anything that isn't one of those five must be crammed into a string or an array — and once crammed in, the client can't tell what it originally was. It just sees bytes.
✗ Pain #1 — floats come back as strings
ZSCORE myset member → $4\r\n3.14\r\n✗ Pain #2 — maps come back as flat arrays
HGETALL myhash → *4
$6 field1
$6 value1
$6 field2
$6 value2Other RESP2 ambiguities: no boolean — yes/no commands return :1/:0 and you interpret it; and null vs empty — a null bulk string ($-1) and a null array (*-1) are two different "nulls," easy to confuse with an empty array.
RESP3 — the wire format carries the type
| Prefix | New RESP3 type | Notes |
|---|---|---|
% | map | %2\r\n… = a map with 2 key/value pairs |
, | double | ,3.14\r\n — a real float, incl. ,inf / ,-inf |
# | boolean | #t = true, #f = false |
_ | null | one unambiguous null (_\r\n) |
~ | set | distinct from array |
( | big number | integers beyond int64 |
= | verbatim string | string with a format hint (txt, mkd) |
> | push | server-initiated, out-of-band message |
Why it matters
| Benefit | RESP2 | RESP3 |
|---|---|---|
Native map from HGETALL | ✗ you re-pair a flat array | ✓ automatic |
Native float from ZSCORE | ✗ you ParseFloat a string | ✓ automatic |
| Native boolean | ✗ you interpret 1/0 | ✓ automatic |
| Unambiguous null | ✗ $-1 vs *-1 | ✓ single _ |
| Server can push messages | ✗ impossible | ✓ push type |
RESP3 is opt-in: a client sends HELLO 3 right after connecting to switch the connection into RESP3 mode. Without it, the server stays in backward-compatible RESP2.
Seeing it in Go
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Protocol: 3, // RESP3. Use 2 to force the legacy protocol.
})
defer rdb.Close()
rdb.HSet(ctx, "user:42", "name", "alice", "age", "30")
rdb.ZAdd(ctx, "scores", redis.Z{Score: 3.14, Member: "pi"})
// --- Map: HGETALL ---
// Under RESP3 the SERVER tags it as a map (%), so the client doesn't have to
// un-flatten a raw array — it's handed a real map on the wire.
fields, _ := rdb.HGetAll(ctx, "user:42").Result()
fmt.Printf("HGETALL -> %#v (Go type: map[string]string)\n", fields)
// --- Double: ZSCORE ---
// Under RESP2 that float arrived as the STRING "3.14" and the library had to
// ParseFloat it. Under RESP3 it arrives as a typed double (,3.14).
score, _ := rdb.ZScore(ctx, "scores", "pi").Result()
fmt.Printf("ZSCORE -> %v (Go type: float64)\n", score)HGETALL -> map[string]string{"age":"30", "name":"alice"} (Go type: map[string]string)
ZSCORE -> 3.14 (Go type: float64)What the library saves you from (the RESP2 world by hand)
// RESP2 HGETALL comes back as a flat array: [name, alice, age, 30].
// You must re-pair it yourself:
flat := []string{"name", "alice", "age", "30"} // what the wire gave you
m := make(map[string]string, len(flat)/2)
for i := 0; i+1 < len(flat); i += 2 {
m[flat[i]] = flat[i+1] // odd = key, even = value — a convention you must KNOW
}
// RESP2 ZSCORE comes back as the string "3.14". You must parse it:
f, err := strconv.ParseFloat("3.14", 64)RESP3's typed responses push all of that guesswork onto the protocol, so neither you nor the library has to reconstruct types from untyped bytes.