Analysis of the startup process for Redis server

  • 2020-05-07 20:38:31
  • OfStack

This article introduces the Redis startup process by analyzing the code. By looking at the Redis startup script, you can see that Redis starts with the main method of Redis.c. Redis startup can be divided into the following steps:

1. Initialize the Redis server global configuration
2. Reset the server Save parameters (more details below) and load the configuration file
Initialize the server
Load the database
5. Start listening

1, initializes Redis server global configuration . This step is mainly to initialize the Redis server configuration according to the Static value set in Redis.h, which is the default configuration of Redis server. Such as:

· TCP Port, Redis Client default Timeout;
· default number of databases for Redis;
· parameter setting of Redis Append persistence mode;
· setting of default values for various data structures supported by Redis;
· Redis memory Swap related Settings;
· configuration related to Redis Master & Slave;
· Redis Command Table initialization.

  2, load configuration file:

This step sets the Redis server by reading the configuration file and overrides some of the default Settings for step 1. Opening the downloaded Redis source code, we can see that there is a default configuration file redis.conf in the root directory. Note that if the configuration file is not specified when Redis is started, the Redis server will not load the default configuration file for configuration at startup. Moreover, the default configuration file is not the same as the global default configuration in step 1. For example, the configuration of Redis's Append mode data saving policy, redis. The Settings in conf are:

save 900 1 - updates every 15 minutes
save 300 10-10 updates in 5 minutes
save 60 10000 -- 10,000 updates per minute.

The default default configuration in step 1 does:

save 60*60 1 -- updates once an hour
save 300 100 -- 100 updates in 5 minutes
save 60 10000 -- 10,000 updates per minute.

Therefore, when we start Redis, if the default configuration does not meet the requirements, we need to specify the configuration file for configuration.

3, initialize server:

The server initialization is done in the initServer() method, which further initializes the server with the parameters set in the previous two steps:

· create list to maintain clients and slaves
· create Shared objects. redisObject, struct has a variable called refcount, which is used for sharing. Redis objects currently only support sharing StringObject in Redis. There are two main analogies for Redis Shared objects: class 1: Redis server Redis Command separator "\r\n" for Redis command reply "+OK\r\n" or "-ERR \r\n" for Redis command for reply "+OK\r\n" or" -ERR \r\n" for Redis command. Second, the Shared object of class is StringObject corresponding to the number, such as: Set "olylakers1" 1234; Set "olylakes2" 1234; Within Redis, "olylakers1" and "olylakers2" both refer to StringObject converted from the number 1234. In this way, under the massive data and specific storage content, can save a lot of memory space. The parameter REDIS_SHARED_INTEGERS can be used to specify how many class 2 Shared objects are created when Redis is started. The default parameter is 10000, that is, the value of StrongObject is between 0 and 9999.

· create the Pub/Sub channel
· initiate network monitoring of EventLoop, such as eventLoop, timeEvent, fileEvent, etc
· if VM is turned on, the virtual memory-related IO/Thread is initialized

4, load data:

Depending on the configuration, Redis also loads data from a different source. If appendonly   yes (no by default) is set in the configuration file, then the data is loaded from appendfile and vice versa

· load data from appendfile: let's first look at 1 to see what appendfile is. The following record is taken from appendfile: SET $9 olylakers $3 oly. Obviously, appendfile stores all the commands received by redis server, so loading data from appendfile is redis server reads the records of these commands from appenfile, and then executes them again. Note that if VM is turned on, an swap operation may be involved when loading data from appendfile.
· load data from redisdb: if appendonly is not turned on, then data needs to be loaded into memory from db file. The process is:
1. Select DB by processing the select command
2. Then read key and value from db file
3. Check whether key is expired. If it is, skip the key. If it is not, put the data Add into dict of db
4. If VM is turned on, load data from db file may also involve swap operations

5, start network listening:

The network monitoring of Redis does not adopt libevent, but realizes a set of simple API driven by event, see ae.c for details.

Related articles: