Step 1 of 3 · Reading · ~2 min
The ECHO Command
RESP Protocol Fundamentals
ECHO — Returning Data
ECHO is almost trivially simple: it takes exactly one argument and sends it straight back. Its real purpose in this course isn't the command itself — it's the first commitment to a bulk string reply, and the first real test of your command dispatcher handling more than one command.
The bulk string format, again
Any time you need to return arbitrary, possibly-long, possibly-binary data, RESP uses the bulk string encoding:
$<byte-length>\r\n<data>\r\n
For ECHO hello world (if you're treating the whole remainder of the line as the message), the reply is:
$11\r\n
hello world\r\n
Note the length is a byte count, not a character count — this distinction doesn't matter yet for ASCII input, but it's worth internalizing now because it becomes critical once you support UTF-8 or arbitrary binary payloads in the RESP array parser later in this chapter.
Growing your dispatch table
With two commands now, resist the urge to write:
Instead, start structuring your handlers as a lookup table keyed by uppercased command name:
A shared bulk_string(s) helper that computes $len\r\n{s}\r\n will be reused by nearly every command you write for the rest of this course (GET, INCR replies as strings in some clients, error messages, etc.), so factor it out now rather than duplicating the format string everywhere.
Edge cases to consider
- No argument: what should
ECHOwith nothing after it do? Real Redis returns a wrong-arguments error (you'll formalize this in the Error Handling lesson) — for now, make sure your code doesn't crash with anIndexErrorifargsis empty. - Multiple words: if your line-splitting logic treats every space-separated token as a separate argument,
ECHO hello worldlooks like two arguments, not one two-word message. Decide now whether ECHO takes "the rest of the line" as a single argument, and be consistent — this exact ambiguity is why real Redis clients never send space-delimited text (you'll replace this entire parsing strategy with length-prefixed RESP arrays in a later lesson).
Keep this command simple, but write it in a way that scales: by the end of this chapter your dispatcher will hold a dozen commands sharing the same reply helpers.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…