# insane-22/mini-redis architecture

> Architecture diagram of the GitHub repository insane-22/mini-redis, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/insane-22/mini-redis
- Repository: https://github.com/insane-22/mini-redis

## Overview

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: [`src/Handler.cpp`](https://github.com/insane-22/mini-redis/blob/master/src/Handler.cpp) (Handler), [`src/GeoHandler.cpp`](https://github.com/insane-22/mini-redis/blob/master/src/GeoHandler.cpp) (Geo), [`src/ReplicationManager.cpp`](https://github.com/insane-22/mini-redis/blob/master/src/ReplicationManager.cpp) (Replication), [`src/Server.cpp`](https://github.com/insane-22/mini-redis/blob/master/src/Server.cpp) (Server)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_g1["Ingress"]
  node_n1["Server<br/>[Server.cpp]"]
  node_n2["Parser<br/>[Parser.cpp]"]
end

subgraph group_g2["Dispatch"]
  node_n3["Handler<br/>dispatcher<br/>[Handler.cpp]"]
end

subgraph group_g3["Data Stores"]
  node_n4["Kv Store<br/>string store<br/>[KvStoreHandler.cpp]"]
  node_n5["List Store"]
  node_n6["Stream Store"]
  node_n7["Sorted Sets<br/>zset store"]
  node_n8["Geo<br/>[GeoHandler.cpp]"]
  node_n9["Geo Encoding<br/>encoding helper<br/>[GeoEncoding.cpp]"]
  node_n10["Pub/Sub<br/>messaging<br/>[PubSubHandler.cpp]"]
end

subgraph group_g4["Distributed"]
  node_n11["Replication<br/>sync manager"]
  node_n12["Replica Client<br/>replica peer<br/>[ReplicaClient.cpp]"]
end

subgraph group_g5["Persistence"]
  node_n13["RDB<br/>snapshot format<br/>[Rdb.cpp]"]
  node_n14["RDB Reader<br/>snapshot loader<br/>[RdbReader.cpp]"]
end

node_n1 -->|"accepts"| node_n2
node_n2 -->|"parses"| node_n3
node_n3 -->|"routes"| node_n4
node_n3 -->|"routes"| node_n5
node_n3 -->|"routes"| node_n6
node_n3 -->|"routes"| node_n7
node_n3 -->|"routes"| node_n8
node_n8 -->|"encodes"| node_n9
node_n8 -.->|"builds on"| node_n7
node_n3 -->|"routes"| node_n10
node_n3 -->|"propagates"| node_n11
node_n11 -->|"connects to"| node_n12
node_n3 -->|"persists"| node_n13
node_n14 -->|"loads"| node_n13
node_n1 -.->|"coordinates"| node_n11

click node_n1 "https://github.com/insane-22/mini-redis/blob/master/src/Server.cpp"
click node_n2 "https://github.com/insane-22/mini-redis/blob/master/src/Parser.cpp"
click node_n3 "https://github.com/insane-22/mini-redis/blob/master/src/Handler.cpp"
click node_n4 "https://github.com/insane-22/mini-redis/blob/master/src/KvStoreHandler.cpp"
click node_n5 "https://github.com/insane-22/mini-redis/blob/master/src/ListStoreHandler.cpp"
click node_n6 "https://github.com/insane-22/mini-redis/blob/master/src/StreamStoreHandler.cpp"
click node_n7 "https://github.com/insane-22/mini-redis/blob/master/src/SortedSetHandler.cpp"
click node_n8 "https://github.com/insane-22/mini-redis/blob/master/src/GeoHandler.cpp"
click node_n9 "https://github.com/insane-22/mini-redis/blob/master/src/GeoEncoding.cpp"
click node_n10 "https://github.com/insane-22/mini-redis/blob/master/src/PubSubHandler.cpp"
click node_n11 "https://github.com/insane-22/mini-redis/blob/master/src/ReplicationManager.cpp"
click node_n12 "https://github.com/insane-22/mini-redis/blob/master/src/ReplicaClient.cpp"
click node_n13 "https://github.com/insane-22/mini-redis/blob/master/src/Rdb.cpp"
click node_n14 "https://github.com/insane-22/mini-redis/blob/master/src/RdbReader.cpp"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_n1,node_n2 toneBlue
class node_n3 toneAmber
class node_n4,node_n5,node_n6,node_n7,node_n8,node_n9,node_n10 toneMint
class node_n11,node_n12 toneRose
class node_n13,node_n14 toneIndigo
```

## Components

### Ingress

Client connection handling and protocol parsing

- [Server](https://github.com/insane-22/mini-redis/blob/master/src/Server.cpp): native server · Accepts client connections and drives request processing · `src/Server.cpp`
- [Parser](https://github.com/insane-22/mini-redis/blob/master/src/Parser.cpp): RESP parser · Converts raw Redis protocol input into structured commands · `src/Parser.cpp`

### Dispatch

Command routing and cross-cutting execution control

- [Handler](https://github.com/insane-22/mini-redis/blob/master/src/Handler.cpp): dispatcher · 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](https://github.com/insane-22/mini-redis/blob/master/src/KvStoreHandler.cpp): string store · Implements core key-value commands and expiration behavior · `src/KvStoreHandler.cpp`
- [List Store](https://github.com/insane-22/mini-redis/blob/master/src/ListStoreHandler.cpp): list store · Implements Redis list operations · `src/ListStoreHandler.cpp`
- [Stream Store](https://github.com/insane-22/mini-redis/blob/master/src/StreamStoreHandler.cpp): stream store · Implements stream append and read commands · `src/StreamStoreHandler.cpp`
- [Sorted Sets](https://github.com/insane-22/mini-redis/blob/master/src/SortedSetHandler.cpp): zset store · Implements sorted-set operations used by other features · `src/SortedSetHandler.cpp`
- [Geo](https://github.com/insane-22/mini-redis/blob/master/src/GeoHandler.cpp): geo feature · Implements geo commands on top of sorted-set encoding · `src/GeoHandler.cpp`
- [Geo Encoding](https://github.com/insane-22/mini-redis/blob/master/src/GeoEncoding.cpp): encoding helper · Encodes coordinates into sortable values for geo lookups · `src/GeoEncoding.cpp`
- [Pub/Sub](https://github.com/insane-22/mini-redis/blob/master/src/PubSubHandler.cpp): messaging · Manages channel subscriptions and live message fanout · `src/PubSubHandler.cpp`

### Distributed

Replication and peer synchronization

- [Replication](https://github.com/insane-22/mini-redis/blob/master/src/ReplicationManager.cpp): sync manager · Coordinates master-replica synchronization and propagation · `src/ReplicationManager.cpp`
- [Replica Client](https://github.com/insane-22/mini-redis/blob/master/src/ReplicaClient.cpp): replica peer · Outbound client used to talk to replica nodes · `src/ReplicaClient.cpp`

### Persistence

RDB snapshot creation and loading

- [RDB](https://github.com/insane-22/mini-redis/blob/master/src/Rdb.cpp): snapshot format · Manages Redis database snapshot persistence · `src/Rdb.cpp`
- [RDB Reader](https://github.com/insane-22/mini-redis/blob/master/src/RdbReader.cpp): snapshot loader · Reconstructs in-memory state from RDB snapshots · `src/RdbReader.cpp`

## Connections

- 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
