Deep understanding of the failure principle and implementation mechanism of Redis primary key

Source: Internet
Author: User
Tags redis

http://blog.jobbole.com/71095/

Source: Liang Xijian's blog (@ Liang Xijian)

For cache invalidation, different cache has different processing mechanism, it can be said that there is a small difference in Datong, the author through the Redis documents and related source of the careful reading, for everyone to detail the Redis cache expiration/failure mechanism related technical principles and implementation details.

Here is the author's original text:

As an important mechanism for periodically cleaning up invalid data, primary key failures exist in most cache systems, and Redis is no exception. Among the many commands provided by Redis, EXPIRE, Expireat, Pexpire, Pexpireat, and Setex and Psetex can be used to set a Key-value pair's expiration time, and a key-value pair once associated with an expiration time will automatically be deleted (or become inaccessible more accurately) after expiration. It can be said that the concept of primary key failure is relatively easy to understand, but in the specific implementation of the Redis is what? Recently, Benbow the main key failure mechanism of Redis produced a few questions, and based on these questions to carefully explore, and now summed up the following, in order to treat you spectators. one, the control of the time of failure

In addition to calling the persist command, there is no other case where the expiration time of a primary key is revoked. The answer is yes. First, when a primary key is deleted via the DEL command, the expiration time will naturally be revoked (this is not nonsense, haha). Second, when a primary key with an expiration time is overwritten, the failure time of the primary key is also revoked (which seems to be nonsense, haha). It should be noted, however, that the primary key is overwritten by the update, instead of the value of the primary key being overwritten by the update, so that SET, MSET, or getset may cause the primary key to be overwritten by the update, while the values of the primary key are updated, such as INCR, DECR, Lpush, Hset, etc. This type of operation does not touch the failure time of the primary key. In addition, there is a special command is RENAME, when we use RENAME to rename a primary key, the previous associated failure time is automatically passed to the new primary key, but if a primary key is overwritten by RENAME (such as the primary key hello may be command RENAME World Hello), the expiration time of the overridden primary key is automatically revoked, and the new primary key continues to retain the original primary key's characteristics. II. Internal realization of failure

How the primary key invalidation in Redis is implemented, that is, how the failed primary key is deleted. In fact, there are two main ways that Redis can delete a failed primary key: The negative method (passive way), if it is found to be invalid when the primary key is accessed, then delete the active method (active way) and periodically select a part of the failed primary key from the primary key that set the expiration time to delete internal representation of the failure

Let's go through the code to explore the implementation of both approaches, but before we do, let's look at how Redis manages and maintains the primary key (note: The source code in this blog post is all from Redis-2.6.12).

"Code Snippet I" gives a structure definition of a database in Redis, which is a pointer to a dictionary in addition to the ID, where we only look at Dict and expries, which is used to maintain all key-value pairs contained in a REDIS database (its structure can be understood Is the Dict[key]:value, which is the mapping between the primary key and the value, and the latter is used to maintain the primary key (whose structure can be understood as expires[key]:timeout, which is the mapping of the primary key to the expiration time) in a Redis database. When we use the Setex and Psetex commands to insert data into the system, Redis first adds the key and Value to the Dict dictionary table, and then adds the key and the expiration time to the Expires dictionary table. When we use the EXPIRE, Expireat, Pexpire, and Pexpireat commands to set the expiration time of a primary key, Redis first looks in the dictionary table of Dict to see if the primary key to be set exists, and if it exists, adds the primary key and the expiration time to the expires This dictionary table. Simply put, the primary key that sets the expiration time and the specific expiration time are all maintained in the Expires dictionary table.

"Code Snippet One" C

1 2 3 4 5 6 7 8 typedef struct REDISDB {dict * dict;      Dict * Expires;      Dict * Blocking_keys;      Dict * Ready_keys;      Dict * Watched_keys; int id; }
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.