# redis-go/redis architecture

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

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

## Overview

Project Type: This is a database system project, specifically a Redis clone implementation in Go. It's a server application that implements the Redis protocol (RESP) and various Redis data structures.

##### Main Components

a. Client Interface Layer
- TCP connection handling (from serve.go)
- RESP Protocol implementation
- Client command processing

b. Command Processing Layer
- Command Registry System
- Individual Command Handlers (shown by cmd_*.go files)
- Command Execution Pipeline

c. Data Storage Layer
- In-memory Database (redisdb.go)
- Data Structures Implementation
- Strings (t_string.go)
- Lists (t_list.go)
- Future implementations (Sets, Sorted Sets, Hashes)

d. Key Management System
- Key Expiration Manager (keyexpire.go)
- TTL Handling

##### Component Relationships

- Client → Server Connection
- Command Processing Pipeline:
- Client Request → RESP Protocol Parsing → Command Registry →
- Command Handler → Data Storage → Response Generation → Client Response
- Key Expirer interaction with Data Storage

Create three main sections:

##### Technical Details

- TCP server implementation
- RESP protocol handling
- Command registration system
- In-memory data structure implementations
- Key expiration mechanism

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Client["Client Interface Layer"]
        TCP["TCP Server"]:::server
        CH["Client Handler"]:::handler
        RESP["RESP Protocol Parser"]:::protocol
    end

    subgraph CmdProc["Command Processing Layer"]
        CR["Command Registry"]:::registry
        subgraph CmdHandlers["Command Handlers"]
            direction TB
            PING["PING Handler"]:::handler
            GET["GET Handler"]:::handler
            SET["SET Handler"]:::handler
            DEL["DEL Handler"]:::handler
            TTL["TTL Handler"]:::handler
            LPUSH["LPUSH Handler"]:::handler
            RPUSH["RPUSH Handler"]:::handler
            LPOP["LPOP Handler"]:::handler
            RPOP["RPOP Handler"]:::handler
            LRANGE["LRANGE Handler"]:::handler
        end
    end

    subgraph Storage["Data Storage Layer"]
        DB["In-memory Database"]:::database
        subgraph DataStructures["Data Structures"]
            direction TB
            STR["String Operations"]:::datastructure
            LST["List Operations"]:::datastructure
            SETS["Sets (Planned)"]:::planned
            ZSETS["Sorted Sets (Planned)"]:::planned
            HASH["Hashes (Planned)"]:::planned
        end
    end

    subgraph Management["Management Systems"]
        KEM["Key Expiration Manager"]:::manager
        PERS["Persistence (Planned)"]:::planned
        PUBSUB["Pub/Sub (Planned)"]:::planned
        CLUSTER["Clustering (Planned)"]:::planned
        REPL["Replication (Planned)"]:::planned
    end

    Client --> CmdProc
    TCP --> CH
    CH --> RESP
    RESP --> CR
    CR --> CmdHandlers
    CmdHandlers --> Storage
    Storage --> KEM
    DB --> DataStructures

    classDef server fill:#4a9eff,stroke:#333,stroke-width:2px
    classDef handler fill:#67b86d,stroke:#333,stroke-width:2px
    classDef protocol fill:#4a9eff,stroke:#333,stroke-width:2px
    classDef registry fill:#67b86d,stroke:#333,stroke-width:2px
    classDef database fill:#ffd700,stroke:#333,stroke-width:2px
    classDef datastructure fill:#ffd700,stroke:#333,stroke-width:2px
    classDef manager fill:#ff6b6b,stroke:#333,stroke-width:2px
    classDef planned fill:#ccc,stroke:#666,stroke-width:2px,stroke-dasharray: 5 5

    click TCP "https://github.com/redis-go/redis/blob/master/serve.go"
    click CH "https://github.com/redis-go/redis/blob/master/client.go"
    click CR "https://github.com/redis-go/redis/blob/master/command.go"
    click PING "https://github.com/redis-go/redis/blob/master/cmd_ping.go"
    click GET "https://github.com/redis-go/redis/blob/master/cmd_get.go"
    click SET "https://github.com/redis-go/redis/blob/master/cmd_set.go"
    click DEL "https://github.com/redis-go/redis/blob/master/cmd_del.go"
    click TTL "https://github.com/redis-go/redis/blob/master/cmd_ttl.go"
    click LPUSH "https://github.com/redis-go/redis/blob/master/cmd_lpush.go"
    click RPUSH "https://github.com/redis-go/redis/blob/master/cmd_rpush.go"
    click LPOP "https://github.com/redis-go/redis/blob/master/cmd_lpop.go"
    click RPOP "https://github.com/redis-go/redis/blob/master/cmd_rpop.go"
    click LRANGE "https://github.com/redis-go/redis/blob/master/cmd_lrange.go"
    click DB "https://github.com/redis-go/redis/blob/master/redisdb.go"
    click STR "https://github.com/redis-go/redis/blob/master/t_string.go"
    click LST "https://github.com/redis-go/redis/blob/master/t_list.go"
    click KEM "https://github.com/redis-go/redis/blob/master/keyexpire.go"
```
