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

I am Redis

Hello, this is Redis , and a man named Antirez brought me into this world.

Speaking of my birth, it is related to the relational database MySQL .

Before I came to this world, MySQL had a very hard time. The Internet is developing faster and faster, and it contains more and more data. User requests also skyrocket, and every user request changes. It has become a read and write operation one after another, MySQL is miserable. Especially in the days when people like "Double 11" and "618" shopping spree, MySQL is suffering.

As MySQL told me later, in fact, most of the user requests are read operations, and they often query something repeatedly, wasting a lot of time for disk I/O.

Later, someone wondered whether it is possible to learn from CPU and add a cache to the database? So I was born!

Soon after I was born, I became good friends with MySQL, and we often appeared in the back-end server hand in hand.

The data that the applications query from MySQL is registered with me. When you need to use it later, you will first ask me for it. I don't have to look for MySQL again here.

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 the memory, I don't need to perform I/O operations that are as slow as a snail, so it takes a lot of time to find me than to find MySQL.

Don't underestimate this simple change, I can reduce the burden for MySQL! With the running of the program, I cached more and more data, and for quite a while, I blocked the user's request for it. This time, it was so easy!

With my participation, the performance of the network service has improved a lot, thanks to the many shots I took for the database.

Cache expiration && cache elimination

But soon I discovered that things were not good. The data I cached was all in memory, but even on the server, the memory space resources were still very limited. I couldn't save it uncontrollably. I had to think of a way. Otherwise, take jujube pills.

Soon, I thought of a way: set a timeout for the cached content, and leave the specific setting to the application to set it. All I have to do is to delete the expired content from me and make space in time. .

The timeout period is over. When should I do this cleanup?

The simplest is to delete regularly , I decided to do it once in 100ms , 10 times a second!

I can't delete all the expired ones in one go when I clean up. I have a lot of data stored in it. If I want to scan it all over, I don't know how long it will take, which will seriously affect my reception of new customer requests!

Time is tight and the task is heavy, so I have to randomly choose a part to clean up, which can relieve the memory pressure.

After a period of time, I found that some key-values ​​had better luck. They were not selected by my random algorithm every time, and they were spared every time. This is not good. These long-expired data have been occupied. Less memory space! Shivering cold!

I can't rub the sand in my eyes! So on the basis of the original regular deletion, another trick is added:

Those key values ​​that escaped my random selection algorithm, once encountered a query request, I found out that it had expired, then I would never be welcome and delete it immediately.

Because this method is triggered passively, it will not happen without query, so it is also called lazy deletion !

However, there are still some key values, which not only escaped my random selection algorithm, but have not been queried, resulting in them being at large! At the same time, the available memory space is getting less and less.

And even if I take a step back, I can delete all the expired data. If the expiration time is set for a long time, the memory will be full before I can clean it up, so I still have to eat jujube pills, so I have to think A way.

I thought for a long time and finally came up with a big move: memory elimination strategy , this time I want to solve the problem completely!

I have provided 8 strategies for application programs to choose for how to make decisions when I encounter memory shortage:

  • noeviction : returns an error and will not delete any key values

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

  • volatile-lru : Use the LRU algorithm to delete the least recently used key value from the key set with an expiration time

  • allkeys-random : delete randomly from all keys

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

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

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

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

With the above sets of combo punches, I no longer have to worry about the problem of filling up the space with more expired data~

Cache penetration && Bloom filter

My life is quite comfortable, but MySQL is not as comfortable as I am. Sometimes when I encounter some annoying requests and the data to be queried does not exist, MySQL will have to work in vain! Not only that, because it doesn't exist, I can't cache it, so that every time the same request comes, I have to keep MySQL busy. My value as a cache has not been reflected! This is what people often call cache penetration .

After going back and forth, MySQL big brother couldn't help it: "Oh, brother, can you help me think of a way to block those queries that I know will not have results"

At this time, I thought of another good friend of mine: Bloom filter

My friend has no other skills, so he is good at quickly telling you whether the data you are looking for exists from a huge data set (to tell you quietly, my friend is a little unreliable, if it tells you that it exists, you can’t believe it completely. In fact, it may not exist, but if it tells you that it does not exist, it must not exist ).

If you are interested in my friend, you can take a look at "Vernacular Bloom Filter BloomFilter" here .

I introduced this friend to the application, so there is no need to bother MySQL for non-existent data, and easily help solve the problem of cache penetration.

Cache breakdown && cache avalanche

After this, a period of peace passed until that day...

Once, the MySQL guy was fishing graciously, and suddenly a lot of requests were sent to him, and he was caught off guard.

After a busy period of work, MySQL found me angrily, "Brother, what's the matter, why is it so fierce all of a sudden" I checked the log, and quickly explained: "Big brother, I'm really sorry, just a hot spot data arrived. I deleted the expiration time. Unfortunately, a large number of query requests for this data came afterwards. I have deleted it here, so the requests are sent to you."

"What are you doing, pay attention next time", MySQL eldest brother left with an unhappy expression.

I didn't pay much attention to this trivial matter, and then I left it behind, but I never thought that a few days later, I would stoke a bigger basket.

That day, a large number of network requests were sent to MySQL, which was much larger than the previous one. The big brother MySQL got down several times in a while!

It took a long time for this wave of traffic to pass, and MySQL was relieved.

"Brother, what's the reason this time?", MySQL eldest brother was exhausted.

"This time is even worse than the last time. This time a large amount of data has passed the expiration date almost simultaneously, and then there have been many requests for these data, so the scale is larger than the last time."

MySQL eldest brother frowned upon hearing this, "Then you have to think of a way to torture me in two days, who can stand it? "

"Actually, I am also very helpless, this time is not set by me, or I go to the application and tell him to set the cache expiration time evenly? At least don't let a large amount of data collectively invalidate."

"Go, let's go together"

Later, we went to the application to discuss, not only randomized the expiration time of the key value, but also set the hot data to never expire, which alleviated a lot. Oh, by the way, we also named the two problems: cache breakdown and cache avalanche .

We finally have a comfortable life again...

Easter eggs

That day, I was working hard, and I accidentally made a mistake and the whole process collapsed.

When I started it again, all the previously cached data was gone, and all the stormy requests were once again sent to Big Brother MySQL.

Alas, if only I could remember what was cached before the crash...

Foresee the future, please pay attention to the follow-up wonderful······

Previous TOP5 articles

The CPU obviously has 8 cores, why is the network card desperately tossing the No. 1 core?

I almost lost my job because of a cross-domain request

That's it! The CPU just begged for something to happen soon!

Which hash table is stronger? Several programming languages ​​are arguing!

A fantasy journey of HTTP packets

Guess you like

Origin blog.csdn.net/xuanyuan_fsx/article/details/108573235