Skip to content
Lesson 1 of 29

Step 1 of 3 · Reading · ~2 min

Understanding PING

RESP Protocol Fundamentals

Your First Redis Command

Every Redis client talks to the server using a simple text-based protocol called RESP (REdis Serialization Protocol). Before you build storage, expiry, or persistence, you need a command loop that can read a request and write a correctly-framed response. PING is the smallest possible command, which makes it the perfect place to start.

The command loop

At its core, a Redis server (and, for now, your clone) is an infinite loop:

  1. Read a line of input (a command).
  2. Parse it into a command name and arguments.
  3. Dispatch to a handler.
  4. Write the RESP-encoded reply.
  5. Repeat.
python

Why +PONG\r\n?

RESP encodes every reply with a type prefix so the client knows how to parse it without ambiguity:

  • + marks a simple string — a short, trusted status reply like OK or PONG.
  • Every RESP message ends in \r\n (CRLF), not just \n. This is a protocol requirement inherited from Redis's original C implementation — get it wrong and real clients (and your test harness) will hang waiting for the second byte.

PING with an argument

Real Redis lets you pass an optional message: PING "hello" replies with the message itself, encoded as a bulk string rather than a simple string:

$5\r\nhello\r\n

The $ prefix is followed by the byte length of the payload, then CRLF, then the raw bytes, then a trailing CRLF. The length prefix matters more than it looks like it should right now — you'll rely on it heavily once values can contain arbitrary bytes.

What to watch for

  • Don't forget to strip only the newline, not trailing spaces that might be meaningful later.
  • PONG is capitalized; RESP simple strings are case-sensitive on the wire even though command names aren't.
  • Keep your command dispatch built around an uppercased command name from the start — you'll be adding many more commands and want a consistent lookup table, not a chain of if statements that grows forever.

This lesson is intentionally narrow: get the read-parse-dispatch-write skeleton right, and every later command (ECHO, SET, GET, EXPIRE...) slots into the same loop.

Up nextECHO — Returning DataRESP Protocol Fundamentals

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…