Coding Challenge #8 - Build a Redis Server
John Crickett
Helping you become a better software engineer by building real-world applications.
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:
In RESP, the first byte determines the data type:
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.:
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.
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
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
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.
??? Security Sleuth | ?? Automation Aficionado | ?? Compliance Conjurer
1 年1000%