We have learned that Redis is an in-memory database, where data in Redis is stored in memory in the form of Key-value, which is maintained and managed by a Redis server, where memory is precious, The unwanted data or the useless data that has been used is not allowed to exist in memory for a long time, after all, we still need to build a conservation-minded society. So we can weigh the limited memory and the growing data by giving the key a time-to-live or an expiration time, the command to set the expiration time is: "Expire key time length (seconds)" or "Pexpire key time Length (milliseconds)" can also pass "Expireat key Point-in-time (Unix timestamp), you can see how much time is left for this key through the TTL command or the Pttl command.
The expiration time of the key in the database is saved in a dictionary, which is mentioned in the Redis_ dictionary, this article mainly explains the problem is expired key deletion, an expired key will certainly not let it persist in memory, it must be deleted, building a conservation-minded society, then when to delete it? There are three different deletion strategies, which we can easily understand by simply explaining them.
1, regular deletion
What is a timed delete? As the name implies, is that I set a time, the point on the deletion, what are the benefits of memory is the most friendly, you can ensure that the expiration key will be removed as soon as possible, the memory is released early, but the coin has two sides ah, memory-friendly, cup time, the point on the deletion of the point on the delete, Light do delete expired key operation, unrealistic, and the server also to create a large number of timers, to achieve scheduled deletion, if the memory is not missing, there is free memory, and this time there are a large number of command requests waiting for the server processing, then the server should also prioritize the cup time to the processing of client requests, Instead of deleting the expiration key.
So timing deletion is not a perfect strategy.
2, lazy delete
Similarly, what is lazy delete, lazy, lazy do delete operation, forced I will not go to delete expired key, lazy home.
Then when is forced by the time, the client now take this data, the key has expired, obviously can not return to the client, this time is also lazy home time, do not delete not Ah, the client side do not want Ah, it can be seen that this strategy is very bad for memory, not building a conservation-minded society, But Cup time is the most friendly, not easy to use cup time to delete expired keys.
3, delete regularly
Regular? Longer than the timing time, will not always go to check the expiration key, delete the expiration key, periodic deletion, is every time to check the database, delete the expired key inside, it is obvious that periodic deletion is a compromise between the timing of deletion and lazy deletion.
So what is the expiration deletion policy used in Redis?
Redis time with lazy deletion and periodic deletion of the two policies together, you can better make the cup time and memory area balance
This article discusses the Expiration key deletion policy of Redis, which cannot be aof and RDB. Two backup strategies for expiration key processing
First, the RDB.
We know that if the RDB function is turned on when the server starts, the server will load the Rdb file
There are two kinds of situations here.
1, the primary server mode, will be checked by the key in the file, the expired key is ignored, so the expiration key will not affect the master server,
2, from the server mode, regardless of the expiration period of the full load database, but the primary server in the data synchronization, the data from the server will be emptied, so generally speaking, the server will not affect
And then the AOF.
Remember that when you perform a aof rewrite, the expiration key is not saved to the rewritten aof file, so the expiration key does not affect the aof.
And when it comes to master-slave replication,
The master deletes an expiration key that will be displayed telling from
When executing a read command sent by a client, even an expired key does not delete the expiration key, but instead handles the same as the non-expiring key, which is removed from the only del command introduced to the main
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Delete policy about expiration keys in Redis