Redis Wire Protocol: A Deep Dive into RESP and its Data Types

Arpit Bhayani

Arpit Bhayani

Apr 11, 2026 • 5 min read

Play

Note: This article is an AI-generated write-up based on the captions and transcript of the video above. Watch the embedded video for the full visual walk-through and nuances.

Redis Wire Protocol: A Deep Dive into RESP and its Data Types

In the realm of distributed systems, clients and servers need a standardized way to communicate. This is where wire protocols come into play. A wire protocol defines the format of data exchanged over a network, ensuring both ends can correctly interpret messages. For Redis, this crucial role is fulfilled by the Redis Serialization Protocol (RESP).

What is a Wire Protocol and Why is it Needed?

Imagine a scenario where a Redis client connects to a server. When the client sends a command like PUT k v, how does the server understand that it needs to store a key k with value v? It’s not just sending plain text; the data must be structured in a way that both the client and server agree upon. This agreed-upon structure and encoding mechanism is the wire protocol.

Wire protocols are essential for several reasons:

  1. Interoperability: Different clients (e.g., Python, Java, Node.js) can communicate with the same server, provided they all implement the protocol correctly.
  2. Efficiency: Protocols are often designed to be lightweight and fast, minimizing network overhead and CPU cycles for serialization/deserialization.
  3. Clarity: They define how various data types (strings, integers, arrays) are represented, avoiding ambiguity.
  4. Error Handling: They provide mechanisms to signal errors or special conditions.

Introducing RESP: Redis Serialization Protocol

RESP is the backbone of communication in Redis. It’s a request-response protocol, meaning client requests are RESP-encoded, and server responses are also RESP-encoded. Designed for simplicity and performance, RESP supports common data types like integers, strings, and arrays, which are sufficient for Redis’s straightforward data model.

General Rules of RESP Encoding

Every data type in RESP adheres to a few fundamental rules:

  • Special Character Prefix: Each data type begins with a unique special character (e.g., +, :, $, *, -) to indicate its type.
  • CRLF Termination: Every data element, including the data itself, is terminated by CRLF (Carriage Return Line Feed), represented as \r\n.

Let’s explore the specific RESP data types.

RESP Data Types Explained

RESP supports five primary data types, each with its own encoding specification.

1. Simple Strings

Simple strings are used for non-binary-safe strings with minimal overhead, typically for short, simple responses like PONG or OK.

  • Encoding: Starts with a + sign, followed by the string, and terminated by \r\n.

  • Example: Sending PONG as a response.

    +PONG\r\n
  • Overhead: Extremely low. For an N-character string, it requires N + 3 bytes (N for the string, 1 for +, 2 for \r\n).

  • Limitation: Simple strings cannot contain \r or \n characters within the string itself, as these would prematurely terminate the string during parsing. This makes them not binary safe.

2. Integers

Integers in RESP are used to transmit 64-bit signed integers.

  • Encoding: Starts with a : sign, followed by the integer value, and terminated by \r\n.

  • Example: Sending the integer 1729.

    :1729\r\n
  • Parsing: The server reads characters after the colon until \r\n is encountered, interpreting them as a 64-bit integer.

3. Bulk Strings

Bulk strings are designed for binary-safe strings, meaning they can contain any byte sequence, including \r, \n, or null characters. This is crucial for storing arbitrary data like images or serialized objects.

  • Encoding: Starts with a $ sign, followed by the length of the string in bytes, then \r\n, then the actual string data, and finally \r\n.

  • Example: Sending the string PONG.

    $4\r\nPONG\r\n

    Here, $4 indicates that the following string is 4 bytes long.

  • Binary Safety: The explicit length prefix ($4 in the example) is what makes bulk strings binary safe. The parser knows exactly how many bytes to read, regardless of their content, before looking for the final \r\n terminator.

  • Special Representations for Bulk Strings:

    • Empty String: A string with zero length.

      $0\r\n\r\n

      The length is 0, followed by \r\n, then no data, and finally \r\n.

    • Null String: Represents the absence of a string or a null value.

      $-1\r\n

      The special length -1 signifies a null bulk string. The client and server interpret this as a lack of data.

4. Arrays

Arrays are a fundamental data type in RESP, primarily used to represent commands sent from the client to the server (e.g., PUT k v) or lists of elements in responses. An array can contain other RESP-encoded data types, including other arrays, allowing for complex data structures.

  • Encoding: Starts with a * sign, followed by the number of elements in the array, then \r\n, and then each element of the array, individually RESP-encoded.

  • Example: The command PUT k v is sent as an array of three bulk strings:

    *3\r\n$3\r\nPUT\r\n$1\r\nk\r\n$1\r\nv\r\n
    • *3 indicates an array with 3 elements.
    • $3\r\nPUT\r\n is the first element (bulk string
Arpit Bhayani

Principal Engineer II at Razorpay - building Agent Studio, Ex-staff engg at GCP Memorystore & Dataproc, Creator of DiceDB, ex-Amazon Fast Data, ex-Director of Engg. SRE and Data Engineering at Unacademy. I spark engineering curiosity through my no-fluff engineering videos on YouTube and my courses