This is the 14th day of my participation in Gwen Challenge

1. Processing flow of cache

1. The general request process is:

Graph LR A[server] -- request --> B{server} B-- query -->C

In order to relieve the pressure on the database, you need to set up the cache, you can use Redis to cache

2. Request process with cache

Graph LR A[customer] -- request --> B{server} B--3 Query -->C((database)) D--2 The query result is null-->B C--4. The query result is returned -->B B--5. Store the query result in the cache -->D

The database is queried only when the data in the cache does not exist, and then the data is stored in the cache. The next time the data is queried, it is queried from the database, which relieves the pressure on the database server

Second, cache penetration

It’s usually a malicious attack that happens.

1. Describe

When a user access a cache, not in the database and no data to the server to get this request, will visit cache server first, and then access the database, but did not query to the data, there is no deposit to cache data, if the user has a lot to send this request at this moment, will increase the pressure of the database and cache.

2. Solutions

  1. Validates the requested data on the server. For example, you need to add an interface to check whether the score is in the range of 0 to 100. If not, you can return NULL to reduce unnecessary queries.
  2. Set an empty data in the cache server. If no data can be queried in the database, a key-null data is directly stored in the cache. The next time the data is queried, a NULL data is directly queried in the cache.

Cache breakdown

1. Describe

When the expiration time of a data in the cache reaches (because the cache is generally stored in memory, it is generally set to an expiration time in order to save memory), a large number of users access the data at the same time. Because the data is not in the cache, the pressure on the database will suddenly increase.

2. Solutions

  1. Do not set expiration time for hot data. The cache always has this data without setting expiration time.
  2. Set an automatic update time on the server. For example, if you set an expiration time of 30 minutes for data in the cache, the data will be updated after 30 minutes.
  3. Locks methods that access the database. Only one user can access the database at a time, which greatly reduces the strain on the database.

Cache avalanche

1. Describe

When a large amount of data in the cache expires at the same time, and there are many users accessing the data at the same time, the database pressure increases suddenly.

2. Solutions

  1. Use random values when setting expiration time. When the expiration time of data is random, the possibility of simultaneous expiration of data is greatly reduced.
  2. With a distributed structure, data can be cached to multiple locations. When data is cached in multiple cache servers, the probability that data in the cache will expire at the same time is greatly reduced.
  3. The workaround in cache breakdown can be used.

Five, the summary

Cache penetration usually occurs when there is no data in the cache. Cache breakdowns and cache avalanches usually occur when the data in the cache expires. Cache breakdowns are data that is expired and accessed by a large number of users at the same time. Cache avalanche is when a large amount of data expires and a large number of users access different data at the same time.

I’ll end this article with a more superficial example. Databases are like our bodies, and caches are like body armor. When a bullet hits us, the body armor is empty (there’s no data in the cache) and the bullet hits us. When the bullet hit us, we were changing our body armor (the data in the cache had expired) and the bullet hit us.

Our goal in solving these cache problems is to protect the database.

— — — — — — — — — — — — — — —

The more you know, the more you don’t know.

If you have any questions about the content of this article, please comment directly or email me. If you think my writing is good, a “like” is also a sign of support

Shall not be reproduced without permission!

Wechat search [programmer Xu Xiaobai], attention can be the first time to read the latest article. There are 50 high-frequency school recruitment interview questions prepared by me, as well as a variety of learning materials.