Java Daniel share a few typical interview questions about Redis cache

With the improvement of traffic system, to enhance the complexity, the response performance become the focus of a concern. The use of the cache to become a priority.

redis as a leader in the cache middleware, becoming the interview will ask project.

This article Redis share a few common interview questions:

Cache avalanche

1.1 What is the cache avalanche?

If we hang the cache, which means that all of our requests ran the database.

Java Daniel share a few typical interview questions about Redis cache


We all know that Redis can not put all the data is cached (expensive and limited memory), so Redis data needs to set the expiration time, and the use of inert + Delete to expire periodically delete key to remove the two strategies.

If the cached data set expiration time is the same, and all of these just Redis delete part of the data of the light. This can result in this period, while the failure of these caches all requests to the database.

This is the cache Avalanche: Redis hung up, go all database requests.

Cache avalanche If this happens, it is likely to bring down put our database, resulting in paralysis of the entire service!

1.2 How to solve the cache avalanche?

To the expiration time plus a random value when the cache, which would greatly reduce the cache expire at the same time.

For "Redis hung up, go all database requests" This situation, we can have the following ideas:

Prior to the incident: Redis achieve high availability (master-slave architecture + Sentinel or Redis Cluster), try to avoid Redis hang this from happening.

Incident in: Redis really hung up the case, we can set up a local cache (ehcache) + current limiting (hystrix), try to avoid our database was to kill (at least to ensure that our services can still work normally)

After the incident: redis persistent, restart automatically load the data from the disk, cache data fast recovery.

Cache penetration

2.1 What is the cache penetration

Cache penetration refers to a certain query data does not exist. Since the cache miss, and for fault-tolerance considerations, if not finding the cache write data from the database

This will result in a time that does not exist in the data request should go to the database query, it lost the meaning of the cache.

Java Daniel share a few typical interview questions about Redis cache


This is the cache penetration: request a large amount of data in the cache misses, leading to requests go database.

Cache penetrating If this happens, it may bring down our database, resulting in paralysis of the entire service!

2.2 How to solve the cache penetrate?

Solve the cache penetration, there are two options:

Since the parameters of the request is not legitimate (every request parameter does not exist), so we can use the Bloom filter (BloomFilter) compression filter or early interception, illegal, do not let this request to the database layer!

When we can not find from the database, we will be this empty object cache is set to go inside. When next time request, you can get the inside from the cache.

In this case we will generally empty object set a short expiration time.

Write cache and database consistent double

3.1 For a read operation, the process is such that

If our data in the cache inside there, then direct access to the cache.

If the data cache is not what we want, we will go to query the database, and then check out the data written to the database cache. Finally, the data returned to the requester.

3.2 What is the cache and dual-write database consistency problem?

If only the query, the data cached data and databases are not the problem. But when we want to update the time for it? The situation is likely to cause a variety of database and caching of data inconsistency.

Here are inconsistent means: inconsistent data cache with database data

Java Daniel share a few typical interview questions about Redis cache


In theory, as long as we set an expiration time key, we can ensure that the data cache and the database is ultimately the same.

As long as cached data has expired, it will be deleted. Then read, because there is no cache, you can check the data in the database, and then written to the database to check out the data to the cache.

In addition to setting the expiration time, we need to do more to avoid inconsistencies in the database and cache to happen.

At last

This article lead us to understand how to solve the avalanche cache, the cache penetrate, such as a cache to ensure consistency with the database write double issue, I hope you read the help.


Guess you like

Origin blog.51cto.com/14480698/2436786