# subayel-wali/redis_clone architecture

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

- Interactive diagram: https://gitdiagram.com/subayel-wali/redis_clone
- Repository: https://github.com/subayel-wali/redis_clone

## Overview

This is a Redis-inspired, multithreaded TCP in-memory database. Clients send textual commands to the server, which parses and dispatches them through command objects into shared storage; storage also handles expiration, LRU eviction, and append-only persistence/replay. The README documents networking and background expiration, while sampled source confirms storage, command construction, persistence calls, and the LRU cache. Some server dispatch and unsampled command implementations are represented only at the documented subsystem level.

Main entry points: [`src/storage/StorageEngine.cpp`](https://github.com/subayel-wali/redis_clone/blob/main/src/storage/StorageEngine.cpp) (Storage engine), [`src/commands/factory/CommandFactory.cpp`](https://github.com/subayel-wali/redis_clone/blob/main/src/commands/factory/CommandFactory.cpp) (Command factory), [`src/server/RedisServer.cpp`](https://github.com/subayel-wali/redis_clone/blob/main/src/server/RedisServer.cpp) (Redis server), [`src/validation/CommandValidator.cpp`](https://github.com/subayel-wali/redis_clone/blob/main/src/validation/CommandValidator.cpp) (Command validation)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_network["Client server"]
  node_server["Redis server<br/>[RedisServer.cpp]"]
end

subgraph group_commands["Command processing"]
  node_parser["Command parser<br/>[CommandParser.cpp]"]
  node_handler["Command handler<br/>[CommandHandler.cpp]"]
  node_factory["Command factory<br/>[CommandFactory.cpp]"]
  node_kv["KV commands<br/>[SetCommand.cpp]"]
  node_listcmd["List commands<br/>[LPUSHCommand.cpp]"]
  node_hashcmd["Hash commands<br/>[HSETCommand.cpp]"]
  node_ttlcmd["TTL commands<br/>[ExpireCommand.cpp]"]
  node_validator["Command validation"]
end

subgraph group_state["Database state"]
  node_storage[("Storage engine<br/>[StorageEngine.cpp]")]
  node_cache["LRU eviction<br/>[LRUCache.cpp]"]
  node_expiry["Expiry worker<br/>[main.cpp]"]
end

subgraph group_durability["Durability"]
  node_persistence[("AOF persistence")]
end

node_client(("TCP client"))

node_client -->|"sends commands"| node_server
node_server -.->|"parses input"| node_parser
node_server -.->|"dispatches input"| node_handler
node_handler -->|"gets command"| node_factory
node_factory -->|"creates commands"| node_kv
node_factory -->|"creates commands"| node_listcmd
node_factory -->|"creates commands"| node_hashcmd
node_factory -->|"creates commands"| node_ttlcmd
node_kv -->|"validates input"| node_validator
node_listcmd -->|"validates input"| node_validator
node_hashcmd -->|"validates input"| node_validator
node_ttlcmd -->|"validates input"| node_validator
node_kv -->|"executes operations"| node_storage
node_listcmd -->|"executes operations"| node_storage
node_hashcmd -->|"executes operations"| node_storage
node_ttlcmd -->|"executes operations"| node_storage
node_storage -->|"updates recency"| node_cache
node_expiry -->|"cleans expired keys"| node_storage
node_storage -->|"appends and replays"| node_persistence
node_server -.->|"sends replies"| node_client

click node_server "https://github.com/subayel-wali/redis_clone/blob/main/src/server/RedisServer.cpp"
click node_parser "https://github.com/subayel-wali/redis_clone/blob/main/src/parser/CommandParser.cpp"
click node_handler "https://github.com/subayel-wali/redis_clone/blob/main/src/commands/CommandHandler.cpp"
click node_factory "https://github.com/subayel-wali/redis_clone/blob/main/src/commands/factory/CommandFactory.cpp"
click node_kv "https://github.com/subayel-wali/redis_clone/blob/main/src/commands/types/SetCommand.cpp"
click node_listcmd "https://github.com/subayel-wali/redis_clone/blob/main/src/commands/types/LPUSHCommand.cpp"
click node_hashcmd "https://github.com/subayel-wali/redis_clone/blob/main/src/commands/types/HSETCommand.cpp"
click node_ttlcmd "https://github.com/subayel-wali/redis_clone/blob/main/src/commands/types/ExpireCommand.cpp"
click node_validator "https://github.com/subayel-wali/redis_clone/blob/main/src/validation/CommandValidator.cpp"
click node_storage "https://github.com/subayel-wali/redis_clone/blob/main/src/storage/StorageEngine.cpp"
click node_cache "https://github.com/subayel-wali/redis_clone/blob/main/src/cache/LRUCache.cpp"
click node_expiry "https://github.com/subayel-wali/redis_clone/blob/main/src/main.cpp"
click node_persistence "https://github.com/subayel-wali/redis_clone/blob/main/src/persistence/PersistenceManager.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_server,node_client toneBlue
class node_parser,node_handler,node_factory,node_kv,node_listcmd,node_hashcmd,node_ttlcmd,node_validator toneAmber
class node_storage,node_cache,node_expiry toneMint
class node_persistence toneRose
```

## Components

### Client server

- [Redis server](https://github.com/subayel-wali/redis_clone/blob/main/src/server/RedisServer.cpp): `src/server/RedisServer.cpp`

### Command processing

- [Command parser](https://github.com/subayel-wali/redis_clone/blob/main/src/parser/CommandParser.cpp): `src/parser/CommandParser.cpp`
- [Command handler](https://github.com/subayel-wali/redis_clone/blob/main/src/commands/CommandHandler.cpp): `src/commands/CommandHandler.cpp`
- [Command factory](https://github.com/subayel-wali/redis_clone/blob/main/src/commands/factory/CommandFactory.cpp): `src/commands/factory/CommandFactory.cpp`
- [KV commands](https://github.com/subayel-wali/redis_clone/blob/main/src/commands/types/SetCommand.cpp): `src/commands/types/SetCommand.cpp`
- [List commands](https://github.com/subayel-wali/redis_clone/blob/main/src/commands/types/LPUSHCommand.cpp): `src/commands/types/LPUSHCommand.cpp`
- [Hash commands](https://github.com/subayel-wali/redis_clone/blob/main/src/commands/types/HSETCommand.cpp): `src/commands/types/HSETCommand.cpp`
- [TTL commands](https://github.com/subayel-wali/redis_clone/blob/main/src/commands/types/ExpireCommand.cpp): `src/commands/types/ExpireCommand.cpp`
- [Command validation](https://github.com/subayel-wali/redis_clone/blob/main/src/validation/CommandValidator.cpp): `src/validation/CommandValidator.cpp`

### Database state

- [Storage engine](https://github.com/subayel-wali/redis_clone/blob/main/src/storage/StorageEngine.cpp): `src/storage/StorageEngine.cpp`
- [LRU eviction](https://github.com/subayel-wali/redis_clone/blob/main/src/cache/LRUCache.cpp): `src/cache/LRUCache.cpp`
- [Expiry worker](https://github.com/subayel-wali/redis_clone/blob/main/src/main.cpp): `src/main.cpp`

### Durability

- [AOF persistence](https://github.com/subayel-wali/redis_clone/blob/main/src/persistence/PersistenceManager.cpp): `src/persistence/PersistenceManager.cpp`

### Other components

- **TCP client**

## Connections

- TCP client → Redis server: sends commands
- Redis server → Command parser: parses input
- Redis server → Command handler: dispatches input
- Command handler → Command factory: gets command (evidence: `src/commands/CommandHandler.cpp`)
- Command factory → KV commands: creates commands (evidence: `src/commands/factory/CommandFactory.cpp`)
- Command factory → List commands: creates commands (evidence: `src/commands/factory/CommandFactory.cpp`)
- Command factory → Hash commands: creates commands (evidence: `src/commands/factory/CommandFactory.cpp`)
- Command factory → TTL commands: creates commands (evidence: `src/commands/factory/CommandFactory.cpp`)
- KV commands → Command validation: validates input
- List commands → Command validation: validates input (evidence: `src/commands/types/LPUSHCommand.cpp`)
- Hash commands → Command validation: validates input (evidence: `src/commands/types/HSETCommand.cpp`)
- TTL commands → Command validation: validates input (evidence: `src/commands/types/ExpireCommand.cpp`)
- KV commands → Storage engine: executes operations
- List commands → Storage engine: executes operations (evidence: `src/storage/StorageEngine.cpp`)
- Hash commands → Storage engine: executes operations (evidence: `src/commands/types/HSETCommand.cpp`)
- TTL commands → Storage engine: executes operations (evidence: `src/commands/types/ExpireCommand.cpp`)
- Storage engine → LRU eviction: updates recency (evidence: `src/storage/StorageEngine.cpp`)
- Expiry worker → Storage engine: cleans expired keys (evidence: `src/main.cpp`)
- Storage engine → AOF persistence: appends and replays (evidence: `src/storage/StorageEngine.cpp`)
- Redis server → TCP client: sends replies
