Redis key expiration strategy and memory elimination strategy

May Hacker 2022-01-26 18:30:42 阅读数:192

redis key expiration strategy memory

Lazy deletion

Don't take the initiative to delete expired key, When you need this key When , To check whether it has expired , Delete after expiration key

Delete periodically

Delimit a certain time frame , That is, polling , Judge key Has it expired , Delete after expiration key

Memory retirement strategy

It should be noted that either regular deletion or lazy deletion is not a completely accurate deletion , There will still be key Scenes that have not been deleted , So we need Memory retirement strategy To supplement .

There are several memory elimination strategies :

  1. noeviction: When memory usage exceeds configuration, an error is returned , It doesn't evict any keys

  2. allkeys-lru: When you add a key , If it goes too far , First, through LRU The algorithm expels the longest unused key

  3. volatile-lru: When you add a key, if it's too limited , First, remove the key from the key set with the expiration time Not used for the longest time Key

  4. allkeys-random: When you add a key, if it's too limited , From all key Random delete

  5. volatile-random: When you add a key, if it's too limited , Randomly expel... From the set of expired keys

  6. volatile-ttl: Evict keys that are about to expire from keys that are configured with expiration time

  7. volatile-lfu: From all keys configured with expiration time Use the least frequent key

  8. allkeys-lfu: Eject the least frequently used key from all keys

There are three main types :LRU Most recently unused ( It doesn't have much to do with the number of times , Concerned about recent usage , Ready to use or not )、 Random delete 、LFU Not often these days ( Realize... According to the number of times of use )

Reference resources

Understand thoroughly Redis Memory obsolescence strategy

copyright:author[May Hacker],Please bring the original link to reprint, thank you.