How huangz1990/redis-3.0-annotated works
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: redis.c Redis server · aof.c AOF · networking.c Networking · rdb.c RDB
Components 24
Source core
Server runtime, data model, persistence, replication, and built-in tools.
- Redis server · Main single-process server entry point and shared core definitions.
src/redis.c - Event loop · Portable event abstraction with OS-specific backends.
src/ae.c - Networking · Connection handling, protocol parsing, and reply writing.
src/networking.c - DB core · Keyspace selection, command dispatch support, and server-side execution state.
src/db.c - Commands · Data-type command implementations for strings, lists, sets, hashes, and sorted sets.
src/t_string.c - Objects · Shared object representation, encodings, and compact data structures.
src/object.c - RDB · Binary snapshot save/load format.
src/rdb.c - AOF · Append-only write logging and rewrite flow.
src/aof.c - RIO · Buffered and checksummed I/O used by persistence code.
src/rio.c - Replication · Master/slave synchronization, backlog, and write propagation.
src/replication.c - Sentinel · Monitoring and failover coordination for Redis instances.
src/sentinel.c - Cluster · Hash-slot coordination and node-to-node messaging.
src/cluster.c - Scripting · Embedded Lua execution integrated with normal command, persistence, and replication semantics.
src/scripting.c - Background I/O · Deferred jobs executed off the main event loop.
src/bio.c - Allocator · Allocation abstraction over the vendored allocator.
src/zmalloc.c - Redis CLI · Interactive command-line client.
src/redis-cli.c - Benchmark · Throughput and latency benchmarking executable.
src/redis-benchmark.c - Check tools · Persistence-file inspection utilities.
src/redis-check-dump.c
Dependencies
Vendored runtime and client support libraries.
Tests
Subsystem and deployment-mode test suites.
- Tests · Unit, integration, cluster, and Sentinel test harnesses.
tests/unit
Utilities
Release, install, and operational scripts.
- Scripts · Release, install, and maintenance scripts.
utils/mkrelease.sh
Connections 25
- 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.