当前位置:网站首页>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.
边栏推荐
- See how DevExpress enriches chart styles and how it empowers fund companies to innovate their business
- JVM笔记
- 技术解析|如何将 Pulsar 数据快速且无缝接入 Apache Doris
- ADC噪声全面分析 -03- 利用噪声分析进行实际设计
- C Expert Programming Chapter 5 Thinking about Linking 5.1 Libraries, Linking and Loading
- 你以为border-radius只是圆角吗?【各种角度】
- 备份工具pg_dump的使用《postgres》
- 图像处理之Bolb分析(一)
- 震惊,99.9% 的同学没有真正理解字符串的不可变性
- 21 days learning challenge 】 【 sequential search
猜你喜欢
随机推荐
如何简化现代电子采购的自动化?
How to open a CITIC Securities online account?is it safe?
px、em、rem的区别
Jenkins 导出、导入 Job Pipeline
The 2022 PMP exam has been delayed, should we be happy or worried?
drools from download to postman request success
Write golang simple C2 remote control based on gRPC
centos 安装postgresql13 指定版本
Cache pool of unity framework
符号表
About yolo7 and gpu
烧录场景下开发如何进行源代码保密工作
Stop behind.
数的划分之动态规划
[Cloud Native--Kubernetes] Pod Resource Management and Probe Detection
BFC、IFC、GFC、FFC概念理解、布局规则、形成方法、用处浅析
有趣的 Kotlin 0x0E:DeepRecursiveFunction
结构体函数练习
OpenGL绘制圆
System design. How to design a spike system (full version transfer)





![[C language advanced] program environment and preprocessing](/img/ac/a13dd2cc47136d4938b6fc7fad660c.png)



