# utkarshrai12/redis_codecrafters architecture

> Architecture diagram of the GitHub repository utkarshrai12/redis_codecrafters, 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/utkarshrai12/redis_codecrafters
- Repository: https://github.com/utkarshrai12/redis_codecrafters

## Overview

1. **What this repo is**
   - A Rust implementation of a Redis-like server for the CodeCrafters “Build Your Own Redis” challenge.
   - It is a single binary crate, with `src/main.rs` as the runtime entry point and the rest of `src/` split into protocol, command, storage, and replication concerns.

2. **Startup and wiring**
   - `main.rs` is the composition root: it likely parses CLI arguments, loads configuration, initializes state, starts the TCP server, and wires in command handling.
   - The `arg_processor` and `config_handler` modules suggest startup-time options such as port, replica settings, or other Redis flags.

3. **Network and request lifecycle**
   - `connection.rs` appears to own client socket handling and the per-connection read/write loop.
   - The flow is: bytes arrive on a connection → RESP decoding → command object creation → command dispatch → response encoding back to the client.

4. **Protocol layer**
   - `resp.rs` is the Redis Serialization Protocol boundary: it likely parses incoming frames and formats outgoing replies.
   - `command.rs` is the internal command representation, bridging raw protocol input to typed operations.

5. **Core data model**
   - `store/mod.rs` and `type_store/mod.rs` indicate a central in-memory database with explicit type tracking per key.
   - This is the main shared state boundary: higher-level handlers operate through it rather than manipulating raw maps directly.

6. **Specialized storage engines**
   - The repo splits Redis data structures into dedicated modules:
     - `set_store`
     - `list_store`
     - `sorted_set_handler`
     - `stream_store`
     - likely string/value operations are covered by the central store or related handlers
   - `list_store/helper.rs` and `stream_store/helper.rs` suggest internal helpers for ordering, iteration, or serialization logic.

7. **Command handlers by feature**
   - Several modules are organized by command family rather than by transport:
     - `auth_handler` for authentication/ACL-like checks
     - `geo_cord_handler` for geospatial commands
     - `sorted_set_handler` for sorted-set operations
     - `replica_handler` for replication-specific commands
     - `subscriptions` for Pub/Sub behavior
   - This implies a dispatcher pattern where command names are routed to feature-specific handlers.

8. **Replication architecture**
   - `replication.rs` and `replica_handler/mod.rs` suggest a master/replica design.
   - One part of the code likely manages replication handshake/state, while the replica handler serves replication-related commands and message flow.
   - This is a distinct subsystem from normal client command execution because it must track replica state and stream updates.

9. **Persistence / snapshot loading**
   - `rdb_loader` is the persistence boundary, focused on loading Redis database snapshots at startup.
   - `rdb_loader/file.rs` and `rdb_loader/enc.rs` imply file parsing plus encoding/decoding support for RDB data.
   - Data flow here is startup snapshot file → loader → in-memory store.

10. **Pub/Sub boundary**
   - `subscriptions/mod.rs` likely manages channel membership and fan-out delivery to connected clients.
   - This is a separate execution path from normal request/response commands because it involves server-side push behavior.

11. **Cross-cutting utilities**
   - `utils/mod.rs` holds shared helpers used across modules.
   - `src/test.rs` suggests local or challenge-specific test support.
   - The `codecrafters` scripts and `your_program.sh` are build/run harnesses rather than product runtime logic.

12. **Overall architecture**
   - The repo is organized around a clean pipeline:
     - startup/configuration
     - network connection handling
     - RESP protocol parsing
     - command routing
     - feature-specific handlers
     - shared in-memory storage
     - optional persistence and replication
   - For an architecture diagram, the main boxes should be: client connections, RESP protocol layer, command dispatcher, storage engine, replication subsystem, and RDB loader, with Pub/Sub as a side path.

Main entry points: [`src/store/mod.rs`](https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/store/mod.rs) (Store), [`src/cmd_store/mod.rs`](https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/cmd_store/mod.rs) (Dispatcher), [`src/main.rs`](https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/main.rs) (Main), [`src/rdb_loader/mod.rs`](https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/rdb_loader/mod.rs) (RDB loader)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime"]
  node_main_rs["Main<br/>binary entrypoint<br/>[main.rs]"]
  node_arg_processor["Args<br/>cli parsing<br/>[mod.rs]"]
  node_config_handler["Config<br/>[mod.rs]"]
  node_connection(("Connection<br/>socket loop<br/>[connection.rs]"))
  node_resp{{"RESP<br/>protocol codec<br/>[resp.rs]"}}
end

subgraph group_core["Core"]
  node_command["Command<br/>[command.rs]"]
  node_cmd_store["Dispatcher<br/>command routing<br/>[mod.rs]"]
  node_auth_handler["Auth<br/>[mod.rs]"]
  node_geo_cord_handler["Geo<br/>[mod.rs]"]
end

subgraph group_storage["Storage"]
  node_store[("Store<br/>database core<br/>[mod.rs]")]
  node_type_store["Types<br/>key type map<br/>[mod.rs]"]
  node_set_store["Sets<br/>set engine<br/>[mod.rs]"]
  node_list_store["Lists<br/>list engine<br/>[mod.rs]"]
  node_sorted_set_handler["Sorted sets<br/>zset handler<br/>[mod.rs]"]
  node_stream_store["Streams<br/>stream engine<br/>[mod.rs]"]
end

subgraph group_ops["Ops"]
  node_subscriptions["Pub/Sub<br/>push delivery<br/>[mod.rs]"]
  node_replication["Replication<br/>replica state<br/>[replication.rs]"]
  node_replica_handler["Replica cmds<br/>replication handler<br/>[mod.rs]"]
  node_rdb_loader["RDB loader<br/>snapshot loader<br/>[mod.rs]"]
  node_rdb_file["RDB file<br/>file parser<br/>[file.rs]"]
  node_rdb_enc["RDB encoding<br/>codec helpers<br/>[enc.rs]"]
end

node_main_rs -->|"reads args"| node_arg_processor
node_main_rs -->|"loads config"| node_config_handler
node_main_rs -->|"loads snapshot"| node_rdb_loader
node_main_rs -->|"starts server"| node_connection
node_connection -->|"decodes/encodes"| node_resp
node_resp -->|"builds commands"| node_command
node_command -->|"dispatches"| node_cmd_store
node_cmd_store -->|"mutates state"| node_store
node_store -->|"tracks types"| node_type_store
node_store -->|"delegates sets"| node_set_store
node_store -->|"delegates lists"| node_list_store
node_store -->|"delegates zsets"| node_sorted_set_handler
node_store -->|"delegates streams"| node_stream_store
node_cmd_store -->|"auth commands"| node_auth_handler
node_cmd_store -->|"geo commands"| node_geo_cord_handler
node_cmd_store -->|"pub/sub commands"| node_subscriptions
node_cmd_store -->|"replica commands"| node_replica_handler
node_replication -->|"coordination"| node_replica_handler
node_rdb_loader -->|"reads file"| node_rdb_file
node_rdb_loader -->|"decodes data"| node_rdb_enc
node_rdb_loader -->|"hydrates store"| node_store

click node_main_rs "https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/main.rs"
click node_arg_processor "https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/arg_processor/mod.rs"
click node_config_handler "https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/config_handler/mod.rs"
click node_connection "https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/connection.rs"
click node_resp "https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/resp.rs"
click node_command "https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/command.rs"
click node_cmd_store "https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/cmd_store/mod.rs"
click node_store "https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/store/mod.rs"
click node_type_store "https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/type_store/mod.rs"
click node_set_store "https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/set_store/mod.rs"
click node_list_store "https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/list_store/mod.rs"
click node_sorted_set_handler "https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/sorted_set_handler/mod.rs"
click node_stream_store "https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/stream_store/mod.rs"
click node_auth_handler "https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/auth_handler/mod.rs"
click node_geo_cord_handler "https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/geo_cord_handler/mod.rs"
click node_subscriptions "https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/subscriptions/mod.rs"
click node_replication "https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/replication.rs"
click node_replica_handler "https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/replica_handler/mod.rs"
click node_rdb_loader "https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/rdb_loader/mod.rs"
click node_rdb_file "https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/rdb_loader/file.rs"
click node_rdb_enc "https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/rdb_loader/enc.rs"

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_main_rs,node_arg_processor,node_config_handler,node_connection,node_resp toneBlue
class node_command,node_cmd_store,node_auth_handler,node_geo_cord_handler toneAmber
class node_store,node_type_store,node_set_store,node_list_store,node_sorted_set_handler,node_stream_store toneMint
class node_subscriptions,node_replication,node_replica_handler,node_rdb_loader,node_rdb_file,node_rdb_enc toneRose
```

## Components

### Runtime

Startup, network, and request lifecycle

- [Main](https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/main.rs): binary entrypoint · Composition root that wires startup, config, and the server loop · `src/main.rs`
- [Args](https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/arg_processor/mod.rs): cli parsing · Parses runtime flags and startup options · `src/arg_processor/mod.rs`
- [Config](https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/config_handler/mod.rs): startup config · Resolves server configuration and boot options · `src/config_handler/mod.rs`
- [Connection](https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/connection.rs): socket loop · Owns client connections and the per-connection read/write cycle · `src/connection.rs`
- [RESP](https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/resp.rs): protocol codec · Parses incoming RESP frames and encodes outgoing replies · `src/resp.rs`

### Core

Command model and shared application state

- [Command](https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/command.rs): command model · Internal command representation between protocol and handlers · `src/command.rs`
- [Dispatcher](https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/cmd_store/mod.rs): command routing · Routes parsed commands to feature-specific handlers · `src/cmd_store/mod.rs`
- [Auth](https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/auth_handler/mod.rs): auth handler · Handles authentication-related commands and checks · `src/auth_handler/mod.rs`
- [Geo](https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/geo_cord_handler/mod.rs): geo handler · Handles geospatial command behavior · `src/geo_cord_handler/mod.rs`

### Storage

Data structure engines and typed key state

- [Store](https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/store/mod.rs): database core · Central in-memory database boundary for shared state · `src/store/mod.rs`
- [Types](https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/type_store/mod.rs): key type map · Tracks value types and typed key metadata · `src/type_store/mod.rs`
- [Sets](https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/set_store/mod.rs): set engine · Implements set-specific storage and operations · `src/set_store/mod.rs`
- [Lists](https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/list_store/mod.rs): list engine · Implements list storage and ordering behavior · `src/list_store/mod.rs`
- [Sorted sets](https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/sorted_set_handler/mod.rs): zset handler · Handles sorted-set operations and scoring logic · `src/sorted_set_handler/mod.rs`
- [Streams](https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/stream_store/mod.rs): stream engine · Implements stream data and iteration behavior · `src/stream_store/mod.rs`

### Ops

Replication, persistence, and push delivery

- [Pub/Sub](https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/subscriptions/mod.rs): push delivery · Manages channel subscriptions and fan-out to clients · `src/subscriptions/mod.rs`
- [Replication](https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/replication.rs): replica state · Manages replication handshake, state, and update flow · `src/replication.rs`
- [Replica cmds](https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/replica_handler/mod.rs): replication handler · Serves replication-specific commands and coordination · `src/replica_handler/mod.rs`
- [RDB loader](https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/rdb_loader/mod.rs): snapshot loader · Loads Redis snapshot data into memory at startup · `src/rdb_loader/mod.rs`
- [RDB file](https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/rdb_loader/file.rs): file parser · Reads and parses snapshot file contents · `src/rdb_loader/file.rs`
- [RDB encoding](https://github.com/utkarshrai12/redis_codecrafters/blob/master/src/rdb_loader/enc.rs): codec helpers · Encodes and decodes RDB-related data formats · `src/rdb_loader/enc.rs`

## Connections

- Main → Args: reads args · Bootstraps runtime options from CLI input
- Main → Config: loads config · Builds server configuration before starting the loop
- Main → RDB loader: loads snapshot · Optionally restores state from an RDB file
- Main → Connection: starts server · Creates the TCP serving loop
- Connection → RESP: decodes/encodes · Transforms bytes to protocol frames and back
- RESP → Command: builds commands · Converts protocol messages into internal commands
- Command → Dispatcher: dispatches · Routes command objects to the appropriate handler
- Dispatcher → Store: mutates state · Performs normal database operations through the shared store
- Store → Types: tracks types · Maintains key-type metadata alongside stored values
- Store → Sets: delegates sets · Uses specialized set operations for set values
- Store → Lists: delegates lists · Uses specialized list operations for list values
- Store → Sorted sets: delegates zsets · Uses sorted-set logic for ordered score-based data
- Store → Streams: delegates streams · Uses stream logic for append-only and range operations
- Dispatcher → Auth: auth commands · Routes authentication-related commands
- Dispatcher → Geo: geo commands · Routes geospatial commands
- Dispatcher → Pub/Sub: pub/sub commands · Routes subscribe and publish behavior
- Dispatcher → Replica cmds: replica commands · Routes replication-specific command flow
- Replication → Replica cmds: coordination · Shares replication state with command handling
- RDB loader → RDB file: reads file · Parses snapshot file contents
- RDB loader → RDB encoding: decodes data · Uses encoding helpers while restoring state
- RDB loader → Store: hydrates store · Loads persisted data into the in-memory database
