Implementation of Redis standalone database, redis Standalone Database

Source: Internet
Author: User
Tags redis server

Implementation of Redis standalone database, redis Standalone Database

Standalone Database implementation

Principle

The Redis server saves all databases in the db array of the redis. h/redisServer structure in the server status. Each item in the db array is a RedisDb structure, and each redisDb structure represents a database. During server initialization, the program determines the number of databases to be created based on the dbnum attribute of the server status. The value of the dbnum attribute is determined by the database option configured by the server. By default, the value of this option is 16, so the Redis server creates 16 databases by default.

Switch Database

By default, the target database of the Redis client is database 0, but the client can switch to the target database by executing the SELECT command.

Key Space

Each database is represented by a redis. h/redisDb structure. The dict Dictionary of the redisDb structure stores all key-value pairs in the database, and this dictionary is called a key space. When you use the Redis command to read and write the database, the server not only performs the specified read and write operations on the key space, but also performs some additional maintenance operations. For example, after reading a key, the server updates the last time the key was used. This value can be used to calculate the idle time of the key. You can run the OBJECT idletime command to view the idle time of the key.

Set the key survival time or expiration time EXPIRE/PEXPIRE key time

Redis expiration key deletion policy: inert deletion and Regular deletion.

Inert deletion: All Database read and write commands -- call the expireIfNeeded function -- determine whether the Input key has expired -- yes, the deletion key

Regular deletion: The activeExpireCycle function is called periodically. Each time the function runs, a certain number of random keys are retrieved from a certain number of databases for check and the expired keys are deleted. The global variable current_db records the current progress and calls it to process it at the next start.

Database notification

Database notification is a new feature of Redis2.8. It allows the client to subscribe to a given channel or mode to learn the changes in the database's middle keys and the execution of commands in the database.

Example: SUBSCRIBE_keyspace @ 0 _: message

RDB persistence

Because Redis is a memory database, it stores its database status in the memory. Redis provides the RDB persistence function, which can

Save the database status in the memory to the disk to avoid unexpected data loss. RDB persistence can be executed manually or regularly based on server configuration options. This function can save the database status at a certain time point to an RDB file. The RDB file generated by the RDB persistence function is a compressed binary file that restores the database status when the RDB file is generated. Because RDB files are stored in the hard disk, even if the Redis server process exits or even the computer running the Redis server is shut down, as long as the RDB file still exists, the Redis server can use it to restore the database status.

RDB File Creation and Loading

Both SAVE and BGSAVE can be used to generate the RDB file. The SAVE command will block the Redis server process until the RDB file is created. During the process blocking, the server cannot process any command requests.

The BGSAVE command derives a sub-process, and then the sub-process creates an RDB file in a complex manner. The server process continues to process the command request.

For different types of key-value pairs, the RDB file is saved in different ways.

AOF persistence

In addition to the RDB persistence function, Redis also provides the AOF persistence function. Unlike RDB persistence, which records the database status by saving the key-value pairs in the database, AOF persistence records the database status by saving the write commands executed by the Redis server.

Because AOF files are usually updated more frequently than RDB files

If the AOF persistence function is enabled on the server, the server will first use the AOF file to restore the database status;

The server uses the RDB file to restore the database status only when the AOF persistence function is disabled.

Redis> SET msg "hello"

Redis> SADDfruits "apple" "banana"

Apsaradb for redis> RPUSHnumbers 128 122 444

RDB persistently saves the database status by saving the key-value pairs of msg, fruits, and numbers to the RDB file, AOF persistently saves the database status by saving the SET, SADD, and RPUSH commands executed by the server to the AOF file.

To improve file writing efficiency, in the current operating system, when you call the write function to write some data to a file, the operating system usually temporarily stores the written data in a memory buffer, and writes the data in the buffer to the disk after the buffer space is filled or exceeds the specified time limit.

Although this method improves the efficiency, it also brings security issues to Data Writing, because if the computer is down, the data written in the memory buffer will be lost.

To this end, the system provides two synchronization functions: fsync and fdatasync, which can force the operating system to immediately write data in the buffer to the hard disk, so as to ensure the security of data writing.

The value of the appendfsync option configured on the server directly determines the efficiency and security of the AOF persistence function.

When the appendfsync value is always, the server writes all the content in the aof_buf cache to the AOF file in each event loop, and synchronizes the AOF file, which is the slowest but safest. Even if a fault stops, AOF persistence will only lose the command data generated in an event loop.

Everysec: the server writes all the content in the aof_buf cache to the AOF file in every event loop, and synchronizes the AOF file in the Child thread every second. High Efficiency: even if a fault occurs, the database only loses command data of one second.

No: in the no mode, the flushAppendOnlyFile call does not need to perform synchronization operations, so the AOF file write speed in this mode is always the fastest. However, because this mode will accumulate data written for a period of time in the system cache, the maximum synchronization duration of this mode is usually three.

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.