How is the cache used in the project? Why use caching? What are the consequences of using the cache incorrectly?

interview questions

How is the cache used in the project? Why use caching? What are the consequences of using the cache incorrectly?

Psychoanalysis of the interviewer

Internet companies must ask this question. It would be embarrassing if a person doesn't even know the cache.

As long as you ask about cache, the first question you come up with must be to ask where cache is used in your project? Why use it? No need, okay? What adverse consequences might there be after using it?

This is to see if you have any thoughts behind the cache. If you just use it stupidly and cannot give the interviewer a reasonable answer, then the interviewer must not have a good impression of you and think that you usually think too little. Just know what to do.

Analysis of interview questions

How is the cache used in the project?

This needs to be combined with the business of your own project.

Why use caching?

There are two main purposes of using cache: high performance and high concurrency .

high performance

Assuming such a scenario, you have an operation, a request comes, and you operate mysql in various messes, and find out a result in half a day, which takes 600ms. But this result may not change for the next few hours, or if it changes, it does not need to be fed back to the user immediately. So what to do at this time?

Cache, toss the result of 600ms, throw it in the cache, a key corresponds to a value, next time someone checks, don’t use mysql toss 600ms, directly from the cache, check a value through a key, and get it done in 2ms. 300 times better performance.

That is to say, for some results that require complex operations and time-consuming checks, and it is determined that there will not be much change in the future, but there are many read requests, then the results are directly placed in the cache, and it is good to directly read the cache later.

High concurrency

A database as heavy as mysql is not designed to allow you to play with high concurrency at all. Although it can be played, the natural support is not good. It is easy to call the police when mysql stand-alone support is reached  2000QPS .

So if you have a system, there are 10,000 requests per second during the peak period, and the mysql stand-alone machine will definitely die. You can only upload the cache at this time, put a lot of data in the cache, don't put mysql. The caching function is simple. To put it bluntly, it is a key-value operation. The concurrency supported by a single machine is easily tens of thousands or hundreds of thousands per second, and it supports high concurrency so easy. The concurrency carried by a single machine is dozens of times that of a single mysql machine.

Cache runs on memory, and memory naturally supports high concurrency.

What are the adverse consequences of using the cache?

Common cache problems include the following:

Guess you like

Origin blog.csdn.net/chang384915878/article/details/86747957