How insane-22/mini-redis works
1. Overall shape
- This repository is a single native C++ Redis clone that runs as one server process.
- The architecture is centered on a network-facing server, a RESP parser, a command dispatcher, and a set of subsystem handlers for each Redis feature area.
2. Client ingress and protocol boundary
- src/Server.cpp is the entry point for client connections and request processing.
- src/Parser.cpp and include/Parser.hpp form the protocol boundary: they turn raw Redis RESP input into structured commands that the rest of the system can execute.
3. Command dispatch layer
- src/Handler.cpp and include/Handler.hpp are the central routing layer.
- This layer decides which concrete handler should process a command, and it is the main junction where normal commands, transactions, replication commands, and pub/sub commands converge.
4. String / key-value storage
- src/KvStoreHandler.cpp handles the basic Redis string operations: SET, GET, DEL, EXISTS, INCR, and expiration-related behavior.
- This is the core in-memory store and is the base data model that many other subsystems build on.
5. List, stream, and sorted-set data structures
- src/ListStoreHandler.cpp implements Redis list behavior such as LPUSH, RPUSH, LPOP, RPOP, and LLEN.
- src/StreamStoreHandler.cpp implements stream commands like XADD, XRANGE, and XREAD.
- src/SortedSetHandler.cpp provides sorted-set operations, which are also used as the foundation for geo features.
6. Geo-spatial subsystem
- src/GeoHandler.cpp implements GEOADD and GEOPOS.
- src/GeoEncoding.cpp contains the coordinate encoding logic used to map geographic points into sortable values, which is why geo commands are tied to the sorted-set subsystem rather than being a separate storage engine.
7. Pub/Sub messaging
- src/PubSubHandler.cpp manages channel-based publish/subscribe behavior.
- This subsystem is separate from normal key-value access because it needs to track subscribers and broadcast messages in real time instead of mutating stored data.
8. Replication architecture
- src/ReplicationManager.cpp coordinates master/replica synchronization and command propagation.
- src/ReplicaClient.cpp represents the outbound side of that relationship, handling communication with a replica peer.
- The replication boundary is distinct from normal command execution: commands may be executed locally and then forwarded to replicas.
9. Persistence and RDB loading
- src/Rdb.cpp and src/RdbReader.cpp implement Redis database persistence support.
- RdbReader is the startup/load side that reconstructs state from an RDB snapshot, while Rdb is the persistence-facing subsystem that packages or manages the snapshot format.
- Persistence is complementary to replication: one restores state across restarts, the other keeps replicas in sync while running.
10. Cross-cutting execution model
- The important data flow is: socket input → RESP parsing → command routing → feature-specific handler → response serialization back to the client.
- For replication and persistence, the flow branches out from the same command layer into side systems that either propagate commands to peers or capture state for later reload.
11. Code organization
- Headers in include/ define the interfaces for each subsystem, while src/ contains the implementation.
- CMakeLists.txt, vcpkg.json, and the Codecrafters scripts indicate a buildable single-binary C++ service rather than a multi-service deployment.
12. What to draw in an architecture diagram
- A good diagram would show one central server with four major horizontal concerns around it: protocol/dispatch, data stores, replication, and persistence.
- Under the data-store layer, separate boxes for strings, lists, streams, sorted sets, geo, and pub/sub are the clearest representation of the repository’s modular design.
Main entry points: Handler.cpp Handler · GeoHandler.cpp Geo · ReplicationManager.cpp Replication · Server.cpp Server
Components 14
Ingress
Client connection handling and protocol parsing
Dispatch
Command routing and cross-cutting execution control
- Handler · Routes commands to the correct feature handler and execution path
src/Handler.cpp
Data Stores
Core in-memory Redis data structures and feature handlers
- Kv Store · Implements core key-value commands and expiration behavior
src/KvStoreHandler.cpp - List Store · Implements Redis list operations
src/ListStoreHandler.cpp - Stream Store · Implements stream append and read commands
src/StreamStoreHandler.cpp - Sorted Sets · Implements sorted-set operations used by other features
src/SortedSetHandler.cpp - Geo · Implements geo commands on top of sorted-set encoding
src/GeoHandler.cpp - Geo Encoding · Encodes coordinates into sortable values for geo lookups
src/GeoEncoding.cpp - Pub/Sub · Manages channel subscriptions and live message fanout
src/PubSubHandler.cpp
Distributed
Replication and peer synchronization
- Replication · Coordinates master-replica synchronization and propagation
src/ReplicationManager.cpp - Replica Client · Outbound client used to talk to replica nodes
src/ReplicaClient.cpp
Persistence
RDB snapshot creation and loading
- RDB · Manages Redis database snapshot persistence
src/Rdb.cpp - RDB Reader · Reconstructs in-memory state from RDB snapshots
src/RdbReader.cpp
Connections 15
- Server → Parser: accepts · Server receives raw protocol input and passes it to the parser
- Parser → Handler: parses · Parsed commands are routed into the central dispatcher
- Handler → Kv Store: routes · String and expiration commands go to the key-value store handler
- Handler → List Store: routes · List commands are dispatched to the list subsystem
- Handler → Stream Store: routes · Stream commands are dispatched to the stream subsystem
- Handler → Sorted Sets: routes · Sorted-set commands are dispatched to the zset subsystem
- Handler → Geo: routes · Geo commands are dispatched to the geo subsystem
- Geo → Geo Encoding: encodes · Geo operations rely on coordinate encoding
- Geo → Sorted Sets: builds on · Geo functionality is layered on top of sorted-set behavior
- Handler → Pub/Sub: routes · Pub/sub commands are handled by the messaging subsystem
- Handler → Replication: propagates · Dispatcher forwards replication-relevant activity into synchronization
- Replication → Replica Client: connects to · Replication manager uses an outbound replica client
- Handler → RDB: persists · Command execution can feed snapshot persistence
- RDB Reader → RDB: loads · Snapshot reader reconstructs state from the RDB format
- Server → Replication: coordinates · The server integrates replication with request processing