当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
Stop behind.
Tensors - Application Cases
Use serve to build a local server
42. 接雨水
信息学奥赛一本通 1312:【例3.4】昆虫繁殖
看DevExpress丰富图表样式,如何为基金公司业务创新赋能
2022软件测试面试题 最新字节跳动50道真题面试题 刷完已拿下15k 附讲解+答疑
Take care of JVM performance optimization (own note version)
The Shell function
【id类型和NSObject指针 ObjectIve-C中】
商城系统APP如何开发 都有哪些步骤
C Expert Programming Chapter 4 The Shocking Fact: Arrays and pointers are not the same 4.4 Matching declarations to definitions
技术解析|如何将 Pulsar 数据快速且无缝接入 Apache Doris
商城App开发都有哪些功能呢
[SemiDrive source code analysis] [MailBox inter-core communication] 47 - Analysis of RPMSG_IPCC_RPC mode limit size of single transmission and limit bandwidth test
Explain detailed explanation and practice
文件系统的简单操作
C专家编程 第4章 令人震惊的事实:数组和指针并不相同 4.1 数组并非指针
【机器学习】21天挑战赛学习笔记(一)
【一步到位】Jenkins的安装、部署、启动(完整教程)



![[Evaluation model] Topsis method (pros and cons distance method)](/img/e7/c24241faced567f3e93f6ff3f20074.png)



![[Cocos 3.5.2]开启模型合批](/img/d9/9e8f71c9a26c8052b11291fe3ba7ac.png)
