Redis solves cache, breakdown and avalanche

Lovely wzz 2021-02-23 13:41:28
redis solves cache breakdown avalanche


Redis The use of caching , Greatly improve the performance and efficiency of the application , Especially in data query . But at the same time , It also brings some problems . among , The most important question , It's data consistency , Strictly speaking , There is no solution to this problem . If there is a high requirement for data consistency , Then you can't use caching .

Other typical problems are , Cache penetration 、 Cache avalanche and cache breakdown . at present , There are also popular solutions in the industry . This article , It's not to solve these three problems more perfectly , It's not about subverting popular solutions in the industry . It is , From the actual code operation , To demonstrate these three phenomena . The reason for this is , Because , Just look at the academic explanation of these problems , It's hard to have an image in your head , With the actual code demo , We can deepen our understanding of these problems .

Cache penetration

Cache penetration , It means to query data that does not exist in a database . The normal process of using cache is roughly , Data query starts with cache query , If key Nonexistence or key It's overdue , Then query the database , And query the object , Put in cache . If the database query object is empty , It's not put into the cache .

Redis Caching process

Code flow

Parameter is passed into the primary key of the object ID according to key Get objects from cache If the object is not empty , Go straight back to If the object is empty , Do database query If the object queried from the database is not empty , Then put it into the cache ( Set expiration time ) Imagine this situation , If the parameter passed in is -1, What will happen ? This -1, Is the object that must not exist . Will go to the database every time , And every query is empty , It doesn't cache every time . If there is a malicious attack , You can take advantage of this loophole , Put pressure on the database , Even crush the database . Even with UUID, It's also easy to find a nonexistent KEY, The attack .

Xiao Bian is at work , Will cache null values , That is to say 【 Code flow 】 pass the civil examinations 5 Step , If the object queried from the database is empty , Also put it in the cache , Only the cache expiration time is set to be shorter , For example, set to 60 second .

Cache null

Cache avalanche

Cache avalanche , At a certain time , Expiration in cache set .

One of the reasons for the avalanche , For example, when writing this article , It's about double twelve o'clock , There will soon be a rush , This wave of commodity time is put into the cache , Suppose you cache for an hour . Then at one o'clock in the morning , The cache of this batch of goods has expired . And the access to this batch of products , It's all in the database , For databases , There will be periodic pressure peaks .

When Xiaobian is doing e-commerce projects , Generally, different kinds of goods are adopted , Cache different cycles . Goods in the same category , Add a random factor . This can spread the cache expiration time as much as possible , and , Popular categories have a longer cache time , The storage time of the products in the popular category is shorter , It can also save resources of cache service .

Cache time added suijiyinzi

Actually, the concentration is overdue , It's not very deadly , More deadly cache avalanche , It means that a node of the cache server is down or disconnected . Because of the cache avalanche formed naturally , Cache must be created in a certain time period , Then the database can withstand the pressure , This is the time , The database can also withstand the pressure . It's just periodic pressure on the database . The cache service node is down , The pressure on the database server is unpredictable , It's likely to crush the database in an instant .

Cache breakdown

Cache breakdown , It means a key Very hot , Constantly carrying big concurrency , Large concurrent centralized access to this point , When this key At the moment of failure , Continuous large concurrency breaks through the cache , Direct request database , It's like cutting a hole in a barrier .

When Xiaobian is doing e-commerce projects , Take this and make it “ Hot style ”.

Actually , Most of the time, it's hard to bring crushing pressure to the database server . Few companies have reached this level . therefore , A pragmatist editor , The main products are prepared early , Let the cache never expire . Even if some of the products have fermented themselves into pop money , Also directly set to never expire .

The greatest truths are the simplest ,mutex key Mutex really doesn't work .

版权声明
本文为[Lovely wzz]所创,转载请带上原文链接,感谢
https://javamana.com/2021/02/20210223133858742r.html

  1. Java collection processing / null value processing / exception processing, use experience sharing!
  2. mysql Innodb_flush_log_at_trx_commit 和 sync_binlog
  3. mysql Innodb_ flush_ log_ at_ trx_ Commit and sync_ binlog
  4. 不能回滚的Redis事务还能用吗
  5. 不能回滚的Redis事务还能用吗
  6. Can redis transactions that cannot be rolled back be used
  7. 23种java设计模式
  8. Java、JavaScript、C、C++、PHP、Python都是用来开发什么?
  9. Docker overlay 清理空间
  10. 「Linux」- 安装 Opera 浏览器 @20210223
  11. java的byte和C#的byte的不同之处
  12. Can redis transactions that cannot be rolled back be used
  13. 23 Java design patterns
  14. What are Java, JavaScript, C, C + +, PHP and python used to develop?
  15. Docker overlay cleaning space
  16. "Linux" - install opera browser @ 20210223
  17. Differences between Java byte and C byte
  18. SAP UI5 JavaScript文件的lazy load - 懒加载
  19. Java 在Excel中添加筛选器并执行筛选
  20. LiteOS:盘点那些重要的数据结构
  21. Lazy load lazy load of SAP ui5 JavaScript files
  22. Add filter and execute filter in excel by Java
  23. Liteos: inventory those important data structures
  24. HDFS依然是存储的王者
  25. [MySQL]事务的MVCC原理与幻读
  26. 93.7%的程序员!竟然都不知道Redis为什么默认16个数据库?
  27. Java 集合处理/ 空值处理/ 异常处理,使用心得分享!
  28. Spring Authorization Server 全新授权服务器整合使用
  29. Spring Security 实战干货:OAuth2登录获取Token的核心逻辑
  30. Java中各种锁的原理解析
  31. java的byte和C#的byte的不同之处
  32. Java 在Excel中添加筛选器并执行筛选
  33. HDFS is still the king of storage
  34. Mvcc principle and unreal reading of [MySQL] transaction
  35. 93.7% of programmers! Why does redis default to 16 databases?
  36. Java collection processing / null value processing / exception processing, use experience sharing!
  37. Integrated use of new authorization server of spring authorization server
  38. Spring security real combat dry goods: the core logic of oauth2 login to obtain token
  39. Principle analysis of various locks in Java
  40. Differences between Java byte and C byte
  41. Add filter and execute filter in excel by Java
  42. Dialogue in spring
  43. 解决Docker MySQL无法被宿主机访问的问题
  44. Oracle OCP 19c 认证1Z0-083考试题库(第1题)
  45. Solve the problem that docker MySQL cannot be accessed by the host
  46. Oracle OCP 19C certification 1z0-083 examination question bank (question 1)
  47. 在 2021 年你需要掌握的 7 种关于 JavaScript 的数组方法
  48. Seven array methods for JavaScript you need to master in 2021
  49. 在 2021 年你需要掌握的 7 种关于 JavaScript 的数组方法
  50. Struts2 + Json _ 配置,异常解决及深入了解Struts2返回JSON数据的原理及具体应用范例
  51. Seven array methods for JavaScript you need to master in 2021
  52. Struts2 + Json _ Configuration, exception resolution and in-depth understanding of Struts2 return JSON data principle and specific application examples
  53. (三)MySQL锁机制 + 事务
  54. (3) MySQL lock mechanism + transaction
  55. 在 2021 年你需要掌握的 7 种关于 JavaScript 的数组方法
  56. Seven array methods for JavaScript you need to master in 2021
  57. 基于Kafka和Elasticsearch构建实时站内搜索功能的实践
  58. Practice of building real time search function in the website based on Kafka and elasticsearch
  59. Golang 实现 Redis(9): 使用GeoHash 搜索附近的人
  60. RxHttp - 轻量级、可扩展、易使用、完美兼容MVVM、MVC架构的网络封装类库