Chuyển đến nội dung chính

Why Cache

Interview question

How is the cache used in the project? Why use a cache? What are the consequences of improper use of the cache?

Interviewer psychoanalysis

This problem, Internet companies must ask, if a person is not even clear about the cache, it is rather embarrassing.
As long as you ask the cache, the first question is, first of all, ask you where to use the cache? Why use it? Don't you have to work? What are the possible consequences if used later?
This is to see if you have any thoughts on the back of the cache. If you are stupid and can't give the interviewer a reasonable answer, then the interviewer will not be very good at you. I think you usually think too little. I know how to work.

Analysis of interview questions

How is the cache used in the project?

This, you need to combine the business of your own project.

Why use a cache?

With caching, there are two main uses: high performance , high concurrency .

high performance

Assuming such a scenario, you have an operation, a request comes over, you are arbitrarily operating mysql, and a result is found for half a day, which takes 600ms. But this result may not change in the next few hours, or it may not need to be immediately feed back to the user. So what do you do at this time?
Cache it, need 600ms to get result, throw it to cache and responds a key to value, next time someone asked, it not spend 600ms to get result again but directly from cache. It's only 2ms. Performance is increased by 300 times.
That is to say, for some results that require complicated operations and time-consuming detection, and it is determined that the latter call does not change much, but there are many read requests, then directly put the results of the query in the cache, and then directly read the cache.

High concurrency

Mysql is such a heavy database, the design is not to let you play high concurrency, although you can play, but natural support is not good. mysql single supported to 2000QPSbe easy to start the alarm.
So if you have a system, there are 10,000 requests coming in one second during the peak period, and that one mysql single machine will definitely die. You can only cache on this time, put a lot of data in the cache, do not put mysql. Caching feature simple, it means key-valueoperation, the amount of concurrent easy one second stand-alone support tens of thousands of hundreds of thousands, support high concurrency so easy. The single-machine bearer is tens of times more than the mysql stand-alone.
The cache is memory-bound, and the memory naturally supports high concurrency.

What are the consequences of using the cache?

Common cache issues are as follows:
This will be described in detail later.

Nhận xét

Bài đăng phổ biến từ blog này

How to ensure double write consistency between the cache and the database?

Interview question How to ensure double write consistency between the cache and the database? Interviewer psychoanalysis You only need to use the cache, it may involve double storage and double storage of the database. As long as you double write, there will be data consistency problems. How do you solve the consistency problem? Analysis of interview questions In general, if you allow the cache to be slightly inconsistent with the database, that is, if your system  is not strictly required to  "cache + database" must be consistent, it is best not to do this, namely:  read request Serialize the write request  and string it into a  memory queue  . Serialization guarantees that there will be no inconsistencies, but it will also result in a significant reduction in system throughput, supporting a request on the line with machines that are several times larger than normal. Cache Aside Pattern The most classic cache + database read and write mode...

Storing Enum like String

In this post, i want to share about storing Enum into Database (Reddis, NoSQL,...) as String then load it again as Enum type. In Ecomerce Application, we use NoSQL repository to do CRUD with DB. Behind the sense, it use some json library(ex: Jackson) to parse your object to JSON and store to DB. With java objects, it work well but for enum type, we may lose old data after we change eum property. Example we have enum: and it is used in Person class: If we persist person object to ivy repo, the JSON after serialization will look like this: This JSON just type String so when we load this from persistence, it will be deserialized and we can get back the correct type enum for property status. Just assume that after several months, customer want to change this enum, he want to rename the property MARRIED to GET_MARRIED, So for all data was persisted after that time it’s fine, but not the old one. we lost all data about status because system does not know that MARRIED now become GET...

Front-end technical trend observation 2019

Top GitHub projects This is trend of front-end technology that may helpful for you. Experience Technology myliang / x-spreadsheet JS implementation of the class Excel form, it is very convenient to embed your own project nosir / cleave.js Automatic formatting<input />Input, easy to deal with credit card input, mobile phone number input, date input and other scenarios didi / chameleon Didi's cross-end development framework, class VUE syntax, multi-terminal rendering transloadit / uppy Functional Web file Upload components, modular plug-in, for modern front-end system instantpage / instant.page When the mouse hovers over the link, the preload page starts and opens directly in seconds after clicking. klaussinani / qoa Simple and easy-to-use lightweight interactive command prompt, which is a must leon-ai / leon Open source AI dialogue robot, you can write your own modules t...