Don't you understand redis? After reading this story, you will understand!

The wind of Xuanyuan 2020-11-07 20:15:09
don understand redis reading story

I am a Redis

Hello , I am a Redis, One is called Antirez The man who brought me into this world .

Speaking of my birth , With relational databases MySQL It's quite original .

Before I came into this world ,MySQL It's been a hard time , The Internet is growing faster and faster , It holds more and more data , User requests have skyrocketed , And every user request becomes a read-write operation to it ,MySQL It's hard to say . Especially to “ double 11”、“618“ This is the day of the national shopping spree , All are MySQL The days of suffering .

According to later MySQL Tell me. , In fact, more than half of user requests are read operations , And it's often a repeat query , Waste a lot of time on disk I/O.

Later, some people thought , Is it possible to learn from CPU, Add a cache to the database ? So I was born !

Shortly after birth , I'll go with MySQL Became a good friend , We're often on the back-end server hand in hand .

Applications start with MySQL Data queried , Check in with me , When you need to use it later , Just ask me for , I'm not looking for MySQL want .

For ease of use , I support the storage of several data structures :

  • String

  • Hash

  • List

  • Set

  • SortedSet

  • Bitmap

  • ······

Because I record all the registered data in memory , You don't have to do snail slow I/O operation , So it's better to look for me than to look for MySQL It will save a lot of time .

Don't underestimate this simple change , I can do it for MySQL It lightens a lot of burden ! As the program runs , I'm caching more and more data , I've been blocking user requests for quite a while , This time it's free !

With my participation , The performance of network services has improved a lot , It's all due to the fact that I've been shot a lot for the database .

Cache expiration && Cache obsolescence

But soon I found out that things were not good , The data I cache is in memory , But even on the server , Memory space resources are still very limited , You can't live like this , I have to find a way , Or take jujube pills .

Soon , I came up with a way : Set a timeout for cached content , It's up to the applications to set the exact length , What I have to do is delete the expired content from me , Just make room in time .

There's a timeout , When should I do the cleaning work ?

The simplest is to delete it regularly , I decided 100ms Just do it once , One second is 10 Time !

When I clean up, I can't delete all the expired ones in one breath , I have a lot of data in it , I don't know how long it will take to scan it all over the place , It will seriously affect my reception of new customer requests !

Time is tight and task is heavy , I had to choose a random part to clean up , It can relieve the memory pressure .


It's been a while , I found that some key values are lucky , Every time I'm not selected by my random algorithm , Every time I survived , This is not acceptable. , These long overdue data have been occupying a lot of memory space ! Shivering cold !

I can't rub sand in my eyes ! So on the basis of the original regular deletion , Another move :

The key values that originally escaped my random selection algorithm , Once you encounter a query request , I found out it was overdue , Then I'll never be polite , Delete... Now .

This is because it's passively triggered , It doesn't happen without a query , So it's also called lazy deletion !

But , There are still some key values , Both escape my random selection algorithm , I haven't been inquired , They've been at large ! And at the same time , There's less and less memory available .

And even to say the least , I can delete all the expired data , In case the expiration time is too long , I haven't been waiting for me to clean up , The memory is full , Take the same jujube pill , So I have to find a way .

I've been thinking about it for a long time , Finally, a big trick came out : Memory retirement strategy , This time, I want to solve the problem completely !

I have provided. 8 There are three kinds of strategies to choose from , Used to decide when I run out of memory :

  • noeviction: Returns an error , No key values will be deleted

  • allkeys-lru: Use LRU Algorithm to delete the least recently used key value

  • volatile-lru: Use LRU The algorithm removes the least recently used key value from the set of keys with expiration time set

  • allkeys-random: From all key Random delete

  • volatile-random: Randomly delete from the set of keys with expiration time set

  • volatile-ttl: Remove the key with the shortest remaining time from the keys with the expiration time set

  • volatile-lfu: Remove the least frequently used key from the keys configured with expiration time

  • allkeys-lfu: Remove the least frequently used key from all keys

With the above sets of combo Boxing , I don't have to worry about the problem that there are too many expired data to fill up the space ~

Cache penetration && The bloon filter

My life is quite comfortable , however MySQL Big brother is not as comfortable as I am , Sometimes there are annoying requests , The query data does not exist ,MySQL It's going to be a waste of time ! More Than This , Because it doesn't exist , I can't cache it either , That leads to the same request, every time you have to make MySQL I'm going to live in vain . My value as a cache has not been reflected ! This is what people call cache penetration .


  This comes and goes ,MySQL Big brother can't help it :“ alas , brother , Can you help me find a way , Block the queries that you know won't work out for me ”

Then I thought of another good friend of mine : The bloon filter

My friend has no other abilities , I'm good at quickly telling you whether the data you're looking for exists from a very large data set ( Whisper it to you , This friend of mine is a little unreliable , It tells you not to believe everything that exists , In fact, it may not exist , But if he tells you it doesn't exist , Then it must not exist ).


If you're interested in my friend , You can have a look here 《 Vernacular bloom filter BloomFilter》.

I introduced this friend to the app , Data that doesn't exist doesn't have to be bothered MySQL 了 , Easy to help solve the problem of cache penetration .

Cache breakdown && Cache avalanche

After that, there was a period of peace , Until that day ···

There is a ,MySQL That guy is fishing for fish , Suddenly, a lot of requests were accepted by him , He was caught off guard .

After a lot of work ,MySQL Angry to find me ,“ brother , What's the matter , How come all of a sudden so fierce ”

I checked the log , Explain to me :“ eldest brother , I'm really sorry , We just had a hot data expiration time , I deleted , Unfortunately, there were a lot of requests for this data , I have deleted , So the requests came to you ”

“ What are you doing , Pay attention next time ”,MySQL Big brother left with a look of unhappiness .

I don't care much about this little thing , And then it was forgotten , But I didn't think about it. A few days later, I made a bigger basket .

On that day , There are a lot of network requests coming to MySQL over there , It's much bigger than last time ,MySQL Big brother, I've been working hard for several times !

It took a long time for this wave of traffic to pass ,MySQL It's time to relax .

“ bro , What's the reason this time ?”,MySQL Big brother is so tired that he has no strength .

“ This time it's even more unfortunate than the last one , This time, a large number of data have passed the validity period almost at the same time , And then there were a lot of requests for that data , So it's bigger than last time ”

MySQL Big brother frowned when he heard it ,“ Then you have to find a way , Torture me for three days , Who can resist this ?”

“ In fact, I am helpless , This time is not set by me , Or I'll go to the app and talk about it , Let him set the cache expiration time more evenly ? At least don't let a lot of data collectively fail ”

“ go , Let's go together ”

later , We talked to the app , Not only the expiration time of the key value is random , Also set the hot data never expired , This problem has eased a lot . oh , We also named the two problems separately : Cache breakdown and cache avalanche .

We've finally had a comfortable life again ···

Colored eggs

On that day , I'm working hard , Something went wrong by accident , The whole process collapsed .

When I start again , All the previously cached data is gone , The storm like requests were met again MySQL Big brother there .

alas , If only I could remember what was cached before the crash ···

Foresee the future , Please pay attention to the following highlights ······

In the past TOP5 article

CPU Mingming 8 A nuclear , Why is the network card struggling with No.1 core ?

Because of a cross domain request , I almost lost my job

Finished !CPU I'm just begging for something to happen !

Which hash table is better ? Several programming languages have been quarrelling !

One HTTP The magic of data packets


本文为[The wind of Xuanyuan]所创,转载请带上原文链接,感谢

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云