# miguelrodriguezrv/codecrafters-redis-go architecture

> Architecture diagram of the GitHub repository miguelrodriguezrv/codecrafters-redis-go, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/miguelrodriguezrv/codecrafters-redis-go
- Repository: https://github.com/miguelrodriguezrv/codecrafters-redis-go

## Overview

##### Main Components
- Entry Point & Server Layer:
- The entry point is in app/server.go. This is where client connections are accepted.
- The “app/server” directory has multiple files (e.g., handler.go, replica.go, handler_replication.go, handler_stream.go, handler_transaction.go) that indicate subcomponents for request handling, replication, streaming, and transactions.
- Protocol Parser:
- Under app/parser, files like resp.go and its corresponding tests indicate logic for parsing the Redis Serialization Protocol (RESP). This component transforms raw input from clients into structured commands.
- Data Storage / Engine:
- The core in-memory store is defined in app/store (with inmemory.go and test files) which implements quick access.
- Additionally, the “app/store/art” directory suggests an Adaptive Radix Tree is used for efficient storage or indexing.
- There is also a stream component in app/store/stream.go to potentially handle stream data structures.
- Persistence Layer:
- The app/persistence directory shows modules that deal with persistence (file-based storage, database abstractions, CRC64 checksum calculations for data integrity) through files like file.go, reader.go, writer.go, and the crc64 submodule.
- Testing & Auxiliary Scripts:
- The test and codecrafters-related files indicate CI or challenge-run scripts (e.g., your_program.sh) that kick off the server for testing.

##### Relationships and Data Flow
- Client Interaction:
- Clients connect to the server entry point (app/server.go). This component accepts connections, likely using an event loop to manage client requests similar to how Redis works.
- Request Processing:
- Incoming data flows from the network to the Parser (app/parser) for decoding the RESP commands.
- The parsed commands are then dispatched within the server layer to appropriate handlers in app/server, which decide what operations to perform (e.g., replication, transactions, streaming).
- Data Storage:
- Persistence:
- If a command requires persistence (or as part of background tasks like replication or saving data), the server or store components will leverage modules in app/persistence.
- The CRC64 checksum component in persistence ensures data integrity when writing to files or managing data consistency.

##### Architectural Patterns & Design Principles
- Modular design: The project is split between clear modules for parsing, server logic, storage, and persistence.
- Separation of Concerns: Each high-level function (networking, protocol handling, command execution, storage, and persistence) lives in its own directory/module.
- Pluggability & Scalability: With distinct components for in-memory and ART-based storage, and files handling replication and transactions, the design allows for swapping or upgrading individual aspects.

##### Notable Technologies and Frameworks
- Go Modules (as shown by go.mod and go.sum).
- Standard Go networking libraries for the server.
- Custom implementations for common data structures (in-memory, ART tree).
- Persistence and file management for simulating Redis’s snapshotting or AOF persistence.

## Diagram (Mermaid)

```mermaid
flowchart TD
    A["Client"]:::clientStyle
    A -->|"connect"| B["Entry (app/server.go)"]:::serverStyle

    subgraph "Server Layer"
        B
        C["Handler (app/server/handler.go)"]:::serverStyle
        D["Handler Replication (app/server/handler_replication.go)"]:::serverStyle
        E["Handler Stream (app/server/handler_stream.go)"]:::serverStyle
        F["Handler Transaction (app/server/handler_transaction.go)"]:::serverStyle
        G["Replica (app/server/replica.go)"]:::serverStyle
    end

    B -->|"Init_Handler"| C
    C -->|"Dispatch_Command"| X["Command Dispatcher"]:::serverStyle

    B -->|"Receive_raw_data"| H["RESP Parser (app/parser/resp.go)"]:::parserStyle
    H -->|"Parsed_Command"| X

    subgraph "Data Storage"
        I["In-Memory Store (app/store/inmemory.go)"]:::storageStyle
        J["ART-based Store (app/store/art/tree.go)"]:::storageStyle
        K["Stream Handling (app/store/stream.go)"]:::storageStyle
    end

    subgraph "Persistence Layer"
        L["Persistence Manager (app/persistence/file.go)"]:::persistenceStyle
        M["CRC64 Validator (app/persistence/crc64/crc64.go)"]:::persistenceStyle
    end

    X -->|"Store_Operation"| I
    X -->|"Store_Index"| J
    X -->|"Stream_Command"| K
    X -->|"Persist_Data"| L
    L -->|"Checksum_Validation"| M

    classDef clientStyle fill:#FFDEAD,stroke:#333,stroke-width:2px;
    classDef serverStyle fill:#B0E0E6,stroke:#333,stroke-width:2px;
    classDef parserStyle fill:#98FB98,stroke:#333,stroke-width:2px;
    classDef storageStyle fill:#DDA0DD,stroke:#333,stroke-width:2px;
    classDef persistenceStyle fill:#ADD8E6,stroke:#333,stroke-width:2px;

    click B "https://github.com/miguelrodriguezrv/codecrafters-redis-go/blob/master/app/server.go"
    click C "https://github.com/miguelrodriguezrv/codecrafters-redis-go/blob/master/app/server/handler.go"
    click D "https://github.com/miguelrodriguezrv/codecrafters-redis-go/blob/master/app/server/handler_replication.go"
    click E "https://github.com/miguelrodriguezrv/codecrafters-redis-go/blob/master/app/server/handler_stream.go"
    click F "https://github.com/miguelrodriguezrv/codecrafters-redis-go/blob/master/app/server/handler_transaction.go"
    click G "https://github.com/miguelrodriguezrv/codecrafters-redis-go/blob/master/app/server/replica.go"
    click H "https://github.com/miguelrodriguezrv/codecrafters-redis-go/blob/master/app/parser/resp.go"
    click I "https://github.com/miguelrodriguezrv/codecrafters-redis-go/blob/master/app/store/inmemory.go"
    click J "https://github.com/miguelrodriguezrv/codecrafters-redis-go/blob/master/app/store/art/tree.go"
    click K "https://github.com/miguelrodriguezrv/codecrafters-redis-go/blob/master/app/store/stream.go"
    click L "https://github.com/miguelrodriguezrv/codecrafters-redis-go/blob/master/app/persistence/file.go"
    click M "https://github.com/miguelrodriguezrv/codecrafters-redis-go/blob/master/app/persistence/crc64/crc64.go"
```
