"Please describe Redis' cache elimination strategy"
If you happen to encounter this question, have you figured out how to answer it?
Regarding this question, I have compiled the answers of the experts into the interview document of 15W words.
You can get it by private message.
Check out the experts' answers below.
Expert:
I need to answer this question from three aspects.
The first aspect:
When the memory used by Redis reaches the threshold configured by the maxmemory parameter, Redis will eliminate the memory according to the configured memory policy.
Remove infrequently accessed keys from memory.
maxmemory by default is the maximum memory of the current server.
Second aspect:
Redis provides 8 cache elimination strategies by default. In general, these 8 cache elimination strategies can be classified into five types.
- The first is to use the LRU strategy, which is to eliminate the keys that are not often used
- Second, the LFU strategy is adopted, which optimizes the LRU algorithm to increase the number of data accesses, thereby ensuring that non-hot keys are eliminated.
- The third, random strategy, is to delete some keys randomly
- The fourth, ttl strategy, selects the key with the latest expiration time from the key with the expiration time set for priority elimination
- Fifth, when the memory is not enough, an error is reported directly, which is the default strategy.
These strategies can be manually configured and modified in the redis.conf file, and we can choose the appropriate elimination strategy according to the type of cache and the scenario in which the cache is used.
The last aspect, when we use caches, is to increase the expiration time of these caches.
Because we know the approximate life cycle of these caches, we can make better use of memory.
Summarize
Redis is an in-memory database, and memory is a very precious resource.
How to use limited server resources to support more business, we must take into account the cache elimination algorithm to eliminate some caches that are not used very much.
Therefore, I think the investigation direction of this interview question is also very good, and I suggest you to study it in depth.
Remember to like and subscribe! ! !
Copyright notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated. Reprint please indicate the sourceMic带你学架构
!
If this article is helpful to you, please help to follow and like, your persistence is the driving force for my continuous creation. Welcome to follow the WeChat public account of the same name to get more technical dry goods!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。