Hello everyone, I am Cola, a dedicated original, willing to share the program ape. This series of tutorials will continue to be updated, you can search “IT Cola” on wechat to read the first time. Ebooks has a huge selection of free books that I’ve selected for you

Before introducing this article, let’s take a look at the following questions:

How to set the expiration time of Redis key?

②, after setting the expiration time of a key, to this time, the key can be obtained? If I can’t get it, is this key still in memory?

③ How to set the Redis memory size? What memory elimination strategies does Redis have when memory is full? How do we choose?

If you know the above questions, you don’t need to read the rest; If you don’t understand, read on with these questions.

1. Set the Redis key expiration time

Redis provides four commands to set the expiration time (lifetime).

EXPIRE: indicates that the lifetime of the key is set to TTL seconds.

PEXPIRE: indicates that the lifetime of the key is set to TTL milliseconds.

③, EXPIREAT: indicates that the survival time of key is set to timestamp specified by the number of seconds.

(4) PEXPIREAT: indicates that the survival time of key is set to the specified millisecond timestamp.

PS: In Redis internal implementation, the first three commands to set the expiration time are converted to the last PEXPIREAT command.

Two additional knowledge points: first, remove key expiration time

PERSIST: removes the key expiration time.

Second, the remaining survival time of the return key

TTL: returns the remaining lifetime of the key in seconds.

PTTL: Returns the remaining lifetime of the key in milliseconds.

2. Determination of Redis expiration time

Within Redis, whenever we set an expiration time for a key, Redis stores the key with the expiration time in an expiration dictionary. When we query a key, Redis first checks to see if the key is in the expired dictionary and, if so, retrieves its expiration date. Then compare the expiration time with the current system time. If the expiration time is greater than the system time, the expiration time is not expired. Otherwise, the key is expired.

3. Expire deletion policy

There are three ways to delete a key. When setting the expiration time of a key, we create a timer to delete it immediately when the expiration time arrives.

Advantages: Scheduled deletion is the most friendly to memory. Once the key that can save memory expires, it can be deleted from the memory immediately.

Disadvantages: Unfriendly to the CPU. Deleting expired keys occupies CPU time when there are too many expired keys, affecting the response time and throughput of the server.

(2) Lazy delete Set the key expiration time, we do not care about it, when the key is needed, we check whether it is expired, if expired, we delete it, and return the key.

Advantages: CPU-friendly, we only do expiration checks when the key is used, and don’t waste time on expiration checks for many keys that are not needed.

Disadvantages: Not memory friendly, if a key has expired but has not been used, the key will always be in memory, if the database has many of these unused expired keys, the keys will never be deleted and memory will never be freed. This causes a memory leak.

Every once in a while, we check some keys and delete expired keys.

Advantages: You can limit the duration and frequency of a delete operation to reduce the impact on the CPU. In addition, you can also release the memory occupied by expired keys by deleting them periodically.

Disadvantages: It is difficult to determine how long and how often a delete operation is executed.

If this is done too often, the scheduled deletion policy becomes as CPU-unfriendly as the scheduled deletion policy.

If too little is done, it is the same as lazy deletion, and the memory occupied by expired keys will not be freed in time.

Most importantly, when retrieving a key, the value of the key is returned if the expiration date of the key has not been reached but periodic deletion has not been performed, which is an unacceptable error for the business.

4. Redis expires deletion policy

In the previous section, we discussed three strategies for removing expired keys, and found that no single strategy would satisfy our practical needs. You might be wise enough to think that if a single strategy does not satisfy our practical needs, we should use a combination of them.

Yes, The Redis expiration deletion strategy is a combination of lazy deletion and periodic deletion.

Lazy deletion: The lazy deletion policy of Redis is implemented by db.c/expireIfNeeded function. The expireIfNeeded function is called before all key read/write commands are executed, and if expired, the key is deleted and the key does not exist. If the command does not expire, do not perform the operation and continue to run the original command.

Regularly delete: by redis. C/activeExpireCycle function, the function to run at a certain frequency, each run, all from a certain number of database to retrieve a certain amount of random key inspection, and remove the key date.

Note: Instead of checking all libraries and all keys in one run, check a random number of keys.

The frequency at which the periodic deletion function is run is defined as 10 times per second (approximately 100ms) in Redis2.6. After Redis2.8, you can adjust this number by changing the Hz option in the redis.conf configuration file.

We can see that, through the expiration deletion policy, some keys that will never be used, and are not selected and deleted regularly for many times, these keys will also remain in memory, or a large number of keys are stored in Redis. These operations may cause Redis to run out of memory. This is where Redis’s memory flushing strategy comes in.

5. Memory elimination strategy

In the redis.conf file, you can set the maximum memory size by using the parameter maxMemory:

When the existing memory is larger than maxmemory, redis will automatically eliminate the memory mode. By setting maxory -policy, there are several ways to eliminate memory mode:

1) volatile- lRU Removes keys that have expired using the LRU algorithm (lRU: Least Recently Used).

2) Allkeys-lru Removes any keys using the LRU algorithm. This method is usually used.

3) Volatile -random Removes random keys that have expired.

4) Allkeys-random undifferentiated random removal.

5) Volatile – TTL Remove keys that are about to expire (minor TTL)

Noeviction does not remove any key, just returns a write error, default option, won’t be selected.

In the redis.conf configuration file, you can set the elimination mode:

6, summary

Through the above introduction, I believe you have a certain understanding of Redis expired data deletion strategy and memory elimination strategy. Here’s a summary:

The Redis expiration deletion strategy is a combination of lazy deletion and periodic deletion. Lazy deletion can ensure that expired data cannot be obtained when we obtain it, while regular deletion with appropriate frequency can ensure that invalid data can be released in time and will not occupy memory data all the time.

But we say that Redis is deployed on physical machines, memory can not be infinite expansion, when the memory reaches the limit we set, it will automatically trigger Redis memory elimination strategy, and the specific strategy should be selected according to the actual business situation.

This series of tutorials will continue to be updated, you can search “IT Cola” on wechat to read the first time. Reply ebook has a selection of books that I have selected for you