redis/redis
redis-server
The main Redis daemon. Holds the dataset in memory, accepts client connections, executes commands, persists to disk, and replicates to followers.
Entry point
int main(int argc, char **argv) lives at the bottom of src/server.c (around line 7865). It does, in order:
- Initialise libraries (zmalloc OOM handler, time zone, RNG, CRC tables, dict hash seed).
- Detect Sentinel mode and
redis-check-rdb/redis-check-aofinvocation by examiningargv[0]. - Initialise the global server config (
initServerConfig), the ACL subsystem (ACLInit), the modules system (moduleInitModulesSystem), the connection types (connTypeInitialize), and per-key metadata (keyMetaInit). - Parse the configuration file and command-line overrides into the
redisServerstruct. - Daemonise if requested (
daemonize). - Load any modules listed in
loadmoduledirectives. - Initialise the server: bind sockets, set up timers, allocate the kvstore for each DB, prime the slot map for cluster mode, restore from RDB/AOF if a file is present.
- Enter
aeMain(server.el)— the event loop runs until shutdown.
After exit, aeMain returns and the process tears down.
Modes
The same binary runs in one of three modes:
| Mode | Triggered by | What changes |
|---|---|---|
| Standalone | Default | Single-DB or 16-DB; no cluster bus; no Sentinel commands. |
| Cluster | cluster-enabled yes |
A single DB; cluster bus on port + 10000; gossip; slot migration; failover. See Cluster. |
| Sentinel | redis-sentinel, --sentinel, or argv[0] ending in redis-sentinel |
Different command table; different cron; monitors a list of masters/replicas. See Sentinel. |
Source layout (the redis-server slice)
The redis-server build links a long list of .o files (see REDIS_SERVER_OBJ in src/Makefile). Conceptually they group into:
| Group | Files |
|---|---|
| Core orchestration | server.c, config.c, commands.c, commands.def, debug.c, monotonic.c, setcpuaffinity.c, setproctitle.c, syncio.c, syscheck.c, crash report (in debug.c) |
| Event loop | ae.c, ae_epoll.c, ae_kqueue.c, ae_evport.c, ae_select.c |
| Network | networking.c, connection.c, socket.c, unix.c, tls.c, anet.c |
| RESP | resp_parser.c, call_reply.c, logreqres.c |
| Data primitives | sds.c, dict.c, kvstore.c, quicklist.c, listpack.c, ziplist.c, zipmap.c, intset.c, rax.c, adlist.c, mstr.c, entry.c, keymeta.c, ebuckets.c, estore.c, fwtree.c, hotkeys.c, vector.c |
| Type implementations | t_string.c, t_list.c, t_set.c, t_zset.c, t_hash.c, t_stream.c, bitops.c, geo.c, geohash.c, geohash_helper.c, hyperloglog.c, gcra.c, stream.h, plus the vector-set sources |
| Persistence | rdb.c, aof.c, redis-check-rdb.c, redis-check-aof.c, chk.c, rio.c, lzf_c.c, lzf_d.c, crc16.c, crc64.c, crcspeed.c, crccombine.c, sha1.c, sha256.c, siphash.c, endianconv.c |
| Replication | replication.c |
| Cluster | cluster.c, cluster_legacy.c, cluster_asm.c, cluster_slot_stats.c |
| Sentinel | sentinel.c |
| Memory | zmalloc.c, lazyfree.c, evict.c, expire.c, defrag.c, childinfo.c, memtest.c, memory_prefetch.c |
| Pub/Sub & tracking | pubsub.c, notify.c, tracking.c, blocked.c, timeout.c |
| Scripting | eval.c, script.c, script_lua.c, function_lua.c, functions.c |
| Modules | module.c |
| ACL & auth | acl.c |
| Threads | bio.c, iothread.c, threads_mngr.c, eventnotifier.c |
| Misc | slowlog.c, latency.c, sparkline.c, lolwut.c, lolwut5.c, lolwut6.c, lolwut8.c, localtime.c, release.c, mt19937-64.c, rand.c, pqsort.c, sort.c, strl.c, util.c, multi.c, fast_float_strtod.c |
Server state: the redisServer struct
A single global struct redisServer server lives in src/server.c. It is the top of the runtime tree and holds:
- The event loop (
server.el). - The array of databases (
server.db[], lengthdbnum). - The list of connected clients (
server.clients). - Replication state (master link, replicas list, backlog).
- Cluster state pointer (
server.cluster). - Persistence configuration and child PIDs (
server.aof_state,server.rdb_child_pid, …). - Statistics (
server.stat_*). - Per-thread mainThread queues for IO threads.
Almost every command has access to it via server.<field>. The struct definition is in src/server.h (~3,000 lines around the struct redisServer definition).
Cron and beforeSleep
The main loop's two callbacks are:
serverCron(registered viaaeCreateTimeEvent) — firesserver.hztimes per second (default 10, adapts under load up to 500). Does active expiration, eviction, replication housekeeping, cluster cron, AOF rewrite scheduling, and dataset housekeeping.beforeSleep— called every iteration before the loop blocks inepoll_wait. Drains pending writes, flushes AOF, processes blocked clients, processes lazy-free queue messages, sends cluster bus packets.
The split between cron-time and beforeSleep work is significant. Anything that has to happen before the next epoll_wait (e.g. flush a pending reply) goes in beforeSleep. Anything that runs on a fixed cadence goes in serverCron.
Shutdown
SHUTDOWN [SAVE|NOSAVE] [NOW] [FORCE] [ABORT] invokes prepareForShutdown in src/server.c. It tries to:
- Stop accepting new clients.
- Wait for replicas to catch up (within
shutdown-timeout). - Save the RDB if requested.
- Close the AOF cleanly.
- Disconnect modules.
exit(0).
SHUTDOWN ABORT cancels a previously-issued SHUTDOWN if it is still pending replica acknowledgement.
Active contributors
Active contributors: antirez, Oran Agra, Pieter Noordhuis, Binbin, Yossi Gottlieb, debing.sun.
Where to start modifying
- Add a configuration option →
src/config.ctable +redis.confdoc. - Add a command → see the Development workflow. New entry in
src/commands/, regeneratesrc/commands.def, implement in the appropriatesrc/t_*.c. - Change cron behaviour →
serverCroninsrc/server.c. Be careful: any cron change has perf implications. - Tweak shutdown →
prepareForShutdownandfinishShutdowninsrc/server.c.
Related pages
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.