Updated February 17, 2023
Introduction to Redis Protocol
Redis protocol is used by redis clients, and redis uses serialization protocol for its client. Basically, redis uses the RESP (redis serialization protocol) to communicate with the server. It is designed specifically for the redis server, so we can use this protocol for other software application projects. Redis resp protocol is human readable, which gives it an advantage over other protocols that implement quality debugging and good quality.
- RESP is a request and response protocol in which the client sends the request to the server and the server responds to the client based on their request.
- There are multiple data types available for protocol. We are using RESP2 and RESP3 protocols.
What is Redis Protocol?
We can switch in different protocols by setting and authenticating the name of the connection and client report. Version 6, it will support two protocols RESP2 and RESP3. The RESP protocol is introduced in version 1.2. RESP is the protocol that is implemented by using redis client. RESP is the serialization protocol that supports multiple data types.
Redis will use the RESP request response protocol in using the following way. Client sends the command to the server as RESP array for the bulk of strings. The server replying with according to RESP types for the command implementation. RESP is a human readable protocol, this will conferring to the RESP protocol. By using it we are encoding the simple strings.
Redis Protocol Specification
Protocol is made up of several components. As we all know, the RESP protocol of Redis supports a variety of data types.
RESP protocol comprises the following:
- Redis protocol is simple for implementing.
- We can parse the protocol easily.
- It is easy to read for humans.
The client will connect to the server via TCP. The port number for connecting to the server is 6379. The below figure shows how we can connect to the server by using a TCP connection.
Redis will accept commands composed of different types of arguments. When the server will receive the command from the client, first it will process and then it will reply back to the client. Redis supports the feature of pipelining, so it is possible for the client to send multiple commands at a single time.
When a client subscribes to a pub or sub, the protocol’s semantics change, and the protocol is renamed push. The hello command returns all of the connection properties of the server. When we use the hello command without any arguments, the default protocol name RESP3 is used. The RESP2 protocol connection details are shown below as an example.
Suppose we need to use the RESP3 protocol, then we need to define the 3 arguments with hello command. The below example shows how we can use the RESP3 protocol as follows.
resp is a serialization protocol that supports strings. By using RESP simple strings are encoded by using plus character.
Redis uses the request response protocol in using following ways as follows:
1. Client sends a command to the server as RESP array of strings.
2. The server replies with RESP types as per the implementation of the command.
The RESP3 protocol is an upgraded version of the RESP2 protocol, which was previously used in redis. RESP3 protocol was designed to handle request and response interactions between server and client. In that case, the client makes a request, and the server responds to the request. The RESP protocol is designed to send non-structured commands such as SADD, set, and myset. Those commands are represented by arrays, with each element containing the array elements.
Redis Protocol Data Type
Below are the data types used in the protocol. Simple strings are important data types in redis data type.
- Simple strings
- Bulk strings
RESP represents the null value which is a variation of the array or bulk array string.
1. Simple strings
Simple strings are encoded in the protocol by using the + character followed by a string that does not contain the LF or CR characters. Redis uses simple strings to transmit non-binary strings with minimal overhead. Many commands only return OK when executed successfully. The below example shows simple string data types of redis protocol.
SET key val
The RESP protocol includes an error of a specific data type. This is similar to the simple strings, but the first character will have a minus sign. The difference between errors and simple strings is that the client treats exceptions as errors, whereas strings compose the type of error. The below example shows the error data type of protocol as follows.
This data type simply contains a terminated string that represents an integer. Multiple commands will return integers such as LLEN, incr, and lastsave. True or false will be returned by an integer. The Exist command returns 0 for false and 1 for true. The below example shows the integer data type of protocol.
4. Bulk strings
This data type is used to represent the single binary string. Bulk string is encoded by using $ byte which was followed by byte number, final CRLF, and actual string data. The bulk string is also used in non-existence value which is used to represent the null value, in the same format length is -1 and it will not contain any data. The below example shows bulk strings data type of protocol as follows.
The client sends the commands to the server by using an array. Redis commands return an element of the collection to the client which uses the array for replies. The below example shows the array data type of protocol as follows.
Given below are the FAQs mentioned:
Q1. What is the use of redis protocol?
Answer: It is used to send the request to the server and the server is used to give the response to the client.
Q2. How many data types are we using in redis protocol?
Answer: There are five data types we are using in redis protocol, i.e. array, simple strings, bulk strings, integer and error.
Q3. Which protocol we are using in redis?
Answer: We are using two protocols in redis, i.e. RESP and RESP3.
We can switch in different protocols by setting and authenticating the name of the connection and client report. From redis version 6 it supports two protocols RESP2 and RESP3. It is designed specifically for the redis servers, so we can use this protocol for other software application projects.
This is a guide to Redis Protocol. Here we discuss the introduction, redis protocol specification, data type, and FAQ. You may also have a look at the following articles to learn more –