Coding Challenge #8 - Build a Redis Server
Photo by Kevin Ku

Coding Challenge #8 - Build a Redis Server

This week’s challenge is to build your own Redis Server.

Redis is an in-memory data structure server, which supports storing strings, hashes, lists, sets, sorted sets and more.

The name Redis reflects the original goal to be a Remote Dictionary Server. Salvatore Sanfilippo the creator of Redis originally wrote it in just over 300 lines of TCL, you can see that original version in a gist he posted here.

Since the first version in 2009, Redis has been ported to C and released as open source. It’s also become one of the most widely used key-value / NoSQL databases.

The Challenge - Building A Redis Server

In this challenge you’re going to build your own lite version of Redis which will support the same operations as the original version of Redis.

Like all the challenges, you can tackle it in any programming language you like. Building a Redis clone in Rust was what inspired me to start this newsletter.

Step Zero

In this introductory step you’re going to set your environment up ready to begin developing and testing your solution.

I’ll leave you to choose your target platform, setup your editor and programming language of choice. I’d encourage you to pick a tech stack that you’re comfortable doing both network programming (we’re building a server) and test driven development (TDD) with.

Once you’ve done that you might like to install Redis itself so you can use it’s CLI client as a test client for your implementation.

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. You may want to refer to the RESP protocol specification.

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. My personal approach to this would be to use 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$5\r\nhello world\r\n”
  • "*2\r\n$3\r\nget\r\n$3\\r\nkey\r\n”
  • "+OK\r\n"
  • "-Error message\r\n"
  • "$0\r\n\r\n"
  • "+hello world\r\n”

Plus some invalid test cases to test outside the happy path.

Step 2

In this step your goal is to create the Redis Lite server. It should start up and begin listening for clients on the port: 6379.

You can find the rest of the build your own Redis Server challenge on the Coding Challenges website.

You can subscribe to receive the full challenge by email every week via the Coding Challenges Substack.


Jawahar Selvaraj

Staff Engineer @ Cloudbees

1 年

Here is the implementation of redis server in Golang. https://github.com/jawahars16/redis-lite Handling TCP connections and concurent requests were challenging. Good learning on RESP as well. Thanks John Crickett

Mohit Jain

Software Tech Lead @Zemetric | ex Co-Founder @Evy Energy (Acquired)

1 年

This was a good challenge to learn about TCP ports and how to handle sockets in Typescript. Building the serializer and deserializer was relatively easy. Handling parallel requests was a challenge. Here's my code with comments achieved with thorough tests - https://github.com/jainmohit2001/coding-challenges/tree/master/src/8

Omar Halabieh

Tech Director @ Amazon Payment Services | #1 LinkedIn Arab World Creator in Management & Leadership | Follow me for Daily Insights on Leadership, Management and Career | Mentor

1 年

Awesome post! John Crickett I couldn't agree more with the idea that building fully functioning applications is the best way to level up your coding skills. Keep up the great work.

Cody Ingram-Moore

??? Security Sleuth | ?? Automation Aficionado | ?? Compliance Conjurer

1 年

1000%

要查看或添加评论,请登录

社区洞察

其他会员也浏览了