# dhravya/radish architecture

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

- Interactive diagram: https://gitdiagram.com/dhravya/radish
- Repository: https://github.com/dhravya/radish

## Overview

##### Project type and purpose
- Radish is a single-process, in-memory key-value store (a Redis replacement) written in Go. It listens over TCP (and RESP protocol), parses commands, executes them against in-memory data structures, implements persistence and pub/sub, and speaks the Redis protocol.

##### Key components (from file tree + README)
- Entry point: main.go
- Protocol handling: redisproto/ (parser.go, writer.go, fmt.go)
- Command dispatch & handlers: utils.go, persistence.go, pubsub.go, transactions.go (plus any command-specific code in utils or dedicated files)
- In-memory datastore: maps, slices, sorted sets, hashes, etc. (implemented throughout utils.go & go files)
- Persistence engine: persistence.go (SAVE, BGSAVE)
- Pub/Sub engine: pubsub.go
- Transaction manager: transactions.go (MULTI/EXEC/DISCARD)
- External interfaces: Dockerfile, CLI binaries in bin/
- Tests: tests/*.py and redisproto/parser_test.go

##### High-level architecture & interactions
- Clients (redis-cli, any RESP client) connect over TCP to the Radish server.
- The server’s TCP listener (main.go) hands raw RESP frames to the redisproto parser.
- Parsed commands are routed by a command dispatcher (in main.go or utils.go) to the appropriate handler module (strings, lists, sets, hashes, sorted-sets, keys, transactions, pub/sub, persistence).
- Handlers manipulate in-memory data structures (primary data store).
- Persistence manager listens for SAVE/BGSAVE commands and snapshots/flushing data to disk (AOF/RDB logic in persistence.go).
- Pub/Sub manager tracks channels and subscriber lists; PUBLISH calls fan-out messages to subscribers.
- Transaction manager queues commands in a MULTI block and executes atomically on EXEC.
- The response writer (redisproto/writer.go) formats replies into RESP and pushes them back over the TCP connection.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Clients
    Client["Clients (redis-cli, any RESP client)"]:::external
    CLI["CLI Binaries (bin/)"]:::external
    Tests["Integration & Unit Tests (tests/, parser_test.go, writer_test.go)"]:::external

    %% Network Layer
    subgraph "Network Layer"
        Listener["TCP Listener / Connection Manager\n(main.go)"]:::network
    end

    %% Protocol Layer
    subgraph "Protocol Layer"
        Parser["Protocol Parser\n(redisproto/parser.go)"]:::protocol
        Formatter["Protocol Formatting Utilities\n(redisproto/fmt.go)"]:::protocol
        Writer["Response Writer\n(redisproto/writer.go)"]:::protocol
    end

    %% Core Logic
    subgraph "Core Logic"
        Dispatcher["Command Dispatcher & Core Handlers\n(utils.go)"]:::corelogic
    end

    %% Data Modules
    subgraph "Data Modules"
        InMemory["In-Memory Data Store Structures & Helpers\n(utils.go)"]:::datastore
        Persistence["Persistence Manager\n(persistence.go)"]:::sidecar
        PubSub["Pub/Sub Engine\n(pubsub.go)"]:::sidecar
        TxManager["Transaction Manager\n(transactions.go)"]:::sidecar
    end

    %% External Interface
    Docker["Docker Configuration\n(Dockerfile)"]:::external

    %% Flows
    Client -->|"TCP Req (RESP frames)"| Listener
    Listener -->|"raw RESP frames"| Parser
    Parser -->|"parsed commands"| Dispatcher
    Dispatcher -->|"reads/writes"| InMemory
    Dispatcher -->|"SAVE/BGSAVE"| Persistence
    Dispatcher -->|"PUBLISH/SUBSCRIBE"| PubSub
    Dispatcher -->|"MULTI/EXEC/DISCARD"| TxManager
    TxManager -->|"exec ops"| InMemory
    Persistence -->|"snapshots/AOF"| InMemory
    InMemory -->|"data state"| Writer
    Formatter --> Writer
    Dispatcher --> Writer
    Writer -->|"RESP replies"| Client

    Client -.-> Tests
    Client -.-> CLI
    Listener --> Docker

    %% Click Events
    click Listener "https://github.com/dhravya/radish/blob/main/main.go"
    click Parser "https://github.com/dhravya/radish/blob/main/redisproto/parser.go"
    click Formatter "https://github.com/dhravya/radish/blob/main/redisproto/fmt.go"
    click Writer "https://github.com/dhravya/radish/blob/main/redisproto/writer.go"
    click Dispatcher "https://github.com/dhravya/radish/blob/main/utils.go"
    click InMemory "https://github.com/dhravya/radish/blob/main/utils.go"
    click Persistence "https://github.com/dhravya/radish/blob/main/persistence.go"
    click PubSub "https://github.com/dhravya/radish/blob/main/pubsub.go"
    click TxManager "https://github.com/dhravya/radish/blob/main/transactions.go"
    click Docker "https://github.com/dhravya/radish/tree/main/Dockerfile"
    click CLI "https://github.com/dhravya/radish/tree/main/bin/"
    click Tests "https://github.com/dhravya/radish/tree/main/tests/"

    %% Styles
    classDef network fill:#D6EAF8,stroke:#1B4F72,stroke-width:1px
    classDef protocol fill:#D5F5E3,stroke:#196F3D,stroke-width:1px
    classDef corelogic fill:#FCF3CF,stroke:#7D6608,stroke-width:1px
    classDef datastore fill:#FCF3CF,stroke:#7D6608,stroke-width:1px
    classDef sidecar fill:#FADBD8,stroke:#922B21,stroke-width:1px
    classDef external fill:#EAECEE,stroke:#626567,stroke-width:1px
```
