Unlocking Redis: How the RESP Protocol Powers Lightning‑Fast Data Access
This article explains Redis's high‑performance RESP protocol, its simple design principles, how commands are serialized over TCP, and demonstrates Java examples for both a mock server and client, while detailing the various response types defined by the protocol.
In the world of Redis, speed reigns supreme, with QPS reaching the hundred‑thousand level.
Redis communicates with its clients using the RESP (REdis Serialization Protocol), designed with three principles: simple implementation, fast parsing for computers, and strong readability for humans.
Simple implementation
Fast parsing for computers
Human‑readable
A Redis command is packaged by the client, transmitted over the network, and then parsed and executed by the server.
The protocol works on top of TCP and defines a compact, line‑oriented format.
To observe the raw protocol messages, you can masquerade as a Redis server using a ServerSocket that listens on port 6379:
public static void server() throws IOException {
ServerSocket serverSocket = new ServerSocket(6379);
Socket socket = serverSocket.accept();
byte[] bytes = new byte[1024];
InputStream input = socket.getInputStream();
while (input.read(bytes) != 0) {
System.out.println(new String(bytes));
}
}Then launch the redis-cli client and send a command, e.g., set key1 value1. The mock server will print the following RESP message:
*3
$3
set
$4
key1
$6
value1The message format is:
*<number of arguments> CRLF
$<byte length of argument 1> CRLF
<argument 1 data> CRLF
$<byte length of argument 2> CRLF
<argument 2 data> CRLF
...
$<byte length of argument N> CRLF
<argument N data> CRLFHere CRLF corresponds to the characters \r\n.
Redis defines several response types:
Simple Strings
Start with +, contain a single line, end with \r\n. Successful commands often return +OK.
Error Replies
Start with -, contain an error type and description, and end with \r\n. They are used for malformed commands or wrong argument counts.
Integer Replies
Start with :, contain a decimal integer, and end with \r\n. Used for commands like INCR, LLEN, or EXISTS.
Bulk Strings
Start with $, followed by the byte length, \r\n, the data, and another \r\n. A length of -1 indicates a null value.
Arrays (Multi‑Bulk Replies)
Start with *, followed by the number of elements, then a series of bulk string replies. This is used to return multiple values such as a list of elements.
Below is a Java example of a Redis client that builds and sends a RESP command and reads the server’s reply:
private static void client() throws IOException {
String CRLF = "
";
Socket socket = new Socket("localhost", 6379);
try (OutputStream out = socket.getOutputStream()) {
StringBuilder sb = new StringBuilder();
sb.append("*3").append(CRLF)
.append("$3").append(CRLF).append("set").append(CRLF)
.append("$4").append(CRLF).append("key1").append(CRLF)
.append("$6").append(CRLF).append("value1").append(CRLF);
out.write(sb.toString().getBytes());
out.flush();
try (InputStream inputStream = socket.getInputStream()) {
byte[] buff = new byte[1024];
int len = inputStream.read(buff);
if (len > 0) {
String ret = new String(buff, 0, len);
System.out.println("Recv:" + ret);
}
}
}
}Running this client prints +OK, confirming successful communication.
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.
Xiao Lou's Tech Notes
Backend technology sharing, architecture design, performance optimization, source code reading, troubleshooting, and pitfall practices
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.
