• 7.dev
  • Posts
  • How to Build a CLI Tool to Send Commands to a Redis Server

How to Build a CLI Tool to Send Commands to a Redis Server

Happy Friday! This is 7.dev with another newsletter all about coding and AI in your inbox.

In this issue, I’ll show you how to build a CLI tool to send commands to a Redis server inspired by the coding challenger John Crickett

Before I get into it: If you also like podcasts, check out the 7.dev Podcast (My favorite). I’ll cover hacks, tips, and tricks every Wednesday in the full-stack development world. You can also search “7.dev podcast” on anywhere you find your podcasts!

Check out the latest episode here:

Alright and with that, let’s get into the how-to:

Step 1

In this step your goal is to build the functionality to serialise and de-serialise Redis Serialisation Protocol (RESP) messages. This is the protocol used to communicate with a Redis Server.

Redis uses RESP as a request-response protocol in the following way:

  • Clients send commands to a Redis Server as a RESP Array of Bulk Strings.

  • The server replies with one of the RESP types according to the command implementation.

In RESP, the first byte determines the data type:

  • For Simple Strings, the first byte of the reply is "+"

  • For Errors, the first byte of the reply is "``"

  • For Integers, the first byte of the reply is ":"

  • For Bulk Strings, the first byte of the reply is "$"

  • For Arrays, the first byte of the reply is "``"

RESP can represent a Null value using a special variation of Bulk Strings: "$-1\r\n" or Array: "*-1\r\n".

Now that we have the basics of the protocol, your challenge is to write the code required to serialise and de-serialise messages. John approaches this by using test-driven development (TDD) to build tests for some example messages, i.e.:

  • "$-1\r\n"

  • "*1\r\n$4\r\nping\r\n”

  • "*2\r\n$4\r\necho\r\n$11\r\nhello world\r\n”

  • "*2\r\n$3\r\nget\r\n$3\r\nkey\r\n”

  • "*3\r\n$3\r\nset\r\n$3\r\nkey\r\n$5\r\nvalue\r\n”

  • "+OK\r\n"

  • "-Error message\r\n"

  • "$0\r\n\r\n"

  • "+hello world\r\n”

Don’t forget to include some invalid test cases too, just to make sure that when things aren’t supposed to work, they don’t!

Step 2

In this step you’ll build a simple tool that opens a network connection to a Redis server running on localhost:6379, sends the PING command to it, waits for a response, decodes and prints the response.

In short running it will look something like this:

% ccredis-cli
PONG

Step 3

In this step, your goal is to support selecting the host and port from the command line, as well as allow the execution of multiple commands until the user enters the quit command.

Firstly allow the user to specify the host and port of the server to connect to, like this:

% ccredis-cli -h localhost -p 6379

Next instead of sending the PING command, provide a prompt to the user which shows the host and port they are connected to and allows them to enter a command, something like this:

% ccredis-cli
127.0.0.1:6379>ping
PONG
127.0.0.1:6379>ping Hello
Hello
127.0.0.1:6379>echo HI!
HI!
127.0.0.1:6379>set key value
OK
127.0.0.1:6379>get key
value
127.0.0.1:6379>quit

The quit command should not be sent to the server, but should exit the program, closing the network connection to the server.

Step 4

In this step your goal is to support the help command and provide help for each of the commands Redis supports. You’re looking to make this work:

% ccredis-cli
localhost:6379> help
To get help about Redis commands type:
      "help @<group>" to get a list of commands in <group>
      "help <command>" for help on <command>
      "quit" to exit
localhost:6379> help set

  SET key value [NX|XX] [GET] [EX seconds|PX milliseconds|EXAT unix-time-seconds|PXAT unix-time-milliseconds|KEEPTTL]
  summary: Set the string value of a key
  since: 1.0.0
  group: string

localhost:6379>quit
% 

You can find the content to use for the help in the Redis GitHub - redis/src/commands as a set of JSON files.

Step 5

In this step, your goal is to support hints for the commands. If you look at the actual Redis CLI tool you’ll see (as shown in the screenshot below) that it offers a hint as to the usage of the command as you type it:

Your task for this step is to extract that information from the JSON files and display the correct hint for the command. A real-world use case for algorithms!

What’s Going on this Week? 🤔

  • OpenAI had a secret breakthrough called Q* that many people are torn about. They even fired their CEO soon after the breakthrough. See more about it here

  • Elon Musk says that xAI will be releasing Grok next week to X Premium + subscribers in a subtle tweet reply.

Check us out on the socials 💅

Every weekday, we publish a video on Instagram, TikTok, and YouTube. If you want more 7.dev than just this weekly email, follow us!

What did you think of this issue?

🫡 Awesome | 🙄 Meh | 🤢 Bad

Login or Subscribe to participate in the poll