Understanding Redis’s Unified Request/Response Protocol: A Deep Dive
This article explains how Redis communicates over TCP using a simple, fast‑parsing, human‑readable protocol, details the unified request format introduced in Redis 1.2, and illustrates request and response structures—including single‑line, error, bulk, and multi‑bulk replies—with concrete examples.
Redis Communication Overview
Redis listens on TCP port 6379. Each client connection is a dedicated TCP socket, and every command sent to the server terminates with the CRLF sequence ( \r\n).
Unified Request Protocol (binary‑safe)
Introduced in Redis 1.2 and made the default in Redis 2.0, the protocol encodes each argument as a bulk string, making the whole request binary‑safe.
*<number_of_arguments>
$<bytes_of_argument_1>
<argument_1_data>
...$<bytes_of_argument_N>
<argument_N_data>
Explanation:
*<number_of_arguments> – asterisk followed by the count of arguments.
$<bytes_of_argument_i> – dollar sign followed by the byte length of the i‑th argument.
<argument_i_data> – the raw bytes of the argument.
Example with nc
nc localhost 6379
keys *The client sends the following byte stream:
*2
$18
user:sign:5:202101
$18
seckill_vouchers:6
Response Types
Redis replies start with a single‑character prefix that identifies the response type:
Simple string – prefix +, e.g. +OK Error – prefix -, e.g. -ERR unknown command `keys*` Integer – prefix :, e.g. :1000 Bulk string – prefix $, followed by the length and the data, e.g. $5\r\nhello\r\n Array (multi‑bulk) – prefix *, followed by the number of elements, each element being any of the above types.
Bulk Reply Example
*2
$18
user:sign:5:202101
$18
seckill_vouchers:6
When an array contains multiple bulk strings, each element is encoded with its own $ length header, allowing clients to parse complex list results efficiently.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
JavaEdge
First‑line development experience at multiple leading tech firms; now a software architect at a Shanghai state‑owned enterprise and founder of Programming Yanxuan. Nearly 300k followers online; expertise in distributed system design, AIGC application development, and quantitative finance investing.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
