# redis/rueidis architecture

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

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

## Overview

##### And illustrate the main components
- Core Client Engine – This is the primary package (client.go, binary.go, cluster.go, etc.) that manages connections, built-in retry logic, auto pipelining configuration, and context cancellation. Label it “Redis Client Engine.”
- Command Builder & Parser – Represent the fluent builder pattern for constructing Redis commands (client.B()) and subsequent parsing of responses. This includes the various built-in builder functions (e.g., for GET, SET, MGET, etc.) and helper functions for converting responses.
- Auto Pipelining & Connection Pooling – Show the mechanism that automatically groups non-blocking commands to reduce round trips. Note that some features allow switching to dedicated connection mode to support context cancellation and CAS transactions.
- Client-Side Caching – Outline the caching subsystem that supports both server-assisted client-side caching and the cache-aside pattern (see “rueidisaside”), including TTL management and cache hit detection.
- Pub/Sub Module – Include components that handle subscribing, receiving, and processing Pub/Sub messages (with dedicated connections and hook support).
- Lua Scripting and Advanced Commands – Add a module for executing Lua scripts safely (using EVALSHA/EVAL fallback) and handling arbitrary commands.
- Generic Object Mapping (OM)
- Compatibility adapter (rueidiscompat)
- Locking utilities (rueidislock)
- Hooks (rueidishook)
- Rate Limiter (rueidislimiter)
- Probabilistic Data Structures (rueidisprob)
- Testing & Mocking – Briefly show the mock directory and testing utilities that support behavior verification.

##### Relationships and interactions
- Command Builder as part of the client’s “input” layer, where commands flow into the client engine.
- That responses are parsed by the core parser that routes output to caller functions.
- For features like client-side caching, indicate that responses might first be intercepted by the caching layer (or, alternately, a cache invalidation notifier from Redis will update the client).
- Pub/Sub and Lua Scripting as side branches emanating from the client engine, clarifying that they use dedicated connection handling or alternative execution modes.
- Integration flows where external libraries (OpenTelemetry, etc.) hook into the client for logging/metrics.

##### Architectural patterns and design choices
- Builder Pattern – Emphasize the command builder design (fluent API style) that is used to construct Redis commands.
- Pipelining and Pooling – Annotate auto pipelining and dedicated connection modes (for CAS transactions, Pub/Sub) as key design patterns.
- Modularity – Show how the repository separates concerns (core functionalities in the root, extensions in subdirectories like rueidisaside, rueidiscompat, om, etc.).
- Caching & Retry Strategies – Note the use of client-side caching and automatic retry for read-only commands.

##### Incorporate relevant technologies and frameworks
- Golang (Golang’s standard libraries, sync.Pool for recycling commands, etc.)
- GitHub Actions and CircleCI for CI/CD (from the .github and .circleci directories)
- OpenTelemetry and testing tooling (codecov, etc.)
- Docker Compose for integration testing (docker-compose.yml)

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Core Components"
        commandBuilder["Command Builder & Parser"]:::core
        redisClientEngine["Redis Client Engine"]:::core
        autoPipelining["Auto Pipelining & Connection Pooling"]:::core
        commandBuilder -->|"buildsCommand"| redisClientEngine
        redisClientEngine -->|"managesPipelining"| autoPipelining
    end

    subgraph "Features"
        clientSideCaching["Client-Side Caching"]:::feature
        pubsubModule["Pub/Sub Module"]:::feature
        luaScripting["Lua Scripting & Advanced Commands"]:::feature
        redisClientEngine -->|"appliesCaching"| clientSideCaching
        redisClientEngine -->|"handlesPubSub"| pubsubModule
        redisClientEngine -->|"executesScripts"| luaScripting
    end

    subgraph "Extension Modules"
        genericOM["Generic Object Mapping (OM)"]:::extension
        compatibilityAdapter["Compatibility Adapter"]:::extension
        lockingUtilities["Locking Utilities"]:::extension
        hooks["Hooks"]:::extension
        rateLimiter["Rate Limiter"]:::extension
        probDataStructures["Probabilistic Data Structures"]:::extension
        redisClientEngine -->|"integratesOM"| genericOM
        redisClientEngine -->|"integratesAdapter"| compatibilityAdapter
        redisClientEngine -->|"integratesLocking"| lockingUtilities
        redisClientEngine -->|"integratesHooks"| hooks
        redisClientEngine -->|"integratesLimiter"| rateLimiter
        redisClientEngine -->|"integratesProbDS"| probDataStructures
    end

    subgraph "External Communication"
        redisServer["Redis Server"]:::external
        externalIntegrations["OpenTelemetry & Metrics"]:::external
        redisClientEngine -->|"sendsCommands"| redisServer
        redisClientEngine -->|"reportsMetrics"| externalIntegrations
    end

    subgraph "Testing & DevOps"
        testingMocking["Testing & Mocking"]:::devops
        ciCd["CI/CD Pipeline"]:::devops
        redisClientEngine -->|"validatedBy"| testingMocking
        testingMocking -->|"runsIn"| ciCd
    end

    %% Click Events
    click redisClientEngine "https://github.com/redis/rueidis/blob/main/client.go"
    click commandBuilder "https://github.com/redis/rueidis/blob/main/internal/cmds/builder.go"
    click autoPipelining "https://github.com/redis/rueidis/blob/main/pipe.go"
    click clientSideCaching "https://github.com/redis/rueidis/blob/main/cache.go"
    click pubsubModule "https://github.com/redis/rueidis/blob/main/pubsub.go"
    click luaScripting "https://github.com/redis/rueidis/blob/main/lua.go"
    click genericOM "https://github.com/redis/rueidis/blob/main/om/conv.go"
    click compatibilityAdapter "https://github.com/redis/rueidis/blob/main/rueidiscompat/adapter.go"
    click lockingUtilities "https://github.com/redis/rueidis/blob/main/rueidislock/lock.go"
    click hooks "https://github.com/redis/rueidis/blob/main/rueidishook/hook.go"
    click rateLimiter "https://github.com/redis/rueidis/blob/main/rueidislimiter/limit.go"
    click probDataStructures "https://github.com/redis/rueidis/blob/main/rueidisprob/bloomfilter.go"
    click testingMocking "https://github.com/redis/rueidis/blob/main/mock/client.go"

    %% Styles
    classDef core fill:#b3d9ff,stroke:#333,stroke-width:2px;
    classDef feature fill:#ffffcc,stroke:#333,stroke-width:2px;
    classDef extension fill:#ccffcc,stroke:#333,stroke-width:2px;
    classDef external fill:#ffcc99,stroke:#333,stroke-width:2px;
    classDef devops fill:#e5ccff,stroke:#333,stroke-width:2px;
```
