Reading — step 1 of 3
Understanding PING
PING — Your First Redis Command
Welcome to Build Redis from Scratch! In this project course, you'll implement a working Redis clone step by step, starting from the simplest possible command.
What is Redis?
Redis is an in-memory data structure store used as a database, cache, and message broker. It's one of the most popular databases in the world — used by Twitter, GitHub, Stack Overflow, and thousands of other companies.
What makes Redis special:
- Blazing fast: All data lives in RAM. Operations complete in microseconds.
- Rich data types: Not just key-value — Redis supports strings, lists, sets, sorted sets, hashes, and more.
- Simple protocol: The Redis Serialization Protocol (RESP) is human-readable and easy to implement.
The RESP Protocol
Redis clients and servers communicate using RESP (Redis Serialization Protocol). Every response has a type prefix as its first byte:
| Prefix | Type | Example |
|---|---|---|
+ | Simple String | +OK\r\n |
- | Error | -ERR unknown command\r\n |
: | Integer | :42\r\n |
$ | Bulk String | $5\r\nhello\r\n |
* | Array | *2\r\n$3\r\nfoo\r\n$3\r\nbar\r\n |
Every piece ends with \r\n (carriage return + line feed).
The PING Command
PING is the simplest Redis command. It's used to test if the server is alive.
PING→+PONG\r\n(simple string response)PING hello→$5\r\nhello\r\n(bulk string echo)
A bulk string encodes the length first: $<length>\r\n<data>\r\n
Your Task
Build a program that:
- Reads commands from stdin (one per line)
- Parses the command name and arguments
- For
PING, writes the correct RESP response to stdout
This is the foundation everything else builds on. Let's go!
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…