Analysis of startup process of Redis server _redis

Source: Internet
Author: User
Tags data structures redis redis server

This article will analyze the code to introduce the Redis startup process by looking at the Redis startup script and learning that Redis started with the main method of REDIS.C. Redis startup can be divided into the following steps:

1. Initialize 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

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

· Default timeout of TCP Port,redis client;
· Redis The default number of databases;
· The parameter setting of Redis Append mode;
· The setting of default values of various data structures supported by Redis;
· Redis memory swap related settings;
· Redis Master & Slave related configuration;
· Redis Command table initialization.

Second, 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. To open the downloaded Redis source code, we can see that there is a default profile redis.conf under its root directory. Note that if you do not specify a profile when you start Redis, the Redis server does not load this default configuration file when it is started. And this default configuration file and the global default configuration of the first step is not the same, such as for the Redis append mode of the data Preservation policy configuration, redis.conf inside the settings are:

Save 900 1-------15 minutes update
Save------10 updates in 5 minutes
Save 10000---Updated 10,000 times in 1 minutes.

The default configuration in the previous step is indeed:

Save 60*60 1-------One hours Update 1 times
Save------100 updates in 5 minutes
Save 10000---Updated 10,000 times 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:

The initialization 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 a list to maintain clients and slaves
• Create shared objects. Redisobject This struct has a variable called RefCount, which is used to implement the shared. Redis objects currently Redis only support shared stringobject. There are two great analogies to Redis's shared objects: the first Category: Redis server operations require a variety of objects that are frequently used, such as the Redis command's separator "\ r \ n" for reply "+ok\r\n" or "-err" for Redis command. \ r \ n "objects, because the various operations in Redis such objects to be used frequently, so the start of the Redis to create, and then share these objects, reduce time and space costs; second, 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 converted from the number 1234. This can save a lot of memory space under the mass data and specific storage content. You can use this parameter to specify how many second-class shared objects are created when Redis is started, the default parameter is 10000, that is, the Strongobject value created is between 0-9999 and Redis_shared_integers.

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

Four, load data:

Depending on the configuration, the source of the Redis load data is not the same, if AppendOnly Yes is set in the configuration file (the default is no), then the data is loaded from the appendfile and the data is loaded from REDISDB instead

• Load data from AppendFile: Let's take a look at what AppendFile is about. The following record is extracted from Appendfile:set $ olylakers $ oly. It is very obvious that AppendFile saves the various commands that Redis server receives, then loading the data from AppendFile is Redis server reads the records from the Appenfile and then executes the commands again. Note that if you turn on the VM, you might want to involve swap operations when loading data from AppendFile.
• Load data from Redisdb: If AppendOnly is not turned on, then you need to load data from DB file into memory, and the process is:
1. Select DB by processing the Select command
2. Then read key and value from DB file
3. Check the key is expired, if the expiration of the skip this key, if not expired, the data add to the corresponding DB Dict
4. If the VM is turned on, the load data from DB file may also involve a swap operation

Five, start Network monitoring:

Redis Network monitoring does not use libevent, but its own implementation of a set of simple opportunity event-driven API, specifically see AE.C.

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.