Deletion policy for expired keys in Redis
We have learned that Redis is a memory database, and data in Redis is stored in the memory in the form of key-value, which is maintained and managed by the Redis server, memory is precious. unwanted data or useless data that has been used will not make it exist in memory for a long time. After all, we still need to build a conservation-type society. Therefore, we can set the survival time or expiration time for the key to weigh the limited memory and the increasing data. The command to set the expiration time is as follows: [expire key length (seconds)] or [pexpire key time length (MS)], you can also use [expireat key time point (Unix timestamp )], you can run the TTL or pttl command to check the remaining time of the key.
The expiration time of the key in the database is stored in a dictionary, which is mentioned in the Redis _ dictionary. This article mainly describes the problem of deleting the expired key, an expired key won't be stored in the memory for a long time. It must be deleted to build a conservation-oriented society. When will it be deleted? There are three different deletion policies, which are described one by one and easy to understand.
1. Scheduled Deletion
What is scheduled deletion? As the name suggests, it means that I set a time to delete it at the point. What are the benefits? The memory is the most friendly and can ensure that the expired key will be deleted as soon as possible, the memory is released as soon as possible, but the coins are on both sides. The memory is friendly. When the CUP time is reached, the coins will be deleted from the point to the point. The CUP will not do anything else, it is unrealistic to simply delete the expired key, and the server also needs to create a large number of timers to implement scheduled deletion. If the memory is not missing now, there is idle memory, at this time, a large number of command requests are waiting for the server to process, so the server should give priority to the CPU time for processing client requests, rather than deleting the expiration key.
Therefore, Regular deletion is not a perfect policy.
2. Lazy Deletion
Similarly, what is a lazy Delete, a lazy, and a lazy delete operation means that I will not delete the expired keys.
So when is the last resort? The client has taken the data and the key has expired. Obviously, it cannot be returned to the client. This is the time when the client is late, I can't do it without deleting it. I don't want to do it on the client side. It can be seen that this strategy is very bad for memory and does not build a conservation-oriented society, but the CUP time is the most friendly, it is not easy to use the CUP time to delete the expiration key.
3. Regular deletion
Regular? It takes a little longer than the scheduled time to check the expired key, delete the expired key, and delete it regularly. It is used to check the database and delete the expired key at intervals, it is obvious that regular deletion is a compromise between regular deletion and lazy deletion.
So what expiration deletion policy is used in Redis?
Redis adopts two policies: inert deletion and Regular deletion, which can better balance the CPU time and memory zone.
This article discusses the expiration key deletion policy of apsaradb for Redis, so we cannot skip the two backup policies AOF and RDB to process the expiration key.
First, let's talk about RDB.
We know that if the RDB function is enabled when the server is started, the server will load the RDB file.
There are two cases:
1. The primary server mode will be checked by the keys in the file. Expired keys are ignored, so the expired keys will not affect the primary server,
2. In slave server mode, all data is loaded into the database no matter the expiration date. However, when the master server synchronizes data, the data on the slave server is cleared, it will not affect the slave server either.
Then AOF
Remember, the expiration key will not be saved to the overwritten AOF file when AOF rewrite is executed, so the expiration key will not affect AOF.
There is also the master-slave Replication
The master deletes an expiration key, which will show
When executing the READ command sent by the client, even the expired key will not delete the expired key, but will be processed like the Non-expired key. Only the Del Command introduced to the master will be deleted.
Install and test Redis in Ubuntu 14.04
Redis cluster details
Install Redis in Ubuntu 12.10 (graphic explanation) + Jedis to connect to Redis
Redis series-installation, deployment, and maintenance
Install Redis in CentOS 6.3
Learning notes on Redis installation and deployment
Redis. conf
Redis details: click here
Redis: click here
This article permanently updates the link address: