Skip to main content

Chapter 1: RESP Protocol

Every Redis command travels over the network as RESP (Redis Serialization Protocol). In this chapter, we’ll build a complete RESP parser and serializer — the foundation for our Redis clone. Why start with the protocol instead of jumping straight to data storage? Because the protocol is the contract between every client and server. If you get this wrong, nothing else works — no redis-cli, no application library, no monitoring tool will be able to talk to your server. Think of RESP as the shared language: before two people can have a productive conversation, they need to agree on a language. RESP is that language for Redis, and it is intentionally simple enough to implement in an afternoon, yet powerful enough to carry binary data of any size.
Prerequisites: Go basics, understanding of TCP
Further Reading: Networking Fundamentals
Time: 2-3 hours
Outcome: Working RESP parser and writer

What is RESP?

RESP is a simple, efficient text-based protocol. Every piece of data starts with a type indicator:
Why RESP?
  • Human-readable for debugging — you can telnet to a Redis server and type commands by hand. Try doing that with a binary protocol like gRPC.
  • Simple to implement — the entire spec fits on a single page. This is deliberate: Salvatore Sanfilippo (antirez) designed RESP to be implementable by any developer in any language in a few hours.
  • Efficient to parse (O(n)) — length prefixes on bulk strings mean the parser knows exactly how many bytes to read, with no scanning for delimiters inside the data.
  • Binary-safe — bulk strings can contain any byte, including null bytes and newlines, because the length is specified upfront rather than relying on a terminator.

Real Examples

Client Request (SET command)

Server Response

GET Response with Value

GET Response for Missing Key


Project Setup

Project Structure


Implementation

Step 1: Define RESP Types

internal/protocol/types.go

Step 2: Build the Parser

internal/protocol/parser.go

Step 3: Build the Writer

internal/protocol/writer.go

Testing the Parser

internal/protocol/parser_test.go
Run tests:

Exercises

RESP3 (Redis 6+) adds more types:
Extend your parser to handle these types.
Create benchmarks for your parser:
Compare with different buffer sizes.
Implement a channel-based parser that streams values:

Key Takeaways

Type Prefixes

Every RESP value starts with a type indicator: +, -, :, $, or *

Length-Prefixed

Bulk strings and arrays include their length for efficient parsing

Binary Safe

Bulk strings can contain any bytes, including nulls

CRLF Terminated

All lines end with \r\n for cross-platform compatibility

What’s Next?

In Chapter 2: TCP Server, we’ll:
  • Build a TCP server that accepts connections
  • Handle multiple clients concurrently with goroutines
  • Integrate our RESP parser

Next: TCP Server

Build the network layer for your Redis clone