Joke Collection Website - Talk about mood - Redis Interview Guide: How does Redis handle expired data?

Redis Interview Guide: How does Redis handle expired data?

This article talks about data processing after Redis key values ??expire, and talks about data cleaning under normal circumstances. However, interviewers often confuse the two concepts, so that they miss the expected work. . One of the responsibilities of our article is to help readers understand the difference between the two. I believe that after reading this article you will have an essential understanding of the concepts of the two.

Our interview question for this article is, how does Redis handle expired data?

An expiration dictionary is maintained in Redis, and all key values ????that have set expiration time will be stored in this dictionary. For example, when we use the command to set the expiration time, the command is as follows:

This command means that the data with the key value mykey:java will expire after 5 seconds, where ex is the abbreviation of expire, which means expiration or expiration.

In addition to the direct setting of the character type above, the expiration time can also be set directly using expire key seconds. The example is as follows:

The execution process of obtaining the key value is , when there is an access request for a key value, Redis will first determine whether the key value is in the expiration dictionary. If not, it means that the key value has no expiration time (never expires), and then the key value data can be returned normally; if this key If the value is in the expiration dictionary, it will be judged whether the current time is less than the expiration time. If it is less than the expiration time, it means that the key value has not expired and the data can be returned normally. Otherwise, it means that the data has expired. The key value will be deleted and nil will be returned to the client. Execution process As shown in the figure below:

This is the method process of key value data, and it is also the process of judging and deleting expired key values.

The interview questions in this article test your mastery of the expired deletion strategy of Redis. In Redis, in order to balance the space occupation and the execution efficiency of Redis, two deletion strategies are adopted. The above answer is not completely correct. , because he only answered one expiration key deletion strategy, and the interview questions related to this knowledge point include the following:

There are three common expiration strategies: