Learn network programming and data structures by building a Redis-like server from scratch with C/C++.
Why build Redis? The knowledge required is broader and deeper than usual application-level development. It's a good way to level up your skills (mainly network programming and data structures).
Why from scratch? A quote from Richard Feynman: "What I cannot create, I do not understand". Human understandings are filled with holes aka "things I don't know I don't know". Building from scratch is a way to ensure your understanding is complete.
Why C? C is widely used in system programming and infrastructure software. Learn how C is used in real projects, with straightforward C code, no prior C/C++ experience required.
Why a book? The Redis project is a complex software system built with lots of effort, which can be harder to grasp for beginners. The book breaks down the core concepts into easy-to-digest steps.
AI Reading Assistant
Whole-book reading guide from stratified index samples; jump to passages in the text
Tip the Site
Support this siteYour recognition and a small knowledge-service contribution help keep this technical work open source.Scan the WeChat Pay or Alipay code below. Logged-in and guest visitors can both tip.
WeChat Pay
Alipay
Open WeChat or Alipay and scan. No login required.
AI guide
# Build Your Own Redis with C/C++ — Reading Guide
## 【One-Line Pitch】
A hands-on, step-by-step guide to building a Redis-like server from scratch in ~1200 lines of C/C++, teaching network programming and data structures through real implementation. Perfect for developers who want to move beyond application-level coding and truly understand how infrastructure software works.
## 【Book Arc】
- **Opening (~0%–5%)**: Sets the philosophical foundation — why building from scratch matters (Feynman's "What I cannot create, I do not understand"), why Redis is the ideal learning target, and why C is the right language. Outlines the book's incremental approach where each chapter builds on the previous one.
- **Early (~5%–16%)**: Introduces socket programming fundamentals — the socket(), bind(), listen(), accept() syscalls — then walks through writing a simple (intentionally broken) server/client pair. Covers the 4-byte header + body protocol design and the critical concept of handling partial reads/writes.
- **Early (~16%–32%)**: Transitions from blocking I/O to the event loop model using poll(). Explains nonblocking sockets, EAGAIN handling, buffer management, and state machines (STATE_REQ → STATE_RES). Introduces pipelining and the challenge of parsing multiple requests from a single buffer.
- **Middle (~37%–53%)**: Implements the core Redis commands (get/set/del) with a hash table. Covers progressive resizing with dual tables to avoid server stalls, embedded HNode design for generic data structures, and a serialization protocol for responses (nil, int, string, array types).
- **Middle (~53%–58%)**: Extends the command set with keys scanning and error handling, then introduces the sorted set data structure. Explains why AVL trees are chosen over skiplists (simplicity of implementation) and covers tree rotations and balance fixing.
- **Late (~58%–end)**: Continues with AVL tree implementation details — node rotations, depth tracking, and rebalancing logic. The book concludes with exercises (like swapping poll for epoll) and hints, pushing readers to extend the code independently.
## 【Key Takeaways】
- **Building from scratch reveals unknown unknowns** (Opening): The Feynman quote isn't just motivation — it's the core pedagogy. You don't know what you don't know until you try to create it, and this book forces that confrontation systematically.
- **Network programming is more than socket APIs** (Early): Understanding event loops, protocols, and timers prevents fatal mistakes even when using high-level libraries. The book covers these core concepts rather than just showing API calls.
- **Protocol design requires explicit message framing** (Early): The 4-byte length header approach solves message boundary problems. Text protocols are human-readable but require more parsing; binary protocols are more efficient but less debuggable — a real trade-off engineers must make.
- **Nonblocking I/O with EAGAIN handling is the heart of event loops** (Early): The poll()-based event loop with per-connection state machines (STATE_REQ/STATE_RES) is the pattern behind all high-performance servers. Understanding this pattern transfers directly to epoll, kqueue, and frameworks.
- **Pipelining breaks naive assumptions** (Early): When clients send multiple requests without waiting for responses, you can't assume one request per read buffer. The try_one_request() pattern — parsing incrementally from a buffer — is essential for real-world servers.
- **Progressive resizing prevents server stalls** (Middle): Redis's dual-hash-table approach (gradually migrating nodes between tables) solves the latency problem of resizing large hashtables. This is a production-grade technique, not a textbook simplification.
- **Embedded nodes make data structures generic and memory-efficient** (Middle): The HNode embedded in payload structs (rather than separately allocated) means the data structure code doesn't own payloads — it merely organizes them. This is a key insight for writing reusable C data structures.
- **AVL trees are the pragmatic choice for sorted data** (Middle): While Redis uses skiplists, AVL trees are simpler to implement correctly. The rotation-based rebalancing (left/right rotations with depth tracking) is a fundamental algorithm worth mastering.
## 【Reading Tips】
- **Skim the opening chapters** (~0%–5%) if you're already convinced about the value of building from scratch — the real substance starts with socket programming. But don't skip the protocol design discussion in chapter 3–4; it's foundational.
- **Deep-read the event loop chapter** (~16%–32%): This is the most conceptually dense material. Trace through the state machine transitions carefully, and make sure you understand why EAGAIN is not an error but a signal to yield.
- **Code along, don't just read**: The book explicitly encourages DIY without the reference code. Type out the server yourself, break it, fix it. The exercises (like switching poll to epoll) are excellent for cementing understanding.
- **Pay special attention to buffer management** (~26%–32%): The memmove optimization exercise (only reclaiming buffer space before read, not after every request) teaches a real performance lesson that applies broadly.
- **For the AVL tree section** (~53%–58%): Draw the rotations on paper. The rot_left/rot_right code is mirror-symmetric, and understanding one makes the other trivial. The avl_fix loop walking up to the root is the trickiest part — trace it with concrete examples.
## 【Coverage Limits】
The excerpts cover roughly the first 58% of the book in detail (through AVL tree implementation). Later chapters on thread pools, asynchronous tasks, and additional Redis features are mentioned but not covered in this guide's analysis.
##
Page 10
he previous one, adding a new concept. The full source code is provided on the web for reference purposes, readers are advised to tinker with it or DIY witho...
s: do_something_with(fd) def do_something_with(fd): if fd is a listening socket: add_new_client(fd) elif fd is a client connection: while work_not_done(fd):...
y be quite a new idea to you if you learned data structures from textbooks, which is probably using void * or C++ templates or even macros. Listing the do_ge...
removing root? return victim; } } } This is the generic function for removing nodes from a binary tree, with the AVL- tree-specific avl_fix. Readers with exp...
destroy((Entry *)arg); } // dispose the entry after it got detached from the key space static void entry_del(Entry *ent) { entry_set_ttl(ent, -1); const size...
Support this siteYour recognition and a small knowledge-service contribution help keep this technical work open source.
Scan the WeChat Pay or Alipay code below. Logged-in and guest visitors can both tip.
WeChat PayAlipay
Open WeChat or Alipay and scan. No login required.
Add Tag
Enter tag name (max 50 characters)
Share E-Book
Build Your Own Redis with CC++ (James Smith) (Z-Library)
Scan QR code with your phone to access
Copy the link or scan the QR code to access this e-book on your phone
Share E-Book via Email
Please enter email address
Donation Statistics
¥.00
Total Donations
0
Donation Count
Build Your Own Redis with CC++ (James Smith) (Z-Library)
Find Your Favorite Books
Only registered users can comment after logging in. Comments need to be reviewed by administrators before being displayed
Loading comments...
Reply to Comment
Edit Comment