# jsparrow08/redis-cpp architecture

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

- Interactive diagram: https://gitdiagram.com/jsparrow08/redis-cpp
- Repository: https://github.com/jsparrow08/redis-cpp

## Overview

1. **Overall shape**
   - This is a C++ Redis-like server, organized as a small set of runtime subsystems: network server, RESP protocol handling, command dispatch, in-memory data store, configuration, and replication support.

2. **Entry point and application wiring**
   - `src/main.cpp` is the top-level bootstrap.
   - It likely constructs config, starts the server loop, and connects the command/replication pieces together.
   - `CMakeLists.txt` plus `vcpkg.json`/`vcpkg-configuration.json` show a C++ build managed with CMake and external packages via vcpkg.

3. **Network/server boundary**
   - `src/server/server.cpp` and `src/server/connection.hpp` define the transport boundary.
   - This layer owns sockets/connections and is responsible for receiving raw bytes from clients and sending responses back.
   - It is the point where protocol parsing begins, but it should stay unaware of Redis command semantics.

4. **RESP protocol layer**
   - `src/resp/resp.cpp` and `src/resp/resp.hpp` implement Redis Serialization Protocol handling.
   - This is the translation layer between raw network data and structured request/response objects.
   - Architecture-wise, it sits between the server transport and the command handlers.

5. **Command dispatch pipeline**
   - `src/command/command_handler.cpp` is the central dispatcher.
   - `src/command/command_utils.*` likely provides shared parsing/validation helpers.
   - The dispatcher routes parsed commands into specialized handlers rather than letting the server layer implement command logic directly.

6. **Command domains**
   - The commands are split by responsibility:
     - `string_commands.*` for key/value string operations.
     - `server_commands.*` for server/admin-style commands.
     - `replication_commands.*` for replication-related commands.
   - This separation keeps protocol handling and command semantics modular.

7. **In-memory data store**
   - `src/rdstore/rdstore.cpp` and `src/rdstore/rdstore.hpp` form the storage core.
   - This is the main stateful subsystem where key/value data is kept and queried by the command layer.
   - Commands should treat this as the authoritative application state for non-replication operations.

8. **Configuration**
   - `src/config/config.*` and `src/config/replication_config.*` define runtime settings.
   - The presence of a dedicated replication config suggests the server can run in different roles or modes, with replication-specific parameters separated from general server config.
   - This configuration layer feeds both server startup and replication behavior.

9. **Replication subsystem**
   - `src/replication/replication_manager.*` is the dedicated replication coordinator.
   - It likely tracks replication state, peers, and synchronization responsibilities.
   - Replication is not just another command category; it appears to be a cross-cutting subsystem with its own state and control path, exposed through replication commands and config.

10. **Dependency flow to diagram**
   - The most important data flow is: **network bytes → RESP parsing → command dispatch → rdstore / replication manager → RESP serialization → network bytes**.
   - A good architecture diagram should show the server layer as the edge-facing boundary, the command layer as the application logic hub, and rdstore/replication as the two major stateful backends.

11. **Operational/tooling context**
   - `.codecrafters/*` and `codecrafters.yml` indicate this is built for a Codecrafters-style environment, which usually means the runtime behavior is validated through protocol-level tests and scripted execution.
   - `your_program.sh` and the compile/run scripts are wrapper tooling around the C++ binary, not core application architecture.

12. **What to emphasize in the diagram**
   - Draw clear boundaries around:
     - transport/server handling,
     - protocol encoding/decoding,
     - command routing,
     - storage state,
     - replication state/control.
   - The key architectural idea is a layered Redis-compatible server, with replication handled as a first-class subsystem rather than embedded ad hoc into string commands.

Main entry points: [`src/command/command_handler.cpp`](https://github.com/jsparrow08/redis-cpp/blob/main/src/command/command_handler.cpp) (Dispatcher), [`src/resp/resp.cpp`](https://github.com/jsparrow08/redis-cpp/blob/main/src/resp/resp.cpp) (RESP), [`src/rdstore/rdstore.cpp`](https://github.com/jsparrow08/redis-cpp/blob/main/src/rdstore/rdstore.cpp) (RD store), [`src/main.cpp`](https://github.com/jsparrow08/redis-cpp/blob/main/src/main.cpp) (Main)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_grp_runtime["Runtime"]
  node_n_main["Main<br/>bootstrap<br/>[main.cpp]"]
  node_n_server["Server<br/>[server.cpp]"]
  node_n_connection["Connection<br/>socket boundary<br/>[connection.hpp]"]
  node_n_resp["RESP<br/>protocol codec<br/>[resp.cpp]"]
end

subgraph group_grp_commands["Commands"]
  node_n_dispatch["Dispatcher<br/>command router"]
  node_n_utils["Cmd utils<br/>parsing helpers<br/>[command_utils.cpp]"]
  node_n_string_cmds["String cmds<br/>kv commands"]
  node_n_server_cmds["Server cmds<br/>admin commands"]
  node_n_repl_cmds["Repl cmds<br/>replication commands"]
end

subgraph group_grp_state["State"]
  node_n_store[("RD store<br/>data store<br/>[rdstore.cpp]")]
  node_n_repl_mgr["Repl mgr<br/>replication core"]
end

subgraph group_grp_config["Config"]
  node_n_config["Config<br/>[config.cpp]"]
  node_n_repl_config["Repl config<br/>replication config"]
end

subgraph group_grp_tooling["Tooling"]
  node_n_build["Build<br/>cmake/vcpkg<br/>[CMakeLists.txt]"]
  node_n_vcpkg["Deps<br/>vcpkg config<br/>[vcpkg.json]"]
  node_n_codecrafters["Runner<br/>scripts<br/>[run.sh]"]
end

node_n_build -->|"builds"| node_n_main
node_n_vcpkg -->|"feeds"| node_n_build
node_n_codecrafters -.->|"wraps"| node_n_build
node_n_main -->|"loads"| node_n_config
node_n_main -->|"loads"| node_n_repl_config
node_n_main -->|"starts"| node_n_server
node_n_server -->|"accepts"| node_n_connection
node_n_connection -->|"bytes to msgs"| node_n_resp
node_n_resp -->|"requests"| node_n_dispatch
node_n_dispatch -->|"validates"| node_n_utils
node_n_dispatch -->|"routes"| node_n_string_cmds
node_n_dispatch -->|"routes"| node_n_server_cmds
node_n_dispatch -->|"routes"| node_n_repl_cmds
node_n_string_cmds -->|"mutates/reads"| node_n_store
node_n_server_cmds -->|"queries"| node_n_store
node_n_repl_cmds -->|"controls"| node_n_repl_mgr
node_n_repl_cmds -->|"syncs"| node_n_store
node_n_repl_mgr -.->|"replicates"| node_n_store
node_n_store -->|"responses"| node_n_resp
node_n_repl_mgr -->|"status"| node_n_resp
node_n_resp -->|"replies"| node_n_connection

click node_n_main "https://github.com/jsparrow08/redis-cpp/blob/main/src/main.cpp"
click node_n_server "https://github.com/jsparrow08/redis-cpp/blob/main/src/server/server.cpp"
click node_n_connection "https://github.com/jsparrow08/redis-cpp/blob/main/src/server/connection.hpp"
click node_n_resp "https://github.com/jsparrow08/redis-cpp/blob/main/src/resp/resp.cpp"
click node_n_dispatch "https://github.com/jsparrow08/redis-cpp/blob/main/src/command/command_handler.cpp"
click node_n_utils "https://github.com/jsparrow08/redis-cpp/blob/main/src/command/command_utils.cpp"
click node_n_string_cmds "https://github.com/jsparrow08/redis-cpp/blob/main/src/command/string_commands.cpp"
click node_n_server_cmds "https://github.com/jsparrow08/redis-cpp/blob/main/src/command/server_commands.cpp"
click node_n_repl_cmds "https://github.com/jsparrow08/redis-cpp/blob/main/src/command/replication_commands.cpp"
click node_n_store "https://github.com/jsparrow08/redis-cpp/blob/main/src/rdstore/rdstore.cpp"
click node_n_repl_mgr "https://github.com/jsparrow08/redis-cpp/blob/main/src/replication/replication_manager.cpp"
click node_n_config "https://github.com/jsparrow08/redis-cpp/blob/main/src/config/config.cpp"
click node_n_repl_config "https://github.com/jsparrow08/redis-cpp/blob/main/src/config/replication_config.cpp"
click node_n_build "https://github.com/jsparrow08/redis-cpp/blob/main/CMakeLists.txt"
click node_n_vcpkg "https://github.com/jsparrow08/redis-cpp/blob/main/vcpkg.json"
click node_n_codecrafters "https://github.com/jsparrow08/redis-cpp/blob/main/.codecrafters/run.sh"

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_n_main,node_n_server,node_n_connection,node_n_resp toneBlue
class node_n_dispatch,node_n_utils,node_n_string_cmds,node_n_server_cmds,node_n_repl_cmds toneAmber
class node_n_store,node_n_repl_mgr toneMint
class node_n_config,node_n_repl_config toneRose
class node_n_build,node_n_vcpkg,node_n_codecrafters toneIndigo
```

## Components

### Runtime

Top-level process, transport, and protocol handling.

- [Main](https://github.com/jsparrow08/redis-cpp/blob/main/src/main.cpp): bootstrap · Process entry point and application wiring. · `src/main.cpp`
- [Server](https://github.com/jsparrow08/redis-cpp/blob/main/src/server/server.cpp): transport server · Network boundary that accepts clients and moves raw bytes. · `src/server/server.cpp`
- [Connection](https://github.com/jsparrow08/redis-cpp/blob/main/src/server/connection.hpp): socket boundary · Connection abstraction for client I/O. · `src/server/connection.hpp`
- [RESP](https://github.com/jsparrow08/redis-cpp/blob/main/src/resp/resp.cpp): protocol codec · RESP parsing and serialization between bytes and structured messages. · `src/resp/resp.cpp`

### Commands

Dispatch and command-domain handlers.

- [Dispatcher](https://github.com/jsparrow08/redis-cpp/blob/main/src/command/command_handler.cpp): command router · Central command routing and execution entry point. · `src/command/command_handler.cpp`
- [Cmd utils](https://github.com/jsparrow08/redis-cpp/blob/main/src/command/command_utils.cpp): parsing helpers · Shared command validation and parsing helpers. · `src/command/command_utils.cpp`
- [String cmds](https://github.com/jsparrow08/redis-cpp/blob/main/src/command/string_commands.cpp): kv commands · String and key-value command implementations. · `src/command/string_commands.cpp`
- [Server cmds](https://github.com/jsparrow08/redis-cpp/blob/main/src/command/server_commands.cpp): admin commands · Server and admin-oriented commands. · `src/command/server_commands.cpp`
- [Repl cmds](https://github.com/jsparrow08/redis-cpp/blob/main/src/command/replication_commands.cpp): replication commands · Commands that expose replication control and state. · `src/command/replication_commands.cpp`

### State

Authoritative in-memory data and replication state.

- [RD store](https://github.com/jsparrow08/redis-cpp/blob/main/src/rdstore/rdstore.cpp): data store · Primary in-memory key/value state. · `src/rdstore/rdstore.cpp`
- [Repl mgr](https://github.com/jsparrow08/redis-cpp/blob/main/src/replication/replication_manager.cpp): replication core · Replication coordinator and synchronization state. · `src/replication/replication_manager.cpp`

### Config

Runtime and replication configuration.

- [Config](https://github.com/jsparrow08/redis-cpp/blob/main/src/config/config.cpp): runtime config · General server configuration. · `src/config/config.cpp`
- [Repl config](https://github.com/jsparrow08/redis-cpp/blob/main/src/config/replication_config.cpp): replication config · Replication-specific runtime settings. · `src/config/replication_config.cpp`

### Tooling

Build and execution wrappers around the binary.

- [Build](https://github.com/jsparrow08/redis-cpp/blob/main/CMakeLists.txt): cmake/vcpkg · Build and dependency management. · `CMakeLists.txt`
- [Deps](https://github.com/jsparrow08/redis-cpp/blob/main/vcpkg.json): vcpkg config · External package configuration. · `vcpkg.json`
- [Runner](https://github.com/jsparrow08/redis-cpp/blob/main/.codecrafters/run.sh): scripts · Codecrafters compile/run wrappers. · `.codecrafters/run.sh`

## Connections

- Build → Main: builds · CMake builds the server binary from the application sources.
- Deps → Build: feeds · Dependency configuration is consumed by the build.
- Runner → Build: wraps · Scripted environment compiles and runs the program.
- Main → Config: loads · Bootstrap initializes general runtime settings.
- Main → Repl config: loads · Bootstrap initializes replication settings.
- Main → Server: starts · Main wires up and starts the network server.
- Server → Connection: accepts · Server owns connection lifecycle and I/O boundaries.
- Connection → RESP: bytes to msgs · Raw client data enters RESP decoding.
- RESP → Dispatcher: requests · Parsed protocol messages are handed to the dispatcher.
- Dispatcher → Cmd utils: validates · Dispatcher uses shared helpers for parsing and checks.
- Dispatcher → String cmds: routes · String-related commands are dispatched to their handler set.
- Dispatcher → Server cmds: routes · Server/admin commands are dispatched separately.
- Dispatcher → Repl cmds: routes · Replication commands are dispatched to replication handlers.
- String cmds → RD store: mutates/reads · String commands operate on the authoritative key/value store.
- Server cmds → RD store: queries · Server commands may inspect shared application state.
- Repl cmds → Repl mgr: controls · Replication commands manipulate replication state and peers.
- Repl cmds → RD store: syncs · Replication commands may read or apply store state.
- Repl mgr → RD store: replicates · Replication coordination is coupled to the data store state.
- RD store → RESP: responses · Command results are serialized back into RESP.
- Repl mgr → RESP: status · Replication status and control responses are encoded for clients.
- RESP → Connection: replies · Encoded responses are written back over the connection.
