Redis Learning Notes--rdb, AOF, and processing of expired keys when copying

Source: Internet
Author: User
Tags redis redis server

AOF, RDB, and replication functions to generate an RDB file for expired key processing

When you execute the Save command or the Bgsave command to create a new Rdb file, the program checks the keys in the database and the expired keys are not saved in the newly created RDB file.
For example, if the database contains three keys K1, K2, K3, and K2 has expired, when the Save command or Bgsave command is executed, the program will only save K1 and K3 data to the Rdb file, and K2 will be ignored.
Therefore, a database that contains an expiration key does not affect the generation of a new Rdb file.

Refer to the RDB.C function Rdbsave () function source code:

 /*Iterate this DB writing every entry * * traverses the database and writes data for each key-value pair*/         while(de = Dictnext (DI))! =NULL) {SDS KEYSTR=Dictgetkey (DE); RobJ Key,*o =Dictgetval (DE); Long Longexpire; //Create a Key object in the stack according to KeystrInitstaticstringobject (KEY,KEYSTR); //gets the expiration time of the keyexpire = Getexpire (db,&key); //Save key value pair data            if(Rdbsavekeyvaluepair (&rdb,&key,o,expire,now) = =-1)GotoWerr; }

The Rdbsavekeyvaluepair function is implemented as follows:
/*Save a Key-value pair, with expire time, type, key, value. * * writes keys, values, expiration times, and types of key-value pairs to the RDB. * * on ERROR-1 is returned. * * ERROR returned-1.  * On success if the key was actually saved 1 are returned, otherwise 0 * is returned (the key was already expired). * * Successfully saved returned 1, when the key has expired, return 0. */intRdbsavekeyvaluepair (Rio *rdb, RobJ *key, RobJ *Val,Long LongExpiretime,Long LongNow ) {    /*Save The expire time * * Save key expiration*/    if(Expiretime! =-1) {        /*If This key is already expired skip it * * Do not write to expired keys*/        if(Expiretime < now)return 0; if(Rdbsavetype (Rdb,redis_rdb_opcode_expiretime_ms) = =-1)return-1; if(Rdbsavemillisecondtime (rdb,expiretime) = =-1)return-1; }    /*save type, key, value * * Saves types, keys, values*/    if(Rdbsaveobjecttype (rdb,val) = =-1)return-1; if(Rdbsavestringobject (rdb,key) = =-1)return-1; if(Rdbsaveobject (rdb,val) = =-1)return-1; return 1;}

Loading RDB files

When the Redis server is started, the server will load the RDB file if the server has the RDB feature turned on:

    • If the server is running in primary server mode, when the Rdb file is loaded, the program checks the saved keys in the file, the non-expired keys are loaded into the database, and the expiration keys are ignored, so the expiration key does not affect the host server that loads the Rdb file;
    • If the server is running from server mode, all keys saved in the file, whether or not expired, will be loaded into the database when the Rdb file is loaded. However, because the master-slave server in the data synchronization, the database from the server will be emptied, so in general, the expiration key on the server loaded with the Rdb file will not be affected;

This part of the code can be viewed RDB.C in the Rdbload () function source code:

/*Check If the key already expired. This function is used if loading * an RDB file from disk, either at startup, or if an RDB was * receiv Ed from the master. In the latter case, the master was * responsible for key expiry.          If we would expire keys here, the * snapshot taken by the master could not be reflected on the slave. * * If the server is the primary node, * Then when the keys have expired, they are no longer associated to the database*/        if(Server.masterhost = = NULL && expiretime! =-1&& Expiretime <Now )            {Decrrefcount (key);            Decrrefcount (Val); //Skip over            Continue; }

AoF File Write

When the server is running in aof persistence mode, if a key in the database has expired, but it has not been lazily deleted or deleted periodically, then the AOF file will not have any effect because of this expiration key.

When the expiration key is lazily deleted or deleted periodically, the program appends (append) a del command to the aof file to explicitly record that the key has been deleted.
For example, if a client uses the get message command to attempt to access an expired message key, the server performs the following three actions:
1) Delete the message key from the database.
2) Append a del message command to the AoF file. (based on the added features of the aof file, aof only have this del operation when the client makes the request)
3) returns an empty reply to the client executing the GET command.

This is part of the use of the expireifneeded function in the lazy deletion policy in Redis. About the lazy deletion policy This section has been described in the Redis lazy removal policy article. So I won't go into this.

It is important to note that the expireifneeded function is called in the Db.c/lookupkeyread () function, and the Lookupkeyread function is used to take the value of key key in database db when performing a read operation.

Redis Learning Notes--rdb, AOF, and processing of expired keys when copying

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.