How Redis Starts: A Deep Dive into server.c Initialization
This article explains the step‑by‑step process Redis follows to launch, from executing the redis‑server binary, initializing the server struct, loading configuration files, setting up data structures, restoring persistence, to entering the event loop.
Assuming Redis is installed in /usr/local/, it can be started with /usr/local/bin/redis-server -c xxx.conf. The executable is built from server.c, so the startup sequence begins in the main function of that file.
The source code is available at https://github.com/hoohack/read-redis-src/blob/master/redis-4.0/src/server.c. Reading main reveals five major steps:
Initialize the redisServer structure.
Load parameters from the configuration file.
Initialize server internals.
Load persistence files to restore the database.
Start the event loop and begin listening for connections.
Initialize server structure
The initServerConfig function sets default values for many fields, such as server ID, default port (6379), LRU clock, replication settings, command table, and slow‑query parameters. It also records the executable path and command‑line arguments for later restarts:
struct redisServer server;
server.executable = getAbsolutePath(argv[0]);
server.exec_argv = zmalloc(sizeof(char*)*(argc+1));
server.exec_argv[argc] = NULL;
for (j = 0; j < argc; j++) server.exec_argv[j] = zstrdup(argv[j]);Load parameters from configuration file
If a configuration file is specified, loadServerConfig parses it, skipping empty lines and comments, splitting each line into key/value pairs, validating them, and applying the settings to the redisServer structure.
Initialize server data structures
The initServer function creates essential runtime structures, including process ID, client and replica lists, shared objects, the event loop, TCP listening socket, database instances, cron timer, AOF handling, slow‑query log, and background I/O modules.
Load persistence files
Depending on the persistence mode, Redis either restores data from an AOF file or loads an RDB snapshot, ensuring the in‑memory database reflects the last saved state.
Start listening for events
The main function registers beforeSleep and afterSleep callbacks, then calls aeMain to start the event loop. aeMain runs an infinite loop that repeatedly invokes the callbacks and processes events with
aeProcessEvents(eventLoop, AE_ALL_EVENTS|AE_CALL_AFTER_SLEEP):
void aeMain(aeEventLoop *eventLoop) {
eventLoop->stop = 0;
while (!eventLoop->stop) {
if (eventLoop->beforesleep != NULL)
eventLoop->beforesleep(eventLoop);
aeProcessEvents(eventLoop, AE_ALL_EVENTS|AE_CALL_AFTER_SLEEP);
}
}Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
