01. Introduction
Learn how to build real-world software by coding a Redis server from scratch. If you can build a Redis server, you can build almost any software! Because it teaches you 2 fundamental skills:
- Network programming. The next level of programming is programming for multiple machines. Think HTTP servers, RPCs, databases, distributed systems.
- Data structures. People say they are useless, only used in LeetCode puzzles. Redis is the best counter argument. There are many useful applications of basic data structures, but you need to know enough to apply them.
What is Redis?
Redis is THE most popular in-memory key-value store, used primarily for caching as no storage is faster than memory. Caching servers are inevitable because it’s the easiest way to scale. A cache can take slow DB queries out of the equation.
A cache server is a map<string,string>
over a
network. But in Redis, the “value” part is not just a string, it can be
any of the data structures: hash, list, or sorted set, enabling complex
use cases such as ranking, list pagination, etc. This is why Redis is
called a data structure server.
Why start from scratch?
To quote Feynman: “What I cannot create, I do not understand”. There are different levels of “create” and “understand”. Gluing libraries together is “create”, but you don’t have to stay at that level — you can create something more low-level, and understand something more fundamental.
The goal is not to reinvent the wheel, but the fundamentals will set you apart. You will make better decisions, be better at debugging, and have more career options.
Why C/C++?
The first version of Redis was written in the TCL scripting language. But that’s only a toy version of Redis; the final product is in C. Creating a toy version of something is a great way to understand it. But there’s always more to learn:
- High-performance software requires low-level control, which requires C/C++.
- C doesn’t have built-in data structures or networking, it forces you to learn.
The code is plain C with minimal, optional C++ features. It should be readable without C/C++ experience. I hope this will motivate you to learn C/C++. But other languages can be valid for a subset of the lessons.
- Go is more high-level, but it’s still good for coding data structures and manipulating bits and bytes. It has a mature, built-in networking library, so you won’t learn the full networking lessons.
- Python is too high-level for coding data structures like hashtables, because it’s built-in. Production stuff is mostly C with Python glue. Python only has a thin wrapper for socket, so the networking lessons are still suitable.
- JavaScript is as high-level as Python, but with an invisible event loop and built-in networking. To fully understand JS or Go, you need to know more than JS or Go.
Why should I code a Redis?
- It prepares you for more technical, system-y works and gives you new career options as generic software development is devalued by AI tools.
- It a good practice, especially if you are a student without real projects.
- It’s a quick review of the basics, which is helpful for job interviews.
- It’s fun and intellectually stimulating to know how things work.
About the book
Each chapter is an incremental step with complete source code. The final product is only 1200 lines of code, focusing on principles while skipping uninteresting details. It’s not a mindless Redis clone, many things are done differently to better illustrate the topics, so the result is not compatible with the real Redis.
Get help from the author or ask questions by email: [email protected]
Book series
This is part of the “Build Your Own X” book series, where X includes database, web server, and compiler. Read the web version on the website.