Welcome to pay attention to github.com/hsfxuebao, I hope to help you, if you think it can trouble to click on the Star ha
0. Meet often
- What are your redis memory Settings for production?
- How to configure and modify redis memory size
- What do you do if the memory is full
- How does Redis clean up memory? Do you know about periodic deletion and lazy deletion
- Redis cache elimination strategy
- Redis LRU for more information, please refer to blog.csdn.net/weixin_4827…
1. An overview of the
View the maximum memory occupied by Redis, as shown in the following figure
Open the Redis configuration file and set the maxMemory parameter. Maxmemory is bytes.
How much memory can redis use by default? If the maximum memory size is not set or the maximum memory size is set to 0, the memory size is not limited in a 64-bit operating system and the maximum memory size is 3GB in a 32-bit operating system
How do you arrange for general production? Redis is generally recommended to set the memory to three-quarters of the maximum physical memory
How do I modify redis memory Settings
- Modify the file configuration, as shown in the following figure
2. Run the following command to change the password
What command is used to view redis memory usage? info memory
What happens when you fill it up? What happens if Redis memory usage exceeds the maximum set? Change the configuration and deliberately set the maximum value to 1 byte, as shown below:
2. Redis sets the expiration time of the key
2.1 Setting and canceling the expiration time
-
Expire key TTL: Sets the TTL to TTL seconds
-
Pexpire key TTL: Sets the TTL to milliseconds
-
Expireat Key timestamp: Indicates the timestamp in seconds with the expiration time set to timstamp
-
Pexpireat Key timestamp: Indicates a timestamp with an expiration time of milliseconds
-
Persist key: indicates the expiration time
-
TTL key: obtains the remaining TTL
Although there are various commands to set the expiration time, the expire, pexpire, and expireat commands are all implemented using pexpireat. After the conversion, the result is the same as the pexpireat command. As follows:
2.2 Saving the expiration time
The expire dictionary in the redisDB structure holds the expiration times of all the keys in the database. This is called the expiration dictionary:
- The key of an expired dictionary is a pointer to a key object in the key space
- The value of the expiration dictionary is an integer of type long, which holds the expiration time of the database key to which the key points –
Unix timestamp with millisecond accuracy
. - Saved expiration time = current millisecond timestamp + expiration time millisecond timestamp
As follows:
typedef struct redisDb {
// Saves all key-value pairs in the database. The keys are strings and the values are multiple types
dict *dict; /* The keyspace for this DB */
// Save all keys in this data with expiration time set
dict *expires; /* Timeout of keys with a timeout set */
// Save client blocking
dict *blocking_keys; /* Keys with clients waiting for data (BLPOP)*/
dict *ready_keys; /* Blocked keys that received a PUSH */
// Save the command by watch
dict *watched_keys; /* WATCHED keys for MULTI/EXEC CAS */
// Save the database index
int id; /* Database ID */
long long avg_ttl; /* Average TTL, just for stats */
} redisDb;
Copy the code
The data structure is as follows:
3. Delete the redis expiration key
If a key is out of date, is it removed from memory immediately after its expiration date? When will it be deleted after expiration? What operation is it?
3.1 Immediate Deletion
Redis cannot traverse all keys that have been set to live time all the time to detect whether data has reached an expiration date and then delete it.
Immediate deletion ensures maximum freshness of data in memory because it guarantees that expired keys will be deleted immediately after expiration, freeing up their memory. But immediate deletion is the least CPU-friendly. Because the deletion operation will take up CPU time, if the CPU is very busy, such as doing intersection or sorting calculation, it will cause extra pressure to the CPU, let the CPU tired, always need to delete, busy dead. This incurs a significant performance cost, as well as affecting data read operations.
Summary: Not CPU friendly, trading processor performance for storage (time for space)
3.2 Lazy Deletion
Data is not processed when it reaches the expiration date. And the next time you access that data,
- If not, return data;
- Delete it and return it does not exist.
The disadvantage of the lazy delete strategy is that it is the least memory friendly.
If a key is out of date and still exists in Redis, its memory will not be released as long as the expired key is not deleted.
When using inert deletion policy, if there are so many expired keys in a database, and these expired keys are not accessed to date, so they may never be deleted (unless the user performed manually FLUSHDB), we can even see it as a memory leak – useless garbage data takes up a lot of memory, The server does not release them itself, which is not good news for Redis servers whose health is heavily dependent on memory.
Summary: Memory unfriendly, trading memory for processor performance (space for time)
3.3 Deleting files Periodically
The periodic deletion policy is a compromise between the preceding two policies. The periodic deletion policy performs the deletion expiration key once in a period of time and limits the duration and frequency of the deletion operation to reduce the impact on the CPU time.
Periodical polling of time-sensitive data in redis library, random sampling strategy is adopted, and the deletion frequency is controlled by the proportion of expired data. Features are as follows:
- CPU usage has a peak value, and the detection frequency can be customized
- The memory pressure is not high, and the cold data that occupies the memory for a long time is cleared continuously
For example, by default, Redis checks every 100ms to see if there are expired keys, and deletes expired keys. Note: redis does not check all keys every 100ms but rather randomly selects them (if all keys are checked every 100ms, redis goes directly to the ICU). Therefore, if you only use the periodic deletion policy, many keys will not be deleted.
The difficulty of the periodic deletion policy is to determine the duration and frequency of the deletion operation. If the deletion operation is carried out too frequently or for a long time, the periodic deletion policy will degenerate into the established deletion policy and consume too much CPU time to delete expired keys. If the deletes are performed too little or for too short a time, the periodic deletion policy will waste memory just like the lazy deletion. Therefore, if the periodic deletion policy is adopted, the server must set the execution duration and frequency of the deletion operation appropriately.
Summary: Periodically check the storage space (random check, key check). Periodically sample keys to see if they are out of date, but there will always be a catch
3.4 summarize
- Delete regularly and never be spot checked
- Lazy deletion is also never used in the dot
This can cause a large number of expired keys to pile up in memory, causing Redis to run out of memory or quickly run out of space. There has to be a better bottom-of-the-line solution, the Redis cache elimination strategy.
4. Elimination strategy
In redis6, there are the following elimination strategies:
- Noeviction: Will not expel any key
- Allkeys-lru: deletes allkeys using the lru algorithm
- Volatile – lRU: Deletes all keys with expiration time using the LRU algorithm
- Allkeys-random: deletes allkeys randomly
- Volatile -random: Randomly deletes all keys whose expiration time is set
- Volatile – TTL: deletes keys that are about to expire
- Allkeys-lfu: deletes allkeys using the lfu algorithm
- Volatile – lFU: Deletes all keys whose expiration time is set using the LFU algorithm
Conclusion: 2 times 4 is 8, 8 out of 8 strategies
- Two dimensions:
Filter in expired key
和Filter among all keys
- Four aspects: LRU, LFU, RANDOM and TTL
Which elimination strategy do you usually use? Most companies are using Allkeys-lRU, note that Redis is designed to be noeviction by default
Redis’s cache expiration policy is not to be confused with its memory expiration policy