当前位置:网站首页>Redis common interview questions
Redis common interview questions
2022-08-04 05:01:00 【Dumpling_skin】
1. What is cache penetration?How to deal with it?
1. There is no such record in the database and no such record in the cache. At this time, someone maliciously accesses such data in large quantities.This will cause the request to bypass the cache and access the data directly, resulting in excessive pressure on the database.
2. Solution:
[1]Add data verification in the controller.
[2] We can store an empty object in redis, and the expiration time should not be too long.More than 5 minutes
[3] We use bloom filter.Bottom layer: There is a bitmap array that stores all the ids of the table.
Solution:
//pseudo codeString get(String key) { //The Bloom filter clock stores the id corresponding to the database clockString value = redis.get(key); //Get it from the cache first. if (value == null) { //cache missif(!bloomfilter.mightContain(key)){//Check whether the bloom filter clock existsreturn null;}else{value = db.get(key); //Query the databaseredis.set(key, value);} }return value;}
2. What is cache breakdown?How to solve?
Cache breakdown means that there is no data in the cache but there is data in the database (usually the cache time expires). At this time, due to the large number of concurrent users, the data is not read in the cache at the same time, and the data is retrieved from the database at the same time.Causes the database pressure to increase instantaneously, causing excessive pressure.
Cache breakdown solution:
1. Set it to never expire.[This is only suitable for servers with large memory]
2. The use of mutex keys is a common practice in the industry.
Solution:
//pseudo codeString get(String key) {String value = redis.get(key);if (value == null) { //Represents that the cached value expires//Set a timeout of 3min to prevent the failure of the del operation, the next cache expiration will not be able to load dbif(redis.setnx(key_mutex,1,3*60)==1){//represents a successful setupvalue = db.get(key);redis.set(key, value, expire_secs);redis.del(key_mutex);}else{//At this time, it means that other threads at the same time have loaded db and set it back to the cache. At this time, you can retry to obtain the cached value.sleep(50);//Retryget(key);}}else {return value;}}
3. What is cache avalanche?How to solve?
Cache avalanche refers to the large amount of data in the cache until the expiration time, and the huge amount of query data, causing excessive pressure on the database or even downtime.Different from cache breakdown, cache breakdown refers to checking the same piece of data, cache avalanche means that different data have expired, and many data cannot be checked, so the database is checked.
1. What will happen to the cache avalanche:
[1] The project has just been launched, and there is no data in the cache
[2] A large number of caches have expired.
[3]redis宕机
2.解决办法:
1.上线前预先把一些热点数据放入缓存。
2. Set the expiration time to the hash value
3. Build a redis cluster
4. What are the Redis elimination strategies?
When there is insufficient memory in Redis, in order to ensure the hit rate, a certain data elimination strategy will be selected.
volatile (from a key that has an expiration time set)
allkeys (in all keys)
1) volatile-lru: When the memory is insufficient, the LRU (least recently used) algorithm is used from the key with the expiration time set, and the least recently used data is selected for elimination;
2) allkeys-lru: When memory is insufficient, LRU (least recently used) algorithm is used from all keys to select the least recently used data for elimination.
3) volatile-lfu: When the memory is insufficient, the LFU (least recently used) algorithm is used from the keys with the expiration time set, and the data with the lowest frequency is selected for elimination.
4) allkeys-lfu: When the memory is insufficient, use the LFU algorithm from all keys, use the LFU algorithm from all keys, and select the data with the lowest frequency for elimination.
5) volatile-random: When the memory is insufficient, the data is randomly selected from the keys with the expiration time set and eliminated.
6) allkeys-random: When the memory is insufficient, data is randomly selected from all keys and eliminated.
7) volatile-ttl: When the memory is insufficient, select the data that will expire from the key with the expiration time set (select the data that will expire first according to the order of expiration time), to be eliminated.
8) no-enviction: When memory is insufficient, data elimination is prohibited, and the default strategy for writing errors is reported, which is the default memory elimination strategy for Redis.
边栏推荐
猜你喜欢
随机推荐
2022年软件测试——精选金融银行面试真题
Tensors - Application Cases
share总结
将xml标签转换为txt(voc格式转换为yolo方便进行训练)
21 days learning challenge 】 【 sequential search
如何打造一篇优秀的简历
C Expert Programming Chapter 4 The Shocking Fact: Arrays and pointers are not the same 4.4 Matching declarations to definitions
C Expert Programming Chapter 4 The Shocking Fact: Arrays and pointers are not the same 4.1 Arrays are not pointers
【21天学习挑战赛】图像的旋转问题(二维数组)
【一步到位】Jenkins的安装、部署、启动(完整教程)
3000字,一文带你搞懂机器学习!
获取单选框选中内容
C专家编程 第5章 对链接的思考 5.1 函数库、链接和载入
震惊,99.9% 的同学没有真正理解字符串的不可变性
Take care of JVM performance optimization (own note version)
C Expert Programming Chapter 5 Thinking about Linking 5.3 5 Special Secrets of Library Linking
Large chain best freight d audit with what software?What are the functions?
7. The principle description of LVS load balancing cluster
Stop behind.
Write golang simple C2 remote control based on gRPC