【TECH】Redis cache elimination strategy

The layered architecture of the application system, in order to speed up data access, puts the most frequently accessed data in the cache to avoid accessing the database every time. Caching refers to putting some data that needs to be read in disk or memory , and it is generally placed in memory because of the pursuit of speed. The cache capacity is limited. If the cache is full, how does the system eliminate some data? It involves the cache elimination strategy

Three cache elimination strategies:

1.FIFO- First In First Out - First In First Out
2.LFU - Least Frequently Used - Least - Use
3.LRU - Least Recently Used - Recently - Least Frequently Used - Use

Deletion strategies for redis three expired keys

When a key expires in redis, it is not deleted from Redis immediately. Instead, the following three ways:

1. Immediately delete:

2. Lazy deletion: When the key reaches the expiration time, it will not be processed. Wait until the next visit to judge, delete the key if it expires, and return the data if it is not expired.

3. Periodic deletion: The periodic deletion strategy is a compromise between the previous two strategies. The periodic deletion strategy executes the operation of deleting expired keys every once in a while, and reduces the impact of deletion operations on CPU time by limiting the duration and frequency of deletion operations. .

Redis adopts the scheme of lazy deletion + regular deletion. The regular scan of Redis will only scan the keys with the expiration time set. Redis uses a separate expiration dictionary expires (which can be regarded as a hash table) to save the expiration time of the data with the expiration time set, so there will be no scanning of all keys. Even so, redis defaults to randomly extracting the key in the expired dictionary every 100ms, checking whether it is expired, and deleting it if it is expired. If you do not perform random random checks at regular intervals but scan all of them, the service may be unavailable for a long time, which is tantamount to a disaster.

Since it is a random scan, what should I do about the keys that have expired but have not been scanned? It doesn’t matter. There is also lazy deletion. When obtaining a key, Redis will check whether the key has expired if the expiration time is set. up? If it expires, it will be deleted at this time and return null.

The key of the expired dictionary dict is a pointer, which points to a key object in the key space (that is, a database key). The value of the expiration dictionary is an integer of type long long, which stores the expiration time of the database key pointed to by the key—a UNIX timestamp with millisecond precision. In practice, the key of the keyspace and the key of the expiration dictionary both point to the same key object, so there will not be any duplicate objects, and no space will be wasted.

Eight Elimination Strategies for Redis Cache​​​​​​​​

noeviction: No keys will be evicted, that is, when the memory exceeds the limit, an error will be reported and OOM will be returned, but no keys will be evicted (factory default configuration)

allkeys-lru: Use the LRU algorithm to delete all keys (recommended)

volatile-lru: Use the LRU algorithm to delete all keys with an expiration time set

allkeys-random: Randomly delete all keys

volatile-random: Randomly delete all keys with an expiration time set

volatile-ttl: Delete keys that are about to expire

allkeys-lfu: use LFU algorithm to delete all keys

volatile-lfu: Use the LFU algorithm to delete all keys with an expiration time set

​​​​​​​

Guess you like

Origin blog.csdn.net/MrChenLen/article/details/114396640