Analysis of the startup process of Redis server

Source: Internet
Author: User
Tags redis server

Reproduced in: http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/127.html?1455808771

This article will describe the startup process of Redis by analyzing the code, and by looking at the startup script of Redis, we learned that Redis started from the main method of REDIS.C. Redis boot can be divided into the following steps:

1. Initializing the REDIS server global configuration
2. Reset the server save parameter (detailed below) and load the configuration file
3. Initializing the server
4. Loading the database
5. Start Network Monitoring

One, initialize the Redis server global configuration . This step is primarily based on the static value set in Redis.h to initialize the Redis server configuration, which is the default configuration of the Redis server. Such as:

· Default timeout for TCP Port,redis client;
· Redis default number of databases;
· Redis Append parameter settings for persistent mode;
· Redis's supported settings for the default values of various data structures;
· Redis memory swap related settings;
· Redis Master & Slave related configurations;
· Redis Command table initialization.

Two, load the configuration file:

This step is to set up the Redis server by reading the configuration file, overwriting some of the default settings in the previous step. Open the downloaded Redis source code and we can see that there is a default profile redis.conf under its root directory. It is important to note that if you do not specify a profile when you start Redis, the Redis server will not load this default configuration file for configuration when it is started. And this default configuration file and the first step in the global default configuration is not the same, such as the Redis append mode of the data saving policy configuration, redis.conf the settings are:

Save 1-------in 15 minutes update
Save three------10 updates in 5 minutes
Save 10000---10,000 updates in 1 minutes.

The default default configuration in the previous step is true:

Save 60*60 1-------1 updates in one hours
Save------100 updates in 5 minutes
Save 10000---10,000 updates in 1 minutes.

So when we start Redis, if the default configuration does not meet the requirements, you need to indicate that the configuration file is configured.

Third, initialize the server:

Initializing the server is done in the Initserver () method, and the secondary method uses the parameters set in the previous two steps to further initialize the server:

• Create list#p# paging titles to maintain clients and slaves #e#
• Create a shared object. Redisobject This struct has a variable called RefCount, which is used for sharing. Redis objects currently Redis only support shared stringobject. Redis's shared objects have two analogies: the first Category: the various types of objects that Redis server needs to use frequently, such as the reply "rn" for Redis command, "+OKRN" or "-errrn" for Redis command. Objects such as the various operations in Redis are frequently used, so they are created when Redis is started, then shared with these objects, reducing time and space costs, and secondly, the shared object of the class is the stringobject corresponding to the number, such as: Set " Olylakers1 "1234; Set "Olylakes2" 1234; within Redis, the two keys "olylakers1" and "olylakers2" point to stringobject that are converted by the number 1234. This can save a lot of memory space under the massive data and the specific storage content. This parameter can be used to specify how many second class shared objects are created by the Redis boot, the default parameter is 10000, that is, the Strongobject value range created is between 0-9999 and Redis_shared_integers.

• Create Pub/sub Channel
• Initialize network monitoring EventLoop related content, such as eventloop,timeevent,fileevent, etc.
• Initializes the virtual memory-related Io/thread If the VM is turned on

Four, load the data:

Depending on the configuration, the Redis load data source is not the same, if the configuration file is set AppendOnly Yes (default is no), then load the data from AppendFile, and vice versa to load the data from Redisdb

• Load data from AppendFile: Let's take a look at AppendFile's content. The following record is extracted from Appendfile:set $9 olylakers $ oly. It is very obvious that appendfile is saving the various commands that Redis server receives, then loading data from AppendFile is the Redis server reading the records of these commands from the Appenfile, and then re-executing the commands again. It is important to note that if the VM is turned on, the swap operation may be involved when loading data from AppendFile.
• Load data from Redisdb: If AppendOnly is not turned on, you need to load the data from the DB file into memory with the following process:
1. Select DB by processing the Select command
2. Then read the key and value from the DB file
3. Check if key is out of date, skip this key if it expires, add the data to the dict of the corresponding DB if it does not expire
4. If the VM is turned on, the load data from the DB file may also involve a swap operation

Five, start Network monitoring:

Redis Network monitoring does not adopt libevent, etc., but the implementation of a simple set of opportunities for the event-driven API, specifically see AE.C.

Analysis of the startup process of Redis server

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.