当前位置:网站首页>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.
边栏推荐
猜你喜欢

Towards Real-Time Multi-Object Tracking(JDE)

应届生软件测试薪资大概多少?

你以为border-radius只是圆角吗?【各种角度】
![[21 Days Learning Challenge] Image rotation problem (two-dimensional array)](/img/51/fb78f36c71e1eaac665ce9f1ce04ea.png)
[21 Days Learning Challenge] Image rotation problem (two-dimensional array)

技术解析|如何将 Pulsar 数据快速且无缝接入 Apache Doris

About yolo7 and gpu

day13--postman接口测试

深度学习21天——卷积神经网络(CNN):实现mnist手写数字识别(第1天)

【云原生--Kubernetes】Pod资源管理与探针检测

7-2 LVS+DR Overview and Deployment
随机推荐
OpenGL绘制圆
备份工具pg_dump的使用《postgres》
The Shell function
Stop behind.
Mobile payment online and offline payment scenarios
7-2 LVS+DR Overview and Deployment
Explain detailed explanation and practice
DataTable使用Linq进行分组汇总,将Linq结果集转化为DataTable
The 2022 PMP exam has been delayed, should we be happy or worried?
What are the steps for how to develop a mall system APP?
Turn: Management is the love of possibility, and managers must have the courage to break into the unknown
将xml标签转换为txt(voc格式转换为yolo方便进行训练)
About yolo7 and gpu
数的划分之动态规划
2022软件测试面试题 最新字节跳动50道真题面试题 刷完已拿下15k 附讲解+答疑
Jenkins export and import Job Pipeline
2022年PMP考试延迟了,该喜该忧?
el-Select 选择器 底部固定
【一步到位】Jenkins的安装、部署、启动(完整教程)
小程序 + 电商,玩转新零售