|
| 1 | +--- |
| 2 | +author: Nimendra |
| 3 | +title: "System Design Notes: Redis Serialization Protocol (RESP)" |
| 4 | +date: 2025-07-12 |
| 5 | +description: "Explore the Redis Serialization Protocol (RESP)" |
| 6 | +tags: ["redis", "protocols", "networking", "RESP", "System-Design"] |
| 7 | +categories: ["Backend", "Redis"] |
| 8 | +lastmod: 2025-07-12 |
| 9 | +showtoc: true |
| 10 | +TocOpen: false |
| 11 | +ShowReadingTime: true |
| 12 | +ShowPostNavLinks: true |
| 13 | +ShowBreadCrumbs: true |
| 14 | +ShowCodeCopyButtons: true |
| 15 | +editPost: |
| 16 | + URL: "https://github.com/nmdra/nmdra.github.io/tree/main/content" |
| 17 | + Text: "Suggest edit" |
| 18 | + appendFilePath: true |
| 19 | +--- |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +Redis is a high-performance in-memory data store, widely used as a database, cache, and message broker. Unlike many web services that use HTTP, Redis uses a custom protocol known as the **Redis Serialization Protocol (RESP)** for communication between clients and servers. |
| 24 | + |
| 25 | +In this article, we’ll explore RESP, how it compares to HTTP, and what makes it ideal for Redis’s design philosophy. |
| 26 | + |
| 27 | +## What is RESP? |
| 28 | + |
| 29 | +RESP, or **Redis Serialization Protocol**, is the **wire protocol** used by Redis clients (including `redis-cli`) to communicate with the Redis server. It defines the way data is serialized and transmitted over the network. |
| 30 | + |
| 31 | +> A **wire protocol** is the formal specification of how data is formatted and exchanged between two systems over a network connection. |
| 32 | +> — [Wikipedia](https://en.wikipedia.org/wiki/Wire_protocol) |
| 33 | + |
| 34 | +RESP is lightweight, efficient, and designed specifically for Redis — but it can be reused in any client-server software. |
| 35 | + |
| 36 | +## RESP vs HTTP |
| 37 | + |
| 38 | +Redis does **not** use HTTP. Instead, it defines its own set of commands and responses via RESP. Here’s how RESP equivalents map to typical HTTP operations: |
| 39 | + |
| 40 | +| **HTTP Feature** | **Redis Equivalent** | |
| 41 | +|------------------------|--------------------------------------------------| |
| 42 | +| `GET /resource` | `GET key` | |
| 43 | +| `POST /resource` | `SET key value` | |
| 44 | +| `PUT /resource` | `SET key value`, or `HSET`, `JSON.SET` (module) | |
| 45 | +| `DELETE /resource` | `DEL key` | |
| 46 | +| `200 OK` | `+OK` | |
| 47 | +| `404 Not Found` | `(nil)` | |
| 48 | +| `500 Internal Error` | `-ERR <message>` | |
| 49 | +| `403 Forbidden` | `-NOAUTH Authentication required.` | |
| 50 | + |
| 51 | +Unlike HTTP, RESP is not based on request methods or status codes. Instead, Redis commands and responses follow a minimal and efficient format optimized for performance. |
| 52 | + |
| 53 | +## RESP Characteristics |
| 54 | + |
| 55 | +| Feature | Description | |
| 56 | +|----------------------------|-----------------------------------------------------------------------------------------| |
| 57 | +| **Text-based** | Commands and responses are human-readable (but binary-safe for values). | |
| 58 | +| **Persistent TCP** | Communication happens over a persistent TCP connection (default port: `6379`). | |
| 59 | +| **Request/Response Model** | Clients issue commands (e.g. `SET key value`) and Redis replies with a response. | |
| 60 | + |
| 61 | +RESP works over **TCP or Unix sockets**, not HTTP. You can even **telnet** into Redis and type RESP commands manually. |
| 62 | + |
| 63 | +## RESP Data Types |
| 64 | + |
| 65 | +RESP supports the following basic data types: |
| 66 | + |
| 67 | +| **Type** | **Prefix** | **Description** | |
| 68 | +| --------------- | ---------- | ----------------------------------------------------------- | |
| 69 | +| Simple String | `+` | Usually used for status replies. | |
| 70 | +| Error | `-` | Used to signal errors. | |
| 71 | +| Integer | `:` | Used to return numeric values (e.g. result of `INCR`). | |
| 72 | +| Bulk String | `$` | String with specified length, can include binary-safe data. | |
| 73 | +| Null Bulk | `$-1` | Used to represent a null value. | |
| 74 | +| Array | `*` | List of other RESP types. | |
| 75 | +| Null Array | `*-1` | Used to represent a null array (RESP3). | |
| 76 | +| Map (RESP3) | `%` | Key-value pairs (available in RESP3). | |
| 77 | +| Set (RESP3) | `~` | Unordered collection of unique elements (RESP3). | |
| 78 | +| Boolean (RESP3) | `#` | `true` or `false` in RESP3. | |
| 79 | +| Double (RESP3) | `,` | Floating point number in RESP3. | |
| 80 | + |
| 81 | +## Security in RESP |
| 82 | + |
| 83 | +Security in RESP is **minimal by default**, because Redis was designed for **trusted local environments**. Here’s what that means: |
| 84 | + |
| 85 | +- RESP is **plaintext**, meaning commands and data are sent unencrypted. |
| 86 | +- Optional password authentication via `requirepass`. |
| 87 | +- No user roles (before Redis 6). |
| 88 | +- Redis will accept any client connection if the port is reachable. |
| 89 | +- From Redis 6 onward, **SSL/TLS** support is available (needs to be compiled in), enabling **encrypted RESP communication** over TLS — just like HTTPS vs HTTP. |
| 90 | + |
| 91 | +More on [Redis TLS support](https://redis.io/docs/latest/operate/oss_and_stack/management/security/encryption/) |
| 92 | + |
| 93 | +## RESP Versions |
| 94 | + |
| 95 | +| Version | Introduced In | Features | |
| 96 | +|-----------|---------------|--------------------------------------------------------------------------| |
| 97 | +| **RESP2** | Redis 2.x | Basic protocol used by most clients today. Fast and text-based. | |
| 98 | +| **RESP3** | Redis 6.0 | Supports richer data types, attribute metadata, and **introspection**. | |
| 99 | + |
| 100 | +> **Introspection** in RESP3 allows clients to query metadata about the server or connection, improving diagnostics and tooling. |
| 101 | + |
| 102 | +## Summary |
| 103 | + |
| 104 | +Redis’s RESP protocol is a prime example of designing a protocol specifically for performance, simplicity, and predictability. While it lacks the verbosity and feature set of HTTP, it excels in its intended domain — powering fast, efficient key-value operations over a lightweight protocol. |
| 105 | + |
| 106 | +--- |
| 107 | + |
| 108 | +- Official RESP Spec: [redis.io/docs/latest/develop/reference/protocol-spec](https://redis.io/docs/latest/develop/reference/protocol-spec) |
| 109 | +- Build Your Own Redis (CodeCrafters): [codecrafters.io](https://app.codecrafters.io/r/gentle-armadillo-148010) |
| 110 | + |
| 111 | + |
0 commit comments