# 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 2024-12-28). 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

##### Main Components Identification

a) Core Client Components:
- Base Redis Client (`redis.go`)
- Connection Pool Management (`internal/pool/`)
- Protocol Implementation (`internal/proto/`)
- Command Interface (`commands.go`, `command.go`)

b) Client Variations:
- Single-node Client
- Cluster Client (`cluster_commands.go`)
- Sentinel Client (`sentinel.go`)
- Ring Client (`ring.go`)

##### Architectural Layers

**Top Layer: Client Interface**

**Middle Layer: Command Processing & Features**

**Bottom Layer: Connection & Protocol Handling**

##### Component Relationships

a) Client Interface Layer:
- How different client types (Single, Cluster, Sentinel, Ring) inherit from base client
- To command interfaces

b) Command Processing Layer:
- Link command processors to protocol handlers
- Pipeline and transaction handling
- To connection pool management

c) Connection Layer:
- Connection pool management
- Protocol (RESP2/RESP3) handling
- Demonstrate connection to Redis servers

##### Special Features to Highlight

- PubSub System (`pubsub.go`)
- Pipeline Processing (`pipeline.go`)
- Transaction Handling (`tx.go`)
- Scripting Support (`script.go`)
- Monitoring & Instrumentation (extra/redisotel, extra/redisprometheus)

**┌─────────────────┐**
- Client Layer   │ → Use rectangles for main components
- ┘

**⬡ Use hexagons for interfaces**

**● Use dots for connection points**

##### Implementation Details

- How the client handles context propagation
- Command execution flow
- Demonstrate connection pooling strategy
- Error handling paths
- Monitoring/metrics collection points

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client_Layer
        BaseClient["Base Redis Client"]
        UniversalClient["Universal Client"]
        ClusterClient["Cluster Client"]
        SentinelClient["Sentinel Client"]
        RingClient["Ring Client"]
    end

    subgraph Command_Processing
        CmdInterface["Command Interface"]
        Pipeline["Pipeline Processing"]
        Transaction["Transaction Handling"]
        PubSub["PubSub System"]
        Script["Scripting Support"]
        CmdResults["Command Results"]
        ErrorHandling["Error Handling"]
    end

    subgraph Connection_Layer
        ConnPool["Connection Pool"]
        ConnMgmt["Connection Management"]
        Pool["Pool Implementation"]
    end

    subgraph Protocol_Layer
        ProtoImpl["Protocol Implementation"]
        Reader["Protocol Reader"]
        Writer["Protocol Writer"]
    end

    subgraph Monitoring
        OpenTelemetry["OpenTelemetry Integration"]
        Prometheus["Prometheus Integration"]
    end

    %% Client Layer Relationships
    UniversalClient --> BaseClient
    ClusterClient --> BaseClient
    SentinelClient --> BaseClient
    RingClient --> BaseClient

    %% Command Processing Relationships
    BaseClient --> CmdInterface
    BaseClient --> Pipeline
    BaseClient --> Transaction
    BaseClient --> PubSub
    BaseClient --> Script
    CmdInterface --> CmdResults
    CmdInterface --> ErrorHandling

    %% Connection Layer Relationships
    BaseClient --> ConnPool
    ConnPool --> ConnMgmt
    ConnPool --> Pool

    %% Protocol Layer Relationships
    ConnMgmt --> ProtoImpl
    ProtoImpl --> Reader
    ProtoImpl --> Writer

    %% Monitoring Relationships
    BaseClient --> OpenTelemetry
    BaseClient --> Prometheus

    %% Styling
    classDef core fill:#2374ab
    classDef conn fill:#2a9d8f
    classDef proto fill:#e9c46a
    classDef feature fill:#f4a261
    classDef monitoring fill:#264653

    class BaseClient,UniversalClient,ClusterClient,SentinelClient,RingClient core
    class ConnPool,ConnMgmt,Pool conn
    class ProtoImpl,Reader,Writer proto
    class Pipeline,Transaction,PubSub,Script,CmdInterface,CmdResults,ErrorHandling feature
    class OpenTelemetry,Prometheus monitoring

    %% Click Events
    click BaseClient "https://github.com/redis/go-redis/blob/master/redis.go"
    click ClusterClient "https://github.com/redis/go-redis/blob/master/cluster_commands.go"
    click SentinelClient "https://github.com/redis/go-redis/blob/master/sentinel.go"
    click RingClient "https://github.com/redis/go-redis/blob/master/ring.go"
    click UniversalClient "https://github.com/redis/go-redis/blob/master/universal.go"
    click CmdInterface "https://github.com/redis/go-redis/blob/master/commands.go"
    click Pipeline "https://github.com/redis/go-redis/blob/master/pipeline.go"
    click Transaction "https://github.com/redis/go-redis/blob/master/tx.go"
    click PubSub "https://github.com/redis/go-redis/blob/master/pubsub.go"
    click Script "https://github.com/redis/go-redis/blob/master/script.go"
    click CmdResults "https://github.com/redis/go-redis/blob/master/result.go"
    click ErrorHandling "https://github.com/redis/go-redis/blob/master/error.go"
    click ConnPool "https://github.com/redis/go-redis/tree/master/internal/pool/"
    click ConnMgmt "https://github.com/redis/go-redis/blob/master/internal/pool/conn.go"
    click Pool "https://github.com/redis/go-redis/blob/master/internal/pool/pool.go"
    click ProtoImpl "https://github.com/redis/go-redis/tree/master/internal/proto/"
    click Reader "https://github.com/redis/go-redis/blob/master/internal/proto/reader.go"
    click Writer "https://github.com/redis/go-redis/blob/master/internal/proto/writer.go"
    click OpenTelemetry "https://github.com/redis/go-redis/tree/master/extra/redisotel/"
    click Prometheus "https://github.com/redis/go-redis/tree/master/extra/redisprometheus/"
```
