# huangz1990/redis-3.0-annotated architecture

> Architecture diagram of the GitHub repository huangz1990/redis-3.0-annotated, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/huangz1990/redis-3.0-annotated
- Repository: https://github.com/huangz1990/redis-3.0-annotated

## Overview

1. This repository is the Redis 3.0 server codebase: a single C monolith that builds several executables, centered on the `redis-server` process in `src/redis.c` and the shared core in `src/redis.h`.

2. The main runtime boundary is the event-driven server loop. `src/ae.c` is the event multiplexer abstraction, with OS-specific backends in `ae_epoll.c`, `ae_kqueue.c`, `ae_evport.c`, and `ae_select.c`. Networking is layered on top through `src/anet.c` and `src/networking.c`, which accept client connections, parse the Redis protocol, and send replies.

3. Client requests flow from the network layer into the command execution path, then into the database/object model. The command implementations are split by data type in `t_string.c`, `t_list.c`, `t_set.c`, `t_hash.c`, and `t_zset.c`, while shared object and encoding logic lives in `object.c`, `sds.c`, `intset.c`, `ziplist.c`, `zipmap.c`, and `dict.c`.

4. `src/db.c`, `src/blocked.c`, `src/multi.c`, `src/pubsub.c`, `src/notify.c`, `src/slowlog.c`, `src/sort.c`, and `src/bitops.c` represent the main in-server feature subsystems around core command execution. These modules handle database selection, blocking operations, transactions, pub/sub fanout, keyspace notifications, slow query tracking, sorting, and bit operations.

5. Persistence is a major architectural boundary. `src/rdb.c` implements snapshot-based RDB persistence, while `src/aof.c` implements append-only file logging and rewrite. Both use `src/rio.c` for buffered/checksummed I/O, and validation tools exist as separate executables in `src/redis-check-dump.c` and `src/redis-check-aof.c`.

6. Replication is integrated into the server rather than externalized. `src/replication.c` manages master/slave sync, backlog handling, and propagation of writes, with the same command stream used for normal clients being replayed to replicas.

7. High-availability and topology management are separate server modes. `src/sentinel.c` implements Sentinel for monitoring masters and reconfiguring replicas, while `src/cluster.c` and `src/cluster.h` implement Redis Cluster, including hash-slot coordination and node-to-node messaging. `src/redis-trib.rb` is the external cluster administration script.

8. Lua scripting is an embedded execution subsystem. `src/scripting.c` hosts the scripting integration, with the Lua runtime vendored under `deps/lua`, and script execution participates in normal command semantics and persistence/replication behavior.

9. Background and asynchronous work is isolated from the main event loop. `src/bio.c` runs deferred I/O jobs in worker threads, reducing latency impact on the single-threaded server path. Memory allocation is also abstracted through `src/zmalloc.c`, with `jemalloc` vendored under `deps/jemalloc` as the preferred allocator.

10. The in-memory data model is backed by utility structures and encodings optimized for small-object performance. `adlist.c`, `dict.c`, `intset.c`, `ziplist.c`, `zipmap.c`, `sds.c`, and `lzf_*` support the server’s compact representations and serialized forms. CRC/SHA helpers and endian conversion utilities support persistence and protocol-related correctness.

11. Several client-facing executables are built from the same source tree. `src/redis-cli.c` is the interactive client, `src/redis-benchmark.c` measures throughput, and `src/redis-check-*` inspect persistence files. `deps/linenoise` provides CLI line editing, and `deps/hiredis` is the C client library used by examples and tooling.

12. Testing is split by subsystem and deployment mode. The `tests/unit`, `tests/integration`, `tests/cluster`, and `tests/sentinel` trees exercise command semantics, persistence, replication, cluster, and Sentinel behavior, with Tcl-based harness code under `tests/support`. Top-level scripts such as `runtest`, `runtest-cluster`, and `runtest-sentinel` drive those suites.

13. Build and release tooling is intentionally simple and source-tree centric. The top-level `Makefile` and `src/Makefile` build the server and tools, while `utils/` contains release, install, and helper scripts. Configuration entry points are `redis.conf` for server mode and `sentinel.conf` for Sentinel mode.

14. A useful architecture diagram for this repo should show one central Redis server core with four major side subsystems around it: networking/event loop, persistence, replication/high availability, and scripting. Around those, the compact in-memory data structures, background I/O, allocator, and test/tooling executables form the supporting layers.

Main entry points: [`src/redis.c`](https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/redis.c) (Redis server), [`src/aof.c`](https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/aof.c) (AOF), [`src/networking.c`](https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/networking.c) (Networking), [`src/rdb.c`](https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/rdb.c) (RDB)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_src["Source core"]
  node_redis_server["Redis server<br/>server core<br/>[redis.c]"]
  node_event_loop["Event loop<br/>multiplexer<br/>[ae.c]"]
  node_networking["Networking<br/>client I/O<br/>[networking.c]"]
  node_db_core["DB core<br/>database core<br/>[db.c]"]
  node_commands["Commands<br/>cmd families<br/>[t_string.c]"]
  node_objects["Objects<br/>data model<br/>[object.c]"]
  node_persistence_rdb["RDB<br/>snapshot persistence<br/>[rdb.c]"]
  node_persistence_aof["AOF<br/>log persistence<br/>[aof.c]"]
  node_rio["RIO<br/>I/O layer<br/>[rio.c]"]
  node_replication["Replication<br/>sync/propagation<br/>[replication.c]"]
  node_sentinel["Sentinel<br/>ha mode<br/>[sentinel.c]"]
  node_cluster["Cluster<br/>sharding mode<br/>[cluster.c]"]
  node_scripting["Scripting<br/>lua runtime<br/>[scripting.c]"]
  node_bio["Background I/O<br/>worker threads<br/>[bio.c]"]
  node_allocator["Allocator<br/>memory alloc<br/>[zmalloc.c]"]
  node_cli["Redis CLI<br/>client tool<br/>[redis-cli.c]"]
  node_bench["Benchmark<br/>client tool<br/>[redis-benchmark.c]"]
  node_checks["Check tools<br/>validators<br/>[redis-check-dump.c]"]
end

subgraph group_deps["Dependencies"]
  node_lua_dep["Lua<br/>embedded runtime"]
  node_jemalloc_dep["Jemalloc<br/>allocator lib"]
  node_hiredis_dep["Hiredis<br/>client library"]
  node_linenoise_dep["Linenoise<br/>line editing"]
end

subgraph group_tests["Tests"]
  node_unit_tests["Tests<br/>tcl suites"]
end

subgraph group_utils["Utilities"]
  node_utils_scripts["Scripts<br/>ops tooling<br/>[mkrelease.sh]"]
end

node_redis_server -->|"drives"| node_event_loop
node_redis_server -->|"accepts I/O"| node_networking
node_redis_server -->|"executes"| node_db_core
node_networking -->|"registers events"| node_event_loop
node_networking -->|"dispatches"| node_commands
node_db_core -->|"stores data"| node_objects
node_commands -->|"uses"| node_objects
node_persistence_rdb -->|"uses"| node_rio
node_persistence_aof -->|"uses"| node_rio
node_redis_server -->|"persists"| node_persistence_rdb
node_redis_server -->|"persists"| node_persistence_aof
node_replication -->|"replays writes"| node_persistence_aof
node_redis_server -->|"replicates"| node_replication
node_sentinel -->|"monitors"| node_replication
node_cluster -->|"coordinates"| node_redis_server
node_scripting -->|"embeds"| node_lua_dep
node_redis_server -->|"executes scripts"| node_scripting
node_bio -->|"offloads work"| node_redis_server
node_allocator -->|"prefers"| node_jemalloc_dep
node_cli -->|"uses"| node_hiredis_dep
node_cli -->|"uses"| node_linenoise_dep
node_checks -->|"inspects"| node_persistence_rdb
node_checks -->|"inspects"| node_persistence_aof
node_unit_tests -->|"exercises"| node_redis_server
node_utils_scripts -->|"supports"| node_redis_server

click node_redis_server "https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/redis.c"
click node_event_loop "https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/ae.c"
click node_networking "https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/networking.c"
click node_db_core "https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/db.c"
click node_commands "https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/t_string.c"
click node_objects "https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/object.c"
click node_persistence_rdb "https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/rdb.c"
click node_persistence_aof "https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/aof.c"
click node_rio "https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/rio.c"
click node_replication "https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/replication.c"
click node_sentinel "https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/sentinel.c"
click node_cluster "https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/cluster.c"
click node_scripting "https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/scripting.c"
click node_bio "https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/bio.c"
click node_allocator "https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/zmalloc.c"
click node_cli "https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/redis-cli.c"
click node_bench "https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/redis-benchmark.c"
click node_checks "https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/redis-check-dump.c"
click node_lua_dep "https://github.com/huangz1990/redis-3.0-annotated/tree/unstable/deps/lua"
click node_jemalloc_dep "https://github.com/huangz1990/redis-3.0-annotated/tree/unstable/deps/jemalloc"
click node_hiredis_dep "https://github.com/huangz1990/redis-3.0-annotated/tree/unstable/deps/hiredis"
click node_linenoise_dep "https://github.com/huangz1990/redis-3.0-annotated/tree/unstable/deps/linenoise"
click node_unit_tests "https://github.com/huangz1990/redis-3.0-annotated/tree/unstable/tests/unit"
click node_utils_scripts "https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/utils/mkrelease.sh"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_redis_server,node_event_loop,node_networking,node_db_core,node_commands,node_objects,node_persistence_rdb,node_persistence_aof,node_rio,node_replication,node_sentinel,node_cluster,node_scripting,node_bio,node_allocator,node_cli,node_bench,node_checks toneBlue
class node_lua_dep,node_jemalloc_dep,node_hiredis_dep,node_linenoise_dep toneAmber
class node_unit_tests toneMint
class node_utils_scripts toneRose
```

## Components

### Source core

Server runtime, data model, persistence, replication, and built-in tools.

- [Redis server](https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/redis.c): server core · Main single-process server entry point and shared core definitions. · `src/redis.c`
- [Event loop](https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/ae.c): multiplexer · Portable event abstraction with OS-specific backends. · `src/ae.c`
- [Networking](https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/networking.c): client I/O · Connection handling, protocol parsing, and reply writing. · `src/networking.c`
- [DB core](https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/db.c): database core · Keyspace selection, command dispatch support, and server-side execution state. · `src/db.c`
- [Commands](https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/t_string.c): cmd families · Data-type command implementations for strings, lists, sets, hashes, and sorted sets. · `src/t_string.c`
- [Objects](https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/object.c): data model · Shared object representation, encodings, and compact data structures. · `src/object.c`
- [RDB](https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/rdb.c): snapshot persistence · Binary snapshot save/load format. · `src/rdb.c`
- [AOF](https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/aof.c): log persistence · Append-only write logging and rewrite flow. · `src/aof.c`
- [RIO](https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/rio.c): I/O layer · Buffered and checksummed I/O used by persistence code. · `src/rio.c`
- [Replication](https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/replication.c): sync/propagation · Master/slave synchronization, backlog, and write propagation. · `src/replication.c`
- [Sentinel](https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/sentinel.c): ha mode · Monitoring and failover coordination for Redis instances. · `src/sentinel.c`
- [Cluster](https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/cluster.c): sharding mode · Hash-slot coordination and node-to-node messaging. · `src/cluster.c`
- [Scripting](https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/scripting.c): lua runtime · Embedded Lua execution integrated with normal command, persistence, and replication semantics. · `src/scripting.c`
- [Background I/O](https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/bio.c): worker threads · Deferred jobs executed off the main event loop. · `src/bio.c`
- [Allocator](https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/zmalloc.c): memory alloc · Allocation abstraction over the vendored allocator. · `src/zmalloc.c`
- [Redis CLI](https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/redis-cli.c): client tool · Interactive command-line client. · `src/redis-cli.c`
- [Benchmark](https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/redis-benchmark.c): client tool · Throughput and latency benchmarking executable. · `src/redis-benchmark.c`
- [Check tools](https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/redis-check-dump.c): validators · Persistence-file inspection utilities. · `src/redis-check-dump.c`

### Dependencies

Vendored runtime and client support libraries.

- [Lua](https://github.com/huangz1990/redis-3.0-annotated/tree/unstable/deps/lua): embedded runtime · Vendored Lua interpreter and libraries. · `deps/lua`
- [Jemalloc](https://github.com/huangz1990/redis-3.0-annotated/tree/unstable/deps/jemalloc): allocator lib · Preferred vendored memory allocator. · `deps/jemalloc`
- [Hiredis](https://github.com/huangz1990/redis-3.0-annotated/tree/unstable/deps/hiredis): client library · C client library used by tooling and examples. · `deps/hiredis`
- [Linenoise](https://github.com/huangz1990/redis-3.0-annotated/tree/unstable/deps/linenoise): line editing · CLI line-editing support library. · `deps/linenoise`

### Tests

Subsystem and deployment-mode test suites.

- [Tests](https://github.com/huangz1990/redis-3.0-annotated/tree/unstable/tests/unit): tcl suites · Unit, integration, cluster, and Sentinel test harnesses. · `tests/unit`

### Utilities

Release, install, and operational scripts.

- [Scripts](https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/utils/mkrelease.sh): ops tooling · Release, install, and maintenance scripts. · `utils/mkrelease.sh`

## Connections

- Redis server → Event loop: drives · Server runtime is built around the event loop.
- Redis server → Networking: accepts I/O · Main server coordinates client networking.
- Redis server → DB core: executes · Incoming requests flow into database and command execution.
- Networking → Event loop: registers events · Networking uses the multiplexed event abstraction.
- Networking → Commands: dispatches · Parsed protocol commands are routed into command implementations.
- DB core → Objects: stores data · Database state is built on the shared object model.
- Commands → Objects: uses · Type-specific commands operate on shared encodings and data structures.
- RDB → RIO: uses · RDB save/load uses buffered checksummed I/O.
- AOF → RIO: uses · AOF writes and rewrite use the same I/O layer.
- Redis server → RDB: persists · Server state can be snapshotted to RDB.
- Redis server → AOF: persists · Server state can be appended to AOF.
- Replication → AOF: replays writes · The replication stream follows normal write propagation semantics.
- Redis server → Replication: replicates · Replication is integrated into the main server.
- Sentinel → Replication: monitors · Sentinel observes and reconfigures replicated deployments.
- Cluster → Redis server: coordinates · Cluster mode is a server-side topology and messaging layer.
- Scripting → Lua: embeds · Redis scripting hosts vendored Lua.
- Redis server → Scripting: executes scripts · Scripts participate in normal command and persistence semantics.
- Background I/O → Redis server: offloads work · Deferred operations are moved off the single-threaded main path.
- Allocator → Jemalloc: prefers · Memory allocation is abstracted over vendored jemalloc.
- Redis CLI → Hiredis: uses · The interactive client relies on the hiredis library.
- Redis CLI → Linenoise: uses · CLI line editing comes from linenoise.
- Check tools → RDB: inspects · RDB check utility validates dump files.
- Check tools → AOF: inspects · AOF check utility validates append-only files.
- Tests → Redis server: exercises · Test suites drive the server through its major modes.
- Scripts → Redis server: supports · Operational scripts assist build, release, and deployment tasks.
