How to analyze several common key and hot issues in redis from multiple dimensions

An independent pig 2021-11-25 18:02:30
analyze common key hot issues


Cache penetration 、 Cache breakdown and cache avalanche are Redis During the interview and actual development , A problem that often needs to be considered . Many people are concerned about the problem 、 The reason and solution are still not clear enough . In fact, for the three situations , A good solution can be found by carefully analyzing the principle of a generation . This paper defines 、 Case study 、 Several perspectives on hazards and solutions , To help you quickly understand the three questions .

Three more

  1. Cache penetration 、 Cache breakdown and cache avalanche are caused by the absence of data in the cache , It leads to going to the database to query data .
  2. Because the cached data does not exist , All requests go to the database , Therefore, it will lead to excessive pressure on the database and even service crash , The whole system cannot be used .

Cache penetration

Definition : Cache penetration is because the data requested by the client does not exist in the cache , Then go to the database , However, the database has no data to be queried by the client , As a result, every request will go through the database query operation . give an example : When the client requests product details , Carry a commodity ID, At this time, the product ID It doesn't exist ( Whether in the cache or in the database ). Cause every request for this ID The data information of commodities will go to the database . harm : Because the data corresponding to the requested parameter does not exist at all , This will cause the database to be requested every time , Increase the pressure on the database or service crash , It even affects other business modules . It often happens to users Malicious request In the case of . Solution :

  1. Cache a... According to the requested parameters null value . And set an expiration time for this value , You can set the time a little short .
  2. Using the bloon filter , First, filter through bloom filter , If it exists in the filter, query the database , Then add it to the cache . If it does not exist, the client data is returned directly .
  3. Because cache penetration may be a malicious request initiated by the user , Users can be ip Write it down , For malicious ip Request a ban .

Cache breakdown

Definition : Cache breakdown is due to Some hot spots key non-existent , This leads to database queries . Increased database pressure . This pressure can be instantaneous , It may also be more lasting . give an example : There are one or more popular products , When the user views the product details, the user carries the product details ID To get the details of the product . At this point, the data in the cache has just expired , Therefore, all incoming requests must go to the database to query . harm : Relative to cache penetration , The data exists in the database , Just because the cache has expired , It leads to going to the database once , Then add to the cache , The next request will go to the cache normally . The so-called harm is also aimed at the harm at the database level . Solution :

  1. Add mutex lock . For the first request , Found no data in cache , At this point, the query database is added to the cache . In this way, the subsequent requests do not need to go through the database query .
  2. Increase business logic expiration time . When setting cache , We can add a cache expiration time . Every time I read , Make a judgment , If the expiration time is less than the current time , Trigger a background thread , Go to the database and pull the data , Then update the cached data and the expiration time of the cache . In fact, the principle is to extend the cache duration at the code level .
  3. Data preheating . The implementation adds data to the cache through the background . For example, before the second kill scene , Just add the inventory of goods to the cache , So when the user request comes , Just go to cache .
  4. Never expired . When setting the expiration time for the cache , Let it never expire . Start a thread in the background , To maintain these cache expiration times and data updates .

Cache avalanche

Definition : We talked about cache breakdown earlier , Because some hotspots in the cache key invalid , Cause a large number of requests to go to the database . However, cache avalanche is the same , It's just that this is more serious , Is most of the cache key invalid , Not one or two key invalid . give an example : In an e-commerce system , The commodity data under a certain category is invalid in the cache . However, many requests in the current system are the product data under the category . This causes all requests to go through the database query . harm : Due to the sudden influx of requests , Every request must go to the database for query . Database instant traffic influx , Seriously increase the burden of database , It can easily lead to the direct paralysis of the database . Solution :

  1. The cache time is random . Because at some point , A lot of cache failures , This indicates that the expiration time of the cache is relatively centralized . We directly set the expiration time as not centralized , Random disruption . In this way, the cache expiration time is relatively less concentrated , There will not be a large number of requests to query the database at the same time .
  2. Multi level cache . Not just by Redis To do caching , We can also use memcached To do caching ( Here is just one example , Other caching services can also be used ). Cache data , Yes Redis Make a cache , Yes memcached Make a cache . If Redis It doesn't work , We can go memcached. But this increases the difficulty of system architecture , And all kinds of other problems , For example, cache multi-level updates .
  3. The mutex . In cache breakdown, we mentioned the use of mutexes to achieve , We can also use it in the case of avalanches .
  4. Set expiration flag . In fact, you can also use the permanent non expiration mentioned in cache breakdown . When requested , Determine expiration time , If the expiration time is approaching, set an expiration flag , Trigger a separate thread to update the cache .


  1. Cache penetration is because the database itself does not have the data .
  2. Cache breakdown and cache avalanche are the reasons why the data exists in the database , Just the data in the cache is invalid , As a result, the database needs to be queried again and then added to the cache .
  3. Cache breakdown is for some hotspots key, Cache avalanche is a large area cache failure . The two principles are actually the same , It's just for caching key It's just different .

本文为[An independent pig]所创,转载请带上原文链接,感谢

  1. 6年老猿带你掌握Spring Boot实现定时任务的动态增删启停
  2. disruptor笔记之六:常见场景,java教程从入门到精通pdf百度云
  3. Pourquoi InnoDB n'utilise - t - il pas un cache LRU naïf?
  4. Java Reflection (2): quelques opérations de base de reflection
  5. 6年老猿帶你掌握Spring Boot實現定時任務的動態增删啟停
  6. Les singes âgés vous permettent de maîtriser le démarrage et l'arrêt dynamiques des tâches programmées par Spring boot
  7. Docker From Beginning to Practice Series IV - docker Container chorégraphe Clean docker Composition
  8. 编写 java 程序,为家用电脑 ipv6 自动更新 goddy dns 记录(ddns)
  9. java jvm-old gc耗时几十s,导致系统告警
  10. Disruptor note 6: scénario commun, tutoriel Java de l'introduction à la maîtrise du PDF Baidu Cloud
  11. 编写Java程序启动脚本最佳实践
  12. How to get the correct Linux user's documents, music videos and other directories?
  13. Java JVM Old GC prend des dizaines de s, ce qui provoque une alarme système
  14. Écrivez un programme Java pour mettre à jour automatiquement les enregistrements DNS goddy (ddns) pour l'ordinateur domestique IPv6
  15. 編寫Java程序啟動脚本最佳實踐
  16. Meilleures pratiques pour écrire des scripts de démarrage de programmes Java
  17. Notes sur springcloud Eureka
  18. Ajout, suppression et modification simples de mybatis
  19. MySQL Learning - Logging System Redo log and Bin log
  20. Springboot Common comments | @ configuration
  21. Mécanisme d'expiration du cache redis et d'élimination de la mémoire
  22. Analyse concise du code source redis 01 - configuration de l'environnement
  23. Java - carte mémoire de l'objet
  24. Redis source Concise Analysis 02 - SDS String
  25. Why did docker lose to kubernetes? Docker employee readme!
  26. Spring cloud gateway practice 2: more routing configuration methods
  27. Principe de mise en œuvre ultime du mécanisme de concurrence Java sous - jacent
  28. [démarrer avec Java 100 exemples] 13. Modifier l’extension de fichier - remplacement de chaîne
  29. Java期末作业——王者荣耀的洛克王国版游戏
  30. Elasticsearch聚合学习之五:排序结果不准的问题分析,阿里巴巴java性能调优实战
  31. Java期末作業——王者榮耀的洛克王國版遊戲
  32. Java final work - King's Glory Rock Kingdom Game
  33. 【网络编程】TCP 网络应用程序开发
  34. 【网络编程入门】什么是 IP、端口、TCP、Socket?
  35. 【網絡編程入門】什麼是 IP、端口、TCP、Socket?
  36. [Introduction à la programmation réseau] qu'est - ce que IP, port, TCP et socket?
  37. [programmation réseau] développement d'applications réseau TCP
  38. [Java Basics] comprendre les génériques
  39. Dix outils open source que les architectes de logiciels Java devraient maîtriser!!
  40. Java经典面试题详解,突围金九银十面试季(附详细答案,mysql集群架构部署方案
  41. java架构之路(多线程)synchronized详解以及锁的膨胀升级过程,mysql数据库实用教程pdf
  42. java整理,java高级特性编程及实战第一章
  43. java教程——反射,mongodb下载教程
  44. Java岗大厂面试百日冲刺 - 日积月累,每日三题【Day12,zookeeper原理作用
  45. Java后端互联网500道中高级面试题(含答案),linux钩子技术
  46. java8 Stream API及常用方法,java初级程序员面试
  47. java-集合-Map(双列)——迪迦重制版,2021Java开发社招面试解答之性能优化
  48. Flink处理函数实战之二:ProcessFunction类,java线程面试题目
  49. flex 布局详解,【Java面试题
  50. Linux basic command learning
  51. Why did docker lose to kubernetes? Docker employee readme!
  52. MySQL安装
  53. Elastic Search Aggregate Learning five: Problem Analysis of Uncertainty of sequencing results, Alibaba Java Performance Tuning Practical
  54. Installing, configuring, starting and accessing rabbitmq under Linux
  55. Oracle SQL injection summary
  56. Installation MySQL
  57. L'exposition à la photo d'essai sur la route i7 du nouveau vaisseau amiral de BMW Pure Electric a également été comparée à celle de Xiaopeng p7.
  58. spring JTA 关于异常处理的时机问题
  59. Le problème du temps de traitement des exceptions dans la JTA printanière
  60. Do you really know MySQL order by