Master the fundamental concepts of protocol implementation through this focused micro-challenge.
Redis speaks RESP over TCP port 6379. Every value starts with a type prefix character:
+OK\r\n simple string-ERR unknown command\r\n error:42\r\n integer$6\r\nfoobar\r\n bulk string (length-prefixed)*2\r\n... array of nested valuesThe GET mykey command encodes as:
cLoading…
That is: array of 2 elements, bulk string "GET" (3 bytes), bulk string "mykey" (5 bytes). Bulk strings use $-1\r\n for NULL; arrays use *-1\r\n for NULL.
A simple state machine reads the prefix byte, parses the length for bulk strings, then consumes exactly that many bytes plus \r\n. No formal grammar needed. Arrays nest arbitrarily: a MULTI/EXEC transaction wraps multiple commands in an outer array, and pipelining sends several encoded commands back-to-back on one TCP connection to 127.0.0.1:6379.
This task asks you to encode a Redis command and decode a RESP response. redis-cli is nothing more than a RESP encoder wrapped around stdin/stdout, and every client library (redis-py, Jedis) uses this same wire format on TCP port 6379. Redis chose RESP because a simple state machine can parse it without a formal grammar, which keeps the server implementation small and fast.
Write a C program that encodes a Redis command into RESP format and decodes a RESP response.
Requirements:
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
All starter code and reference implementations are available for your local setup.
View on Github