Build Your Own Redis Challenge

I’ve found ‘Build Your Own X’ tutorials to be a very effective way of improving my understanding of tools we use everyday, like Git, Docker, Redis etc.

What I cannot create, I cannot understand

- Richard Feynman

The problem with many of these tutorials is that they aren’t interactive. It’s okay when tutorials are small, but as they grow in size it can get very hard to follow.

I wish there was a HackerRank-like product where I could, over time, complete ~20 steps and build a large-ish program, while receiving constant TDD-style feedback. This could also be used as a recruiting tool.

This ‘Build Your Own Redis’ challenge is an attempt at such a product. It serves as a code-along challenge for my ‘Build Your Own Redis’ series.

How it works

You start out by cloning a repo that has a basic ‘Hello World’ scaffolding for your language, and a tester utility that guides you through the challenge.

Run make test the first time, and you’re presented with something like this:

redis-stage-1

The tester attempted to connect to Redis’s default port (6379), but wasn’t able to.

Once you’ve written the code to start a server on 6379, tests for the first stage are green.

redis-stage-1-done

Now you can proceed to the next step:

redis-stage-2

You can also choose to report results to a leaderboard:

redis-leaderboard

(Thanks to @jayantbh for the gorgeous UI!)

Redis Challenge Stages

The Redis challenge has multiple stages.

Stage 0: Bind to a port

Spawn a TCP server that binds to Redis’s default port and accepts connections from clients.

Stage 1: PING <-> PONG

Respond to a PING command. You’ll need to reply using the Redis Protocol.

Stage 2: ECHO… O… O…

Respond to an ECHO command. In stage 1, you didn’t really have to parse what the user sent you, but in this stage you will. Time to parse RESP!

Stage 3: Multiple Clients

Handle multiple clients at once.

Resist the temptation to use multiple threads. Do this using a single thread (like the original Redis does!). Hint: Build an event loop using select.

Stage 4: SET & GET

Implement SET and GET. Your server must now be capable of holding state!

Stage 5: Expiry!

There’s a flag in SET that allows a client to set an expiry on a key. Find it, and add it to your Redis implementation.

Stages 6-10:

Coming soon!

Note: Contact me if you’d like to suggest next stages.

How do I try this out?

Clone one of these repos:

(If you’d like to attempt this in a different language, do let me know!)

Follow the instructions in README.md. If you’d like to be part of a leaderboard, contact me for an API key.

Happy hacking!