# antirez/redis architecture

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

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

## Overview

##### Project type and purpose
- This is Redis: a high-performance, in-memory data structure server (NoSQL database, cache, message broker, vector store).
- Written in C, it exposes a text-based RESP protocol to clients, supports modules (extensions), clustering, replication, and sentinel for high availability.

##### File-structure analysis
- Top-level directories:
- src/             ← core server code (event loop, command dispatch, data structures, persistence, replication, cluster, sentinel)
- deps/            ← bundled third-party libraries (jemalloc, lua, hiredis, linenoise, fpconv, hdr_histogram, etc.)
- modules/         ← Redis modules (RedisJSON, RedisGraph, RedisBloom, vector-sets, etc.) built via the modules API
- tests/           ← tests for core, cluster, sentinel, modules (tcl, C, Python)
- utils/           ← scripts and tools (release, building, cluster, schema linter, etc.)
- src/commands/    ← machine-generated JSON specs for every Redis command
- src/ae.c/.h      ← event-driven reactor (select/epoll/kqueue)
- src/thread_mngr  ← optional I/O threads manager
- src/rdb.c/aof.c  ← persistence engines (RDB snapshots, AOF)
- src/replication.c, cluster.c, sentinel.c ← HA and sharding
- Makefile, redis.conf, redis-full.conf ← build and deployment config

##### Key insights from README
- Redis keeps data in memory, supports modules, provides multiple data structures & probabilistic structures, clustering, sentinel, and can be built with TLS and modules support.
- The build instructions stress feature flags (BUILD_WITH_MODULES, BUILD_TLS) and show how core, deps, and modules interoperate.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client & Admin Tools
    subgraph "Client & Admin Tools"
        CLI["redis-cli"]:::external
        HiredisCLI["Hiredis Lib"]:::external
        RBM["redis-benchmark"]:::external
        RCAOF["redis-check-aof"]:::external
        RCRDB["redis-check-rdb"]:::external
    end

    %% Core Server
    subgraph "Core Server" 
        subgraph "Network & Event"
            NET["Network Layer / RESP Parser"]:::network
            AE["Event Loop (ae.*)"]:::event
            THREAD["I/O Thread Manager"]:::event
        end
        CMD["Command Dispatcher"]:::command
        ENGINE["Core Data-Structure Engine"]:::core
        LUA["Lua Scripting Engine"]:::ext
        MODAPI["Modules API / Loader"]:::ext
    end

    %% Persistence Layer
    subgraph "Persistence"
        RDB["RDB Snapshot"]:::persistence
        AOF["AOF Append-Only File"]:::persistence
    end

    %% High Availability
    subgraph "High Availability"
        REPL["Replication Subsystem"]:::ha
        CLUST["Cluster Manager"]:::ha
        SENT["Sentinel Process"]:::ha
    end

    %% Dependencies
    subgraph "Dependencies"
        JM["Jemalloc"]:::deps
        LUADEP["Lua (embedded)"]:::deps
        HREDISDEP["Hiredis (client lib)"]:::deps
        LINO["Linenoise"]:::deps
    end

    %% Utilities & Scripts
    UT["Utilities & Scripts"]:::external

    %% Connections
    CLI -->|"RESP/TCP"| NET
    HiredisCLI -->|"RESP API"| NET
    RBM -->|"RESP/TCP"| NET
    RCAOF -->|"RESP/TCP"| NET
    RCRDB -->|"RESP/TCP"| NET
    UT -->|"RESP/TCP"| NET

    NET -->|"I/O events"| AE
    AE -->|"dispatch"| CMD
    THREAD --> AE
    CMD -->|"execute"| ENGINE

    ENGINE -->|"fork/write"| RDB
    ENGINE -->|"append"| AOF

    ENGINE -->|"sync"| REPL
    REPL -->|"apply"| ENGINE

    ENGINE -->|"route slots/gossip"| CLUST
    CLUST -->|"redirect"| ENGINE

    ENGINE -->|"call API"| MODAPI
    MODAPI -->|"invoke"| ENGINE

    ENGINE -->|"eval script"| LUA
    LUA -->|"return"| ENGINE

    SENT -->|"health checks"| REPL
    SENT -->|"monitor/gossip"| CLUST

    %% Click Events
    click CLI "https://github.com/antirez/redis/blob/unstable/src/redis-cli.c"
    click HiredisCLI "https://github.com/antirez/redis/tree/unstable/deps/hiredis/"
    click RBM "https://github.com/antirez/redis/blob/unstable/src/redis-benchmark.c"
    click RCAOF "https://github.com/antirez/redis/blob/unstable/src/redis-check-aof.c"
    click RCRDB "https://github.com/antirez/redis/blob/unstable/src/redis-check-rdb.c"

    click NET "https://github.com/antirez/redis/blob/unstable/src/anet.c"
    click NET "https://github.com/antirez/redis/blob/unstable/src/anet.h"
    click NET "https://github.com/antirez/redis/blob/unstable/src/resp_parser.c"
    click NET "https://github.com/antirez/redis/blob/unstable/src/resp_parser.h"

    click AE "https://github.com/antirez/redis/blob/unstable/src/ae.c"
    click AE "https://github.com/antirez/redis/blob/unstable/src/ae.h"
    click AE "https://github.com/antirez/redis/blob/unstable/src/ae_epoll.c"
    click AE "https://github.com/antirez/redis/blob/unstable/src/ae_select.c"
    click AE "https://github.com/antirez/redis/blob/unstable/src/ae_kqueue.c"
    click AE "https://github.com/antirez/redis/blob/unstable/src/ae_evport.c"

    click THREAD "https://github.com/antirez/redis/blob/unstable/src/threads_mngr.c"
    click THREAD "https://github.com/antirez/redis/blob/unstable/src/threads_mngr.h"

    click CMD "https://github.com/antirez/redis/blob/unstable/src/commands.c"
    click CMD "https://github.com/antirez/redis/blob/unstable/src/commands.h"
    click CMD "https://github.com/antirez/redis/blob/unstable/src/commands.def"
    click CMD "https://github.com/antirez/redis/tree/unstable/src/commands/"

    click ENGINE "https://github.com/antirez/redis/blob/unstable/src/db.c"
    click ENGINE "https://github.com/antirez/redis/blob/unstable/src/kvstore.c"
    click ENGINE "https://github.com/antirez/redis/blob/unstable/src/quicklist.c"
    click ENGINE "https://github.com/antirez/redis/blob/unstable/src/quicklist.h"
    click ENGINE "https://github.com/antirez/redis/blob/unstable/src/listpack.c"
    click ENGINE "https://github.com/antirez/redis/blob/unstable/src/listpack.h"
    click ENGINE "https://github.com/antirez/redis/blob/unstable/src/rax.c"
    click ENGINE "https://github.com/antirez/redis/blob/unstable/src/rax.h"

    click LUA "https://github.com/antirez/redis/tree/unstable/deps/lua/"
    click LUA "https://github.com/antirez/redis/blob/unstable/src/script.c"
    click LUA "https://github.com/antirez/redis/blob/unstable/src/script.h"
    click LUA "https://github.com/antirez/redis/blob/unstable/src/script_lua.c"
    click LUA "https://github.com/antirez/redis/blob/unstable/src/script_lua.h"

    click MODAPI "https://github.com/antirez/redis/blob/unstable/src/redismodule.h"
    click MODAPI "https://github.com/antirez/redis/blob/unstable/src/module.c"
    click MODAPI "https://github.com/antirez/redis/tree/unstable/src/modules/"

    click RDB "https://github.com/antirez/redis/blob/unstable/src/rdb.c"
    click RDB "https://github.com/antirez/redis/blob/unstable/src/rdb.h"
    click AOF "https://github.com/antirez/redis/blob/unstable/src/aof.c"

    click REPL "https://github.com/antirez/redis/blob/unstable/src/replication.c"
    click CLUST "https://github.com/antirez/redis/blob/unstable/src/cluster.c"
    click CLUST "https://github.com/antirez/redis/blob/unstable/src/cluster.h"
    click CLUST "https://github.com/antirez/redis/blob/unstable/src/cluster_legacy.c"
    click CLUST "https://github.com/antirez/redis/blob/unstable/src/cluster_legacy.h"
    click SENT "https://github.com/antirez/redis/blob/unstable/src/sentinel.c"

    click JM "https://github.com/antirez/redis/tree/unstable/deps/jemalloc/"
    click LUADEP "https://github.com/antirez/redis/tree/unstable/deps/lua/"
    click HREDISDEP "https://github.com/antirez/redis/tree/unstable/deps/hiredis/"
    click LINO "https://github.com/antirez/redis/tree/unstable/deps/linenoise/"

    click UT "https://github.com/antirez/redis/tree/unstable/utils/"

    %% Styles
    classDef external fill:#BBDEFB,stroke:#1E88E5,color:#0D47A1
    classDef network fill:#C8E6C9,stroke:#2E7D32,color:#1B5E20
    classDef event fill:#C8E6C9,stroke:#388E3C,color:#1B5E20
    classDef command fill:#A5D6A7,stroke:#388E3C,color:#1B5E20
    classDef core fill:#C5E1A5,stroke:#558B2F,color:#33691E
    classDef persistence fill:#FFF9C4,stroke:#F9A825,color:#F57F17
    classDef ha fill:#FFE0B2,stroke:#FB8C00,color:#E65100
    classDef ext fill:#E1BEE7,stroke:#8E24AA,color:#4A148C
    classDef deps fill:#EEEEEE,stroke:#9E9E9E,color:#424242
```
