# blackmyth-dev/mini-redis-cpp architecture

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

- Interactive diagram: https://gitdiagram.com/blackmyth-dev/mini-redis-cpp
- Repository: https://github.com/blackmyth-dev/mini-redis-cpp

## Overview

1. **Overall shape**
   - This repo builds a single C++ executable, `edge_gateway`, that exposes **two network interfaces over one shared in-memory state**:
     - a **Redis-like TCP server** for key/value commands
     - an **HTTP management API** for the same data
   - The architecture is intentionally layered: transport/parsing on the edges, shared storage in the center.

2. **Entry point and composition**
   - `apps/edge_gateway_main.cpp` is the application assembly point.
   - It wires together the servers, the shared key-value store, the thread pool, and snapshot persistence.
   - This is the boundary where runtime configuration enters the system: ports, database file, thread count.

3. **Core state subsystem**
   - `include/gateway/core/key_value_store.hpp` and `src/core/key_value_store.cpp` define the main shared state.
   - This store is the authoritative source for values and TTL-related metadata.
   - Both the Redis-like server and the HTTP API operate on this same store, so the main architectural boundary is between **protocol handling** and **state mutation/querying**.

4. **Concurrency model**
   - `include/gateway/core/thread_pool.hpp` provides a shared worker pool used to process incoming requests concurrently.
   - This avoids each connection/request needing its own dedicated thread and makes the concurrency story centralized.
   - For an architecture diagram, show the thread pool as a shared execution resource behind both servers.

5. **Redis-like TCP subsystem**
   - `include/gateway/redis/tcp_server.hpp` and `src/redis/tcp_server.cpp` implement the TCP listener and connection handling.
   - `include/gateway/redis/command_parser.hpp` and `src/redis/command_parser.cpp` translate text commands into internal operations.
   - Supported commands in the README include `SET`, `GET`, `EXPIRE`, `TTL`, `SAVE`, and `QUIT`.
   - Responses follow a Redis-style text protocol, so this server forms a clean external protocol boundary.

6. **HTTP subsystem**
   - `include/gateway/http/http_server.hpp` and `src/http/http_server.cpp` expose the management API.
   - `include/gateway/http/http_parser.hpp` and `src/http/http_parser.cpp` handle HTTP request parsing.
   - The documented endpoints are `/kv/<key>` for GET/PUT/DELETE and `/health` for liveness.
   - This server is a second protocol adapter over the same core store, not a separate data model.

7. **Persistence and snapshotting**
   - `include/gateway/storage/snapshot_store.hpp` and `src/storage/snapshot_store.cpp` handle saving state to disk.
   - The README indicates a `SAVE` command and a `--db gateway.db` runtime option, so persistence is snapshot-based rather than a full database engine.
   - Diagram this as a storage boundary off the core store, likely used for load/save or checkpointing.

8. **Data flow**
   - Client request arrives on TCP or HTTP.
   - The relevant parser converts raw bytes into an internal command or request object.
   - The server dispatches that work through the thread pool.
   - The shared key-value store performs the operation.
   - If requested, snapshot storage serializes state to the database file.
   - This makes the system’s central flow: **network input → parse → dispatch → shared store → optional snapshot**.

9. **Operational behavior**
   - The README emphasizes **thread-safe access**, **TTL**, **snapshot**, and **graceful shutdown**.
   - Those are important architecture concerns because they affect how the servers stop, how expired keys are handled, and how state remains consistent across concurrent requests.
   - For diagramming, treat graceful shutdown as a cross-cutting control path touching both servers and the worker pool.

10. **Testing boundary**
    - `tests/gateway_tests.cpp` validates the integrated behavior of the gateway.
    - Since the repo is small and centered on one executable, tests likely cover the protocol handlers and core storage behavior together rather than isolated microservices.
    - That makes the tests a good place to verify the contract between parsers, store, and persistence.

11. **Technology footprint**
    - The project is standard C++ built with CMake.
    - There are no visible external runtime dependencies in the tree; the architecture is mostly custom C++ networking/parsing/storage code.
    - The only externally visible interfaces today are TCP on one port and HTTP on another.

12. **What is not yet in this tree**
    - The README mentions Device/SOME-IP and MQTT in the conceptual diagram, but the repository tree shown here does not include those subsystems.
    - So the concrete architecture to diagram for this repo is the **Redis-like TCP + HTTP management gateway backed by a shared in-memory store and snapshot persistence**.

Main entry points: [`apps/edge_gateway_main.cpp`](https://github.com/blackmyth-dev/mini-redis-cpp/blob/main/apps/edge_gateway_main.cpp) (Gateway main), [`include/gateway/redis/tcp_server.hpp`](https://github.com/blackmyth-dev/mini-redis-cpp/blob/main/include/gateway/redis/tcp_server.hpp) (TCP server), [`include/gateway/http/http_server.hpp`](https://github.com/blackmyth-dev/mini-redis-cpp/blob/main/include/gateway/http/http_server.hpp) (HTTP server), [`include/gateway/core/key_value_store.hpp`](https://github.com/blackmyth-dev/mini-redis-cpp/blob/main/include/gateway/core/key_value_store.hpp) (KV store)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime"]
  node_apps_edge_gateway_main["Gateway main<br/>entrypoint"]
end

subgraph group_core["Core state"]
  node_core_key_value_store[("KV store<br/>state")]
  node_core_thread_pool["Thread pool<br/>concurrency<br/>[thread_pool.hpp]"]
  node_src_core_key_value_store["KV impl<br/>state impl"]
end

subgraph group_redis["Redis-like TCP"]
  node_redis_tcp_server["TCP server<br/>network server<br/>[tcp_server.hpp]"]
  node_redis_command_parser["Command parser<br/>protocol parser<br/>[command_parser.hpp]"]
  node_src_redis_tcp_server["TCP impl<br/>server impl<br/>[tcp_server.cpp]"]
  node_src_redis_command_parser["Command impl<br/>parser impl<br/>[command_parser.cpp]"]
end

subgraph group_http["HTTP API"]
  node_http_http_server["HTTP server<br/>network server<br/>[http_server.hpp]"]
  node_http_http_parser["HTTP parser<br/>protocol parser<br/>[http_parser.hpp]"]
  node_src_http_http_server["HTTP impl<br/>server impl<br/>[http_server.cpp]"]
  node_src_http_http_parser["HTTP impl<br/>parser impl<br/>[http_parser.cpp]"]
end

subgraph group_storage["Persistence"]
  node_storage_snapshot_store[("Snapshot store<br/>persistence<br/>[snapshot_store.hpp]")]
  node_src_storage_snapshot_store["Snapshot impl<br/>persistence impl<br/>[snapshot_store.cpp]"]
end

node_tests_gateway_tests["Gateway tests<br/>integration tests<br/>[gateway_tests.cpp]"]

node_apps_edge_gateway_main -->|"starts"| node_redis_tcp_server
node_apps_edge_gateway_main -->|"starts"| node_http_http_server
node_apps_edge_gateway_main -->|"creates"| node_core_key_value_store
node_apps_edge_gateway_main -->|"creates"| node_core_thread_pool
node_apps_edge_gateway_main -->|"configures"| node_storage_snapshot_store
node_redis_tcp_server -->|"uses"| node_redis_command_parser
node_redis_tcp_server -->|"dispatches"| node_core_thread_pool
node_redis_tcp_server -->|"mutates/query"| node_core_key_value_store
node_http_http_server -->|"uses"| node_http_http_parser
node_http_http_server -->|"dispatches"| node_core_thread_pool
node_http_http_server -->|"mutates/query"| node_core_key_value_store
node_core_key_value_store -->|"snapshots"| node_storage_snapshot_store
node_src_core_key_value_store -->|"implements"| node_core_key_value_store
node_src_redis_tcp_server -->|"implements"| node_redis_tcp_server
node_src_redis_command_parser -->|"implements"| node_redis_command_parser
node_src_http_http_server -->|"implements"| node_http_http_server
node_src_http_http_parser -->|"implements"| node_http_http_parser
node_src_storage_snapshot_store -->|"implements"| node_storage_snapshot_store
node_tests_gateway_tests -.->|"covers"| node_apps_edge_gateway_main
node_tests_gateway_tests -.->|"covers"| node_core_key_value_store
node_tests_gateway_tests -.->|"covers"| node_redis_tcp_server
node_tests_gateway_tests -.->|"covers"| node_http_http_server
node_tests_gateway_tests -.->|"covers"| node_storage_snapshot_store

click node_apps_edge_gateway_main "https://github.com/blackmyth-dev/mini-redis-cpp/blob/main/apps/edge_gateway_main.cpp"
click node_core_key_value_store "https://github.com/blackmyth-dev/mini-redis-cpp/blob/main/include/gateway/core/key_value_store.hpp"
click node_core_thread_pool "https://github.com/blackmyth-dev/mini-redis-cpp/blob/main/include/gateway/core/thread_pool.hpp"
click node_redis_tcp_server "https://github.com/blackmyth-dev/mini-redis-cpp/blob/main/include/gateway/redis/tcp_server.hpp"
click node_redis_command_parser "https://github.com/blackmyth-dev/mini-redis-cpp/blob/main/include/gateway/redis/command_parser.hpp"
click node_http_http_server "https://github.com/blackmyth-dev/mini-redis-cpp/blob/main/include/gateway/http/http_server.hpp"
click node_http_http_parser "https://github.com/blackmyth-dev/mini-redis-cpp/blob/main/include/gateway/http/http_parser.hpp"
click node_storage_snapshot_store "https://github.com/blackmyth-dev/mini-redis-cpp/blob/main/include/gateway/storage/snapshot_store.hpp"
click node_tests_gateway_tests "https://github.com/blackmyth-dev/mini-redis-cpp/blob/main/tests/gateway_tests.cpp"
click node_src_core_key_value_store "https://github.com/blackmyth-dev/mini-redis-cpp/blob/main/src/core/key_value_store.cpp"
click node_src_redis_tcp_server "https://github.com/blackmyth-dev/mini-redis-cpp/blob/main/src/redis/tcp_server.cpp"
click node_src_redis_command_parser "https://github.com/blackmyth-dev/mini-redis-cpp/blob/main/src/redis/command_parser.cpp"
click node_src_http_http_server "https://github.com/blackmyth-dev/mini-redis-cpp/blob/main/src/http/http_server.cpp"
click node_src_http_http_parser "https://github.com/blackmyth-dev/mini-redis-cpp/blob/main/src/http/http_parser.cpp"
click node_src_storage_snapshot_store "https://github.com/blackmyth-dev/mini-redis-cpp/blob/main/src/storage/snapshot_store.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_apps_edge_gateway_main toneBlue
class node_core_key_value_store,node_core_thread_pool,node_src_core_key_value_store toneAmber
class node_redis_tcp_server,node_redis_command_parser,node_src_redis_tcp_server,node_src_redis_command_parser toneMint
class node_http_http_server,node_http_http_parser,node_src_http_http_server,node_src_http_http_parser toneRose
class node_storage_snapshot_store,node_src_storage_snapshot_store toneIndigo
class node_tests_gateway_tests toneNeutral
```

## Components

### Runtime

Executable composition and shared execution resources

- [Gateway main](https://github.com/blackmyth-dev/mini-redis-cpp/blob/main/apps/edge_gateway_main.cpp): entrypoint · Assembles servers, shared store, thread pool, and snapshot persistence. · `apps/edge_gateway_main.cpp`

### Core state

Shared in-memory data and concurrency primitives

- [KV store](https://github.com/blackmyth-dev/mini-redis-cpp/blob/main/include/gateway/core/key_value_store.hpp): state · Authoritative in-memory key/value store with TTL metadata. · `include/gateway/core/key_value_store.hpp`
- [Thread pool](https://github.com/blackmyth-dev/mini-redis-cpp/blob/main/include/gateway/core/thread_pool.hpp): concurrency · Shared worker pool used to process incoming requests concurrently. · `include/gateway/core/thread_pool.hpp`
- [KV impl](https://github.com/blackmyth-dev/mini-redis-cpp/blob/main/src/core/key_value_store.cpp): state impl · Implements the shared in-memory key/value and TTL logic. · `src/core/key_value_store.cpp`

### Redis-like TCP

Text protocol gateway for key/value commands

- [TCP server](https://github.com/blackmyth-dev/mini-redis-cpp/blob/main/include/gateway/redis/tcp_server.hpp): network server · Redis-like listener and connection handler. · `include/gateway/redis/tcp_server.hpp`
- [Command parser](https://github.com/blackmyth-dev/mini-redis-cpp/blob/main/include/gateway/redis/command_parser.hpp): protocol parser · Parses Redis-style text commands into internal operations. · `include/gateway/redis/command_parser.hpp`
- [TCP impl](https://github.com/blackmyth-dev/mini-redis-cpp/blob/main/src/redis/tcp_server.cpp): server impl · Implements TCP accept loop, connection handling, and dispatch. · `src/redis/tcp_server.cpp`
- [Command impl](https://github.com/blackmyth-dev/mini-redis-cpp/blob/main/src/redis/command_parser.cpp): parser impl · Implements command parsing for the Redis-like protocol. · `src/redis/command_parser.cpp`

### HTTP API

Management API gateway over the same store

- [HTTP server](https://github.com/blackmyth-dev/mini-redis-cpp/blob/main/include/gateway/http/http_server.hpp): network server · HTTP management API server for key/value operations and health checks. · `include/gateway/http/http_server.hpp`
- [HTTP parser](https://github.com/blackmyth-dev/mini-redis-cpp/blob/main/include/gateway/http/http_parser.hpp): protocol parser · Parses HTTP requests into internal request objects. · `include/gateway/http/http_parser.hpp`
- [HTTP impl](https://github.com/blackmyth-dev/mini-redis-cpp/blob/main/src/http/http_server.cpp): server impl · Implements the HTTP management endpoints. · `src/http/http_server.cpp`
- [HTTP impl](https://github.com/blackmyth-dev/mini-redis-cpp/blob/main/src/http/http_parser.cpp): parser impl · Implements HTTP request parsing. · `src/http/http_parser.cpp`

### Persistence

Snapshot-based disk state

- [Snapshot store](https://github.com/blackmyth-dev/mini-redis-cpp/blob/main/include/gateway/storage/snapshot_store.hpp): persistence · Saves and restores state from the database snapshot file. · `include/gateway/storage/snapshot_store.hpp`
- [Snapshot impl](https://github.com/blackmyth-dev/mini-redis-cpp/blob/main/src/storage/snapshot_store.cpp): persistence impl · Implements snapshot serialization and file access. · `src/storage/snapshot_store.cpp`

### Other components

- [Gateway tests](https://github.com/blackmyth-dev/mini-redis-cpp/blob/main/tests/gateway_tests.cpp): integration tests · Validates gateway behavior across protocol handling, state, and persistence. · `tests/gateway_tests.cpp`

## Connections

- Gateway main → TCP server: starts · Main wires up the Redis-like TCP service.
- Gateway main → HTTP server: starts · Main wires up the HTTP management service.
- Gateway main → KV store: creates · Main creates the shared in-memory store.
- Gateway main → Thread pool: creates · Main creates the shared worker pool.
- Gateway main → Snapshot store: configures · Main passes snapshot persistence settings and database path.
- TCP server → Command parser: uses · TCP server delegates protocol decoding to the command parser.
- TCP server → Thread pool: dispatches · Requests are processed on the shared worker pool.
- TCP server → KV store: mutates/query · Command execution reads and writes the shared store.
- HTTP server → HTTP parser: uses · HTTP server delegates request decoding to the parser.
- HTTP server → Thread pool: dispatches · HTTP work is executed via the shared worker pool.
- HTTP server → KV store: mutates/query · HTTP endpoints operate on the same shared store.
- KV store → Snapshot store: snapshots · Store state can be persisted through snapshot storage.
- KV impl → KV store: implements · Source file provides the store implementation.
- TCP impl → TCP server: implements · Source file provides the TCP server implementation.
- Command impl → Command parser: implements · Source file provides the command parser implementation.
- HTTP impl → HTTP server: implements · Source file provides the HTTP server implementation.
- HTTP impl → HTTP parser: implements · Source file provides the HTTP parser implementation.
- Snapshot impl → Snapshot store: implements · Source file provides the snapshot storage implementation.
- Gateway tests → Gateway main: covers · Integration tests exercise the assembled gateway.
- Gateway tests → KV store: covers · Tests validate shared state behavior.
- Gateway tests → TCP server: covers · Tests validate the Redis-like protocol path.
- Gateway tests → HTTP server: covers · Tests validate the HTTP management path.
- Gateway tests → Snapshot store: covers · Tests validate snapshot persistence behavior.
