Start-Up Procedures
spt_init: This procedure initializes the process’s name and deep copy command-line arguments & environments.tzset.zmalloc_set_oom_handlerinit_genrand64with timestamp and pid number. Code Path:mt19937-64.ccrc64_init. Code Path:crc64.cumaskdictSetHashFunctionSeedwith a random 16-byte seed. Code Path:dict.cinitServerConfigACLInit. Code Path:acl.cmoduleInitModulesSystem. Code Path:module.cconnTypeInitialize. Code Path:connection.cinitSentinelConfigandinitSentinelif the server was started in sentinel mode. Code Path:sentinel.c- Run
redis_check_aof_mainorredis_check_rdb_mainand exit. Code Path:redis-check-aof.c,redis-check-rdb.c - Parse command-line options and
loadServerConfig. Code Path:config.c. sentinelCheckConfigFileif it’s in sentinel mode. Code Path:sentinel.c- Check whether the server is supervised by
upstartorsystemd. daemonizeif required.initServer. It initializes the globalredisServerstructure.createPidFileif required.redisSetProcTitleif required.checkTcpBacklogSettings.clusterInitif it’s in cluster mode. Code Path:cluster.cmoduleLoadFromQueue. Code Path:module.c.ACLLoadUsersAtStartup, Code path:acl.c.initListeners. Redis supports multiple types of listeners, and each one has its own accept handling logic. The listeners will be registered byaeCreateFileEventclusterInitListenersif it’s in cluster mode. Code Path:cluster.cbioInitinitializes the redis’s background I/O. Code Path:bio.cinitThreadedIOinitializes the threaded I/O. Code Path:networking.cset_jemalloc_bg_thread.- If it’s not sentinel mode:
aofLoadManifestFromDisk. Code Path:aof.cloadDataFromDisk. It will either load the AOF file withloadAppendOnlyFiles, or load the RDB file withrdbLoadand initialize the replication backlog withcreateReplicationBacklog.- RDB Code Path:
rdb.c
- RDB Code Path:
aofOpenIfNeededOnServerStartto open the AOF file on disk. Code Path:aof.caofDelHistoryFilesto clear thehistory_aof_list. Code Path:aof.cverifyClusterConfigWithDatacheck the cluster slots. Note that cluster mode only allows data in db 0. Code Path:cluster.c
- Else, do
sentinelIsRunning. redisSetCpuAffinitysetOOMScoreAdjwill update the value in/proc/self/oom_score_adjaeMainstarts the event loop.aeDeleteEventLoopafter the event loop is finished. Code Path:ae.c
Event Loop
server.el got initialized during initServer. The main event loop is created, serverCron and module_pipe events are registered.
beforeSleep and afterSleep are also configured on the event loop.
connSocketAcceptHandler
- Code Path:
socket.c
connSocketAcceptHandler is the accept handler for the default CT_Socket type.
anetTcpAcceptreturns a file descriptor for the accepted connection. Code Path:anet.cconnCreateAcceptedSocketreturns a newconnectionfor the accepted file descriptor.acceptCommonHandlerchecks the max connection number, creates a newclientand links it on the global client list withcreateClient, and executesconnSocketAcceptto fire the module events.createClientwill also registerreadQueryFromClientas the connection’s read handler, and register on the event loop.
readQueryFromClient
readQueryFromClient is the main handler to handle a pending query on a client.
What’s Next
- ACL implementation
- Event loop implementation
- AOF implementation
- Blocking operation.
- Clustering/replication/sentinel implementation
- Database operations (scan, query, etc.).
- Active memory defragmentation
- Memory eviction policies
- Key expiration implementation.
- Multi command implementation
- Pub/Sub
- Data structures implementations:
- Hash
- List
- Set
- Stream
- String
- ZSet
- Hyperloglog
- listpack and quicklist
- Radix tree
- Dynamic String
- etc.