How blackmyth-dev/mini-redis-cpp works
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: edge_gateway_main.cpp Gateway main · tcp_server.hpp TCP server · http_server.hpp HTTP server · key_value_store.hpp KV store
Components 15
Runtime
Executable composition and shared execution resources
- Gateway main · 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 · Authoritative in-memory key/value store with TTL metadata.
include/gateway/core/key_value_store.hpp - Thread pool · Shared worker pool used to process incoming requests concurrently.
include/gateway/core/thread_pool.hpp - KV 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 · Redis-like listener and connection handler.
include/gateway/redis/tcp_server.hpp - Command parser · Parses Redis-style text commands into internal operations.
include/gateway/redis/command_parser.hpp - TCP impl · Implements TCP accept loop, connection handling, and dispatch.
src/redis/tcp_server.cpp - Command 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 · HTTP management API server for key/value operations and health checks.
include/gateway/http/http_server.hpp - HTTP parser · Parses HTTP requests into internal request objects.
include/gateway/http/http_parser.hpp - HTTP impl · Implements the HTTP management endpoints.
src/http/http_server.cpp - HTTP impl · Implements HTTP request parsing.
src/http/http_parser.cpp
Persistence
Snapshot-based disk state
- Snapshot store · Saves and restores state from the database snapshot file.
include/gateway/storage/snapshot_store.hpp - Snapshot impl · Implements snapshot serialization and file access.
src/storage/snapshot_store.cpp
Other components
- Gateway tests · Validates gateway behavior across protocol handling, state, and persistence.
tests/gateway_tests.cpp
Connections 23
- 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.