How jsparrow08/redis-cpp works
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: command_handler.cpp Dispatcher · resp.cpp RESP · rdstore.cpp RD store · main.cpp Main
Components 16
Runtime
Top-level process, transport, and protocol handling.
- Main · Process entry point and application wiring.
src/main.cpp - Server · Network boundary that accepts clients and moves raw bytes.
src/server/server.cpp - Connection · Connection abstraction for client I/O.
src/server/connection.hpp - RESP · RESP parsing and serialization between bytes and structured messages.
src/resp/resp.cpp
Commands
Dispatch and command-domain handlers.
- Dispatcher · Central command routing and execution entry point.
src/command/command_handler.cpp - Cmd utils · Shared command validation and parsing helpers.
src/command/command_utils.cpp - String cmds · String and key-value command implementations.
src/command/string_commands.cpp - Server cmds · Server and admin-oriented commands.
src/command/server_commands.cpp - Repl cmds · Commands that expose replication control and state.
src/command/replication_commands.cpp
State
Authoritative in-memory data and replication state.
Config
Runtime and replication configuration.
- Config · General server configuration.
src/config/config.cpp - Repl config · Replication-specific runtime settings.
src/config/replication_config.cpp
Connections 21
- 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.