# antonychr/redisgo architecture

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

- Interactive diagram: https://gitdiagram.com/antonychr/redisgo
- Repository: https://github.com/antonychr/redisgo

## Overview

##### Project Type and Purpose
- This is a networked server application written in Go that implements a subset of Redis functionality (RESP protocol parsing, command execution, in-memory storage, and master-slave replication).
- It’s not a full “frontend/backend” web app but a standalone TCP service that speaks the Redis protocol.

##### File Structure
- Top-level entry point: main.go (starts the server)
- command/ : extracts and dispatches Redis commands
- network/ : TCP server implementation
- protocol/ : RESP protocol parser
- redis/    : core Redis instance logic (manages config, info, etc.)
- replica/  : replication (master/slave) logic
- storage/  : in-memory key-value and list storage
- utils/    : helper functions (e.g., crypto if authentication is added)

##### README for Stack Insights
- Uses Go ≥1.18
- Implements RESPv2 parsing, concurrent client handling, basic commands (PING, SET, GET, LPUSH, etc.), and simple replication.
- No external databases—pure in-process storage in `storage/`.

##### Main Components
a. Client(s)
- External Redis CLI or any RESP client
b. TCP Server (network/server.go)
- Listens on port 6379
- Spawns a goroutine per connection
c. Protocol Parser (protocol/parser.go)
- Parses incoming RESP messages into structured commands
d. Command Handler (command/…)
- extract_commands.go → maps parsed input to a command enum
- concrete_handlers.go → business logic for each command
e. Redis Instance (redis/Instance.go & info_controller.go)
- Configures the server, holds references to storage and replica modules
- Exposes an interface to execute commands and retrieve INFO
f. Storage Engine (storage/storage.go)
- In-memory data structures for string and list types
g. Replication Module (replica/replica.go)
- Coordinates master-slave syncing; applies commands on the slave
h. Utilities (utils/crypto.go)
- Any helper routines (e.g., hashing or authentication support)

##### Relationships & Data Flow
1. Client → TCP Server: client sends raw RESP data over TCP
2. TCP Server → Parser: raw bytes streamed into parser
3. Parser → Command Handler: parser emits a command object
4. Command Handler → Redis Instance: calls into instance.Execute(cmd)
5. Redis Instance → Storage / Replication
- For read/write: interacts with in-memory storage
- For replication: if master, forwards command to replica; if slave, applies incoming replication data
6. Redis Instance → Command Handler: returns result
7. Command Handler → TCP Server: serializes result to RESP
8. TCP Server → Client: writes RESP-encoded response

##### Architectural Patterns & Principles
- Modular layering: network → protocol → handler → core logic → storage/replica
- Concurrency: goroutine per client, sync primitives in storage/replica
- Separation of concerns: parsing vs. execution vs. persistence vs. replication

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "Client Layer"
        Client["redis-cli / Client"]:::external
    end

    subgraph "Network & Protocol Layer"
        Server["network/server.go\n(TCP Server)"]:::network
        PARSER_IF["protocol/parser_interface.go\n(Parser Interface)"]:::network
        PARSER_IMPL["protocol/redis_protocol_parser.go\n(Protocol Parser)"]:::network
    end

    subgraph "Core Logic Layer"
        MAIN["main.go\n(Application Entry Point)"]:::core
        EXTRACT["command/extract_commands.go\n(Command Extraction)"]:::core
        DISPATCH["command/command_handler.go\n(Handler Dispatch)"]:::core
        CONCRETE["command/concrete_handlers.go\n(Concrete Command Logic)"]:::core
        INSTANCE["redis/Instance.go\n(Core Engine)"]:::core
        INFO["redis/info_controller.go\n(INFO Controller)"]:::core
        REDIS_ENTRY["redis/redis.go\n(Entry Logic)"]:::core
    end

    subgraph "Persistence & Replication Layer"
        STORAGE["storage/storage.go\n(In-Memory Store)"]:::data
        REPLICA["replica/replica.go\n(Replication Engine)"]:::data
    end

    UTILS["utils/crypto.go\n(Utilities)"]:::util

    Client -->|1. Raw RESP| Server
    MAIN -->|starts| Server
    MAIN -->|configures| INSTANCE
    Server -->|2. Stream Bytes| PARSER_IF
    PARSER_IF -->|uses| PARSER_IMPL
    PARSER_IMPL -->|2. Parsed Command| EXTRACT
    EXTRACT -->|3. Execute Command| DISPATCH
    DISPATCH -->|dispatches| CONCRETE
    CONCRETE -->|executes| INSTANCE
    INSTANCE -->|provides INFO| INFO
    INSTANCE -->|4a. Fetch/Update Data| STORAGE
    INSTANCE -->|4b. Replicate Command| REPLICA
    REPLICA -->|apply replication| STORAGE
    INSTANCE -->|uses| UTILS
    INSTANCE -->|result| DISPATCH
    DISPATCH -->|4. Format RESP| PARSER_IMPL
    PARSER_IMPL -->|5. RESP Response| Server
    Server -->|5. RESP Response| Client

    click Server "https://github.com/antonychr/redisgo/blob/main/network/server.go"
    click PARSER_IF "https://github.com/antonychr/redisgo/blob/main/protocol/parser_interface.go"
    click PARSER_IMPL "https://github.com/antonychr/redisgo/blob/main/protocol/redis_protocol_parser.go"
    click EXTRACT "https://github.com/antonychr/redisgo/blob/main/command/extract_commands.go"
    click DISPATCH "https://github.com/antonychr/redisgo/blob/main/command/command_handler.go"
    click CONCRETE "https://github.com/antonychr/redisgo/blob/main/command/concrete_handlers.go"
    click INSTANCE "https://github.com/antonychr/redisgo/blob/main/redis/Instance.go"
    click INFO "https://github.com/antonychr/redisgo/blob/main/redis/info_controller.go"
    click REDIS_ENTRY "https://github.com/antonychr/redisgo/blob/main/redis/redis.go"
    click STORAGE "https://github.com/antonychr/redisgo/blob/main/storage/storage.go"
    click REPLICA "https://github.com/antonychr/redisgo/blob/main/replica/replica.go"
    click UTILS "https://github.com/antonychr/redisgo/blob/main/utils/crypto.go"
    click MAIN "https://github.com/antonychr/redisgo/blob/main/main.go"

    classDef network fill:#D0E8FF,stroke:#000;
    classDef core fill:#DFFFD0,stroke:#000;
    classDef data fill:#FFD8A8,stroke:#000;
    classDef util fill:#E8E8E8,stroke:#000;
    classDef external fill:#F0F0F0,stroke:#000;
```
