# sleeprite/rudis architecture

> Architecture diagram of the GitHub repository sleeprite/rudis, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-25). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sleeprite/rudis
- Repository: https://github.com/sleeprite/rudis

## Overview

##### Project type and purpose
- Rudis is an open-source, Redis-compatible, high-performance in-memory key-value database written in Rust.
- It implements the Redis wire protocol, provides common data structures (strings, lists, hashes, sets, sorted sets), supports RDB/AOF persistence, and can be deployed as a standalone binary or in Docker.

##### File structure
- Top-level: Cargo manifest, Docker assets, release binaries, docs/ (VitePress-powered static site), examples/, tests/.
- src/ contains the main crate with these sub-modules:
- server.rs / server_handler.rs – network listener, TCP framing, request/response loop
- frame.rs – Redis protocol parser/generator
- session.rs – per-connection state (selected DB index, authentication)
- cmd/ – one file per command or per command group, implements the “Command” trait
- db.rs – in-memory storage engine and data-structure implementations
- persistence/ – RDB snapshotter and AOF appender modules
- args.rs, command.rs – command-line flags, dispatch registry
- tests/ exercise commands and performance.

##### README for additional insights
- Describes core components: command parsing, database operations, persistence, session management.
- Mentions configuration options (port, bind, AOF/RDB settings).

1. Clients (any Redis client library or CLI)
2. Rudis Server
– Config Loader (args.rs + properties file)
– TCP Listener & Reactor (server.rs)
– Protocol Layer (frame.rs)
– Session Manager (session.rs)
– Command Dispatcher (command.rs)
– Command Modules (src/cmd/*.rs)
– Storage Engine (db.rs)
– Persistence Manager (persistence/rdb & persistence/aof)
– Logging & Metrics
3. File System (RDB/AOF files on disk)
4. Optional: Docker / Release Packaging (docker/Dockerfile, release/)
5. Documentation Site (docs/ – outside runtime, but part of the project)

b. Relationships and data flow
- TCP Listener → Protocol Layer (parse incoming byte frames)
- Protocol Layer → Session Manager (attaches DB index & auth state)
- Session Manager → Command Dispatcher (routes command name to handler)
- Dispatcher → Command Module (e.g., cmd/get.rs, cmd/set.rs)
- Command Module ↔ Storage Engine (query/update in-memory data)
- Write commands → Persistence Manager → File System (append AOF / trigger RDB)
- Command Module → Protocol Layer (serialize response)
- Protocol Layer → TCP Listener → Clients

c. Architectural patterns & design principles
– Modular “command” plugin pattern: each Redis command is its own Rust module, registered in a dispatch table.
– Single-responsibility: separate protocol framing, session tracking, storage, persistence.
– Layered design: network → protocol → logic → storage → disk.
– Extensibility point: add new cmd/*.rs modules and register in command.rs.

d. Key technologies & frameworks
– Rust & Cargo (crate management)
– Tokio or std::net (async or blocking TCP I/O in server.rs)
– Custom Redis protocol implementation (frame.rs)
– Static site generator (VitePress in docs/) for documentation

##### Detail is critical but avoid over-complicating
- How a new command would flow through the same pipeline.

## Diagram (Mermaid)

```mermaid
flowchart TD
    Clients["Clients (Redis CLI/Lib)"]:::external

    subgraph "Rudis Server"
        ConfigLoader["Config Loader\n(args.rs)"]:::deployment
        TCPListener["TCP Listener & Reactor\n(server.rs, server_handler.rs)"]:::network
        ProtocolLayer["Protocol Layer\n(frame.rs)"]:::network
        SessionManager["Session Manager\n(session.rs)"]:::logic
        CommandDispatcher["Command Dispatcher\n(command.rs)"]:::logic
        CommandModules["Command Modules\n(src/cmd/*.rs)"]:::logic
        StorageEngine["Storage Engine\n(db.rs)"]:::storage
        PersistenceManager["Persistence Manager\n(persistence/rdb & aof)"]:::persistence
    end

    FileSystem[(RDB/AOF Files\non Disk)]:::persistence

    Docker["Docker Deployment\ndocker/Dockerfile"]:::deployment
    Release["Release Packaging\nrelease/"]:::deployment
    Docs["Documentation Site\ndocs/"]:::deployment

    %% Data Flow
    Clients -->|requests| TCPListener
    TCPListener --> ProtocolLayer
    ProtocolLayer --> SessionManager
    SessionManager --> CommandDispatcher
    CommandDispatcher --> CommandModules
    CommandModules <--> StorageEngine
    CommandModules -->|write cmds| PersistenceManager
    PersistenceManager --> FileSystem
    CommandModules --> ProtocolLayer
    ProtocolLayer --> TCPListener
    TCPListener -->|responses| Clients

    ConfigLoader --> TCPListener

    %% External Artifacts
    ConfigLoader --> Docker
    ConfigLoader --> Release
    ConfigLoader --> Docs

    %% Click Events
    click ConfigLoader "https://github.com/sleeprite/rudis/blob/0.0.6/src/args.rs"
    click TCPListener "https://github.com/sleeprite/rudis/blob/0.0.6/src/server.rs"
    click TCPListener "https://github.com/sleeprite/rudis/blob/0.0.6/src/server_handler.rs"
    click ProtocolLayer "https://github.com/sleeprite/rudis/blob/0.0.6/src/frame.rs"
    click CommandDispatcher "https://github.com/sleeprite/rudis/blob/0.0.6/src/command.rs"
    click CommandModules "https://github.com/sleeprite/rudis/tree/0.0.6/src/cmd/"
    click StorageEngine "https://github.com/sleeprite/rudis/blob/0.0.6/src/db.rs"
    click Docker "https://github.com/sleeprite/rudis/tree/0.0.6/docker/Dockerfile"
    click Release "https://github.com/sleeprite/rudis/tree/0.0.6/release/"
    click Docs "https://github.com/sleeprite/rudis/tree/0.0.6/docs/"

    %% Styles
    classDef network fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef logic fill:#90EE90,stroke:#333,stroke-width:1px
    classDef storage fill:#FFA500,stroke:#333,stroke-width:1px
    classDef persistence fill:#D2B48C,stroke:#333,stroke-width:1px
    classDef deployment fill:#D3D3D3,stroke:#333,stroke-width:1px
    classDef external fill:#FFFFFF,stroke:#333,stroke-width:1px,stroke-dasharray: 5 5
```
