This page introduces the Redis codebase, providing a technical summary of its architecture, core components, and the internal mechanisms that enable it to function as a high-performance in-memory data structure store. It covers foundational systems—such as the event loop and networking—as well as advanced features introduced in Redis 8.x, including Vector Sets, GCRA rate limiting, I/O threading optimizations, the Array data type (8.8), and Redis 8.10 additions like compact hashes and the node-side backup system.
Redis is an in-memory data structure server. All data is stored in RAM for sub-millisecond access latency, with optional durability provided through RDB point-in-time snapshots and an Append-Only File (AOF). While historically single-threaded for command execution, modern Redis utilizes multi-threaded I/O and memory prefetching to maximize throughput.
Core Capabilities and 8.x/8.10 Enhancements:
| Capability | Implementation / New in 8.x & 8.10 |
|---|---|
| Vector Sets | Similarity search using HNSW (Hierarchical Navigable Small World) graphs, supporting FP32/Q8/BIN quantization. |
| Array Data Type | New sparse array (Redis 8.8) with 64-bit index space, implemented in sparsearray.c src/server.h25 Supports aggregate operations (AROP) and regex search (ARGREP). |
| Compact Hashes | Redis 8.10 introduces the Hash Template Registry (OBJ_ENCODING_TMPL_LP) and HIMPORT for bulk insertion into memory-efficient compact hashes src/rdb.c84 |
| Rate Limiting | Native GCRA (Generic Cell Rate Algorithm) implementation for sophisticated traffic shaping src/debug.c126-134 |
| Streams (8.10) | New MAXCOUNT and MAXSIZE arguments for XREAD and XREADGROUP to cap cumulative reply entries and size. New XNACK command to release claimed messages. |
| Persistence (8.10) | New BACKUP command system (START, SEAL, STATUS, LIST, ABORT, CLEANUP) for node-side backup based on multi-part AOF src/aof.c43-72 |
| Data Structures | Strings, Lists, Sets, Sorted Sets, Hashes, Streams, HyperLogLog, Bitmaps, and Geospatial indices src/server.h15-68 |
| I/O Threading | Independent IOThreads handle reading queries and writing replies independently from the main thread src/networking.c140-142 |
Sources: src/server.h15-68 src/networking.c121-160 src/db.c35-47 src/aof.c43-72 src/rdb.c97-105 src/debug.c126-134 src/server.h25
The server entry point is main() in src/server.c which initializes the global struct redisServer server src/server.c85 loads configuration via config.c src/config.c1-15 and starts the event loop.
Overall System Architecture and Code Entities
The architecture separates networking concerns from execution logic. The ae.c event loop drives the system src/server.h50 while networking.c manages client structures src/networking.c121-122 Command execution is handled in server.c, which interacts with the db.c layer for data manipulation. Memory prefetching src/server.h69 optimizes batched command execution by loading data into cache before the main logic hits.
Sources: src/server.c81-90 src/server.h50-70 src/networking.c121-150 src/db.c15-30 src/memory_prefetch.h1-20 src/aof.c43-55 src/server.h25 src/rdb.c84
Redis state is encapsulated in a few primary structures. The most critical is the global server variable of type struct redisServer src/server.c85
Core Data Model and Code Associations
The redisServer struct holds the entire state of the instance. The redisDb struct manages individual databases, utilizing kvstore for efficient slot-based key management src/server.h55 The client struct src/networking.c121-123 tracks connection state, query buffers, and parsed arguments. Data is stored as kvobj (a specialized robj), which supports embedded keys and metadata bits for expiration and other attributes src/db.c121-147 The new Array data type is represented by a sparsearray struct, pointed to by the ptr field of a kvobj when its type is OBJ_ARRAY src/server.h25 src/redismodule.h92
Sources: src/server.h15-100 src/server.c85 src/networking.c121-140 src/db.c121-147 src/server.h25 src/redismodule.h92
Commands undergo a structured pipeline from the network socket to the data store.
Command Processing Pipeline
ae.c event loop detects data on a socket src/server.h50readQueryFromClient() in src/networking.c133 reads data into the querybuf.argv and argc within the client structure src/networking.c169-172lookupCommand() finds the redisCommand implementation. The isCommandReusable function src/server.c101-112 optimizes this by checking if the previous command can be reused.processCommand() performs ACL checks, memory limits, and cluster redirection.memory_prefetch.c is used to load data into CPU cache before execution src/server.h69proc function is called.addReply() and sent to the client, potentially using IOThreads src/networking.c141-143Sources: src/networking.c121-150 src/server.c101-112 src/server.h50-70
| Component | Files | Role |
|---|---|---|
| Server Core | server.c, server.h | Global state, initialization, and main command dispatcher. |
| Event Loop | ae.c, ae.h | High-performance asynchronous event notification library. |
| Networking | networking.c, anet.c, connection.c | Client management, RESP parsing, and transport abstraction. |
| Storage Engine | db.c, kvstore.c, dict.c | Key-value store logic, sharding, and hash table implementation. |
| Persistence | rdb.c, aof.c | Disk serialization (RDB) and write-ahead logging (AOF). Includes 8.10 BACKUP system. |
| Data Types | object.c, t_string.c, t_hash.c, sparsearray.c | Object management and core data structure implementations, including the new Array type. |
| 8.x Modules | module.c, redismodule.h | Extension system for RediSearch, RedisJSON, etc. |
Sources: src/server.c1-35 src/networking.c1-25 src/db.c1-25 src/rdb.c1-25 src/aof.c1-15 src/module.c1-80 src/server.h25
Refresh this wiki
This wiki was recently refreshed. Please wait 4 days to refresh again.