Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to ensure cache consistency in Java

2025-03-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to ensure cache consistency in Java". In daily operation, I believe many people have doubts about how to ensure cache consistency in Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt of "how to ensure cache consistency in Java"! Next, please follow the editor to study!

Plan 1: update the cache and update the database

This approach can be easily excluded, because if the cache update is successful, but the database update fails, it will certainly result in data inconsistency.

Plan 2: update the database and cache

This cache update strategy is commonly known as double writes. the problem is that dirty data will be brushed to the cache in the scenario of concurrent database updates.

UpdateDB (); updateRedis ()

For example: if the database and cache are later requested to be modified between two operations, it is out of date to update the cache at this time.

Plan 3: delete the cache and update the database

Problem: before updating the database, dirty data will be brushed to the cache if there is a query request.

DeleteRedis (); updateDB ()

Example: if a data query occurs between two operations, old data will be cached.

This scheme will result in inconsistent request data.

If one request A for update operation at the same time, the other request B for query operation. Then the following situations will occur:

Request A to write to delete the cache

Request B query found that cache does not exist

Request B to query the database to get the old value.

Request B to write the old value to the cache

Request A to write the new value to the database

This will lead to inconsistencies. Moreover, if you do not set the expiration policy for the cache, the data will always be dirty.

Plan 4: update the database and delete the cache

There is a problem: there is a query request before updating the database, and the cache is invalid, the database will be queried, and then the cache will be updated. If a database update is performed between querying the database and updating the cache, dirty data will be brushed to the cache

UpdateDB (); deleteRedis ()

For example: if a data update occurs and the cache is deleted between querying the database and putting it into the cache, then the old data will be cached.

If there are two requests, one for A to do the query operation and the other for B to do the update operation, the following situations will occur

The cache just expires.

Request A to query the database to get an old value

Request B to write the new value to the database

Request B to delete the cache

Request A to write the old values found to the cache

If this happens, dirty data will indeed occur. However, there is a congenital condition that writing to the database takes less time than reading to the database.

However, the read operation of the database is much faster than the write operation.

Therefore, this situation is very difficult to happen.

Scheme comparison

Common shortcomings of options 1 and 2:

In the scenario of concurrent database update, dirty data will be brushed to the cache, but the probability of concurrent write is relatively small.

From a thread safety point of view, dirty data will be generated, such as:

Thread A updates the database

Thread B updates the database

Thread B updated the cache

Thread A updated the cache

Common shortcomings of options 3 and 4:

No matter which order is used, there are some problems with the two approaches:

Master-slave delay problem: whether it is deleted first or later, database master-slave delay may lead to dirty data.

Cache deletion failed: if cache deletion fails, dirty data will be generated.

Problem-solving ideas: delay double deletion, add retry mechanism, the following introduction!

Update cache or delete cache?

1. Updating the cache cache requires a certain maintenance cost, and there will be the problem of concurrent updates

two。 In the case of more writing and less reading, the read request has not yet come, and the cache has been updated many times, which does not play the role of cache.

3. The value put into the cache may be calculated complicately. if each update, the value of the write cache is calculated, which is a waste of performance.

Delete cache advantages: simple, low cost, easy to develop; disadvantages: it will result in a cache miss

If you update the cache with less overhead and less read and write, you can update the cache only when writing concurrently, otherwise the general practice is to delete the cache.

Summary solution problem probability to update cache-> update database to ensure data accuracy, the data must be based on the database update result. Therefore, this solution is absolutely not recommended to update the database-> update the cache and update the database concurrently. Dirty data will be brushed to the cache and written in the scenario. The probability is generally inconsistent when there are many write requests, which is not recommended. Delete cache-> update database before updating the database, if there are query requests, dirty data will be brushed to the cache and read scenarios will be sent. Inconsistencies will occur when there are more read requests. It is not recommended to use update database-> delete cache has query requests before updating the database, and if the cache expires, the database will be queried and then the cache will be updated. If a database update operation is performed between querying the database and updating the cache, dirty data will be brushed to the cache and read scenarios will be sent & read operations are slower than write operations, and the probability of minimum read operations is slower than write operations. the probability of error is less than that of other methods. Reluctantly recommended. Delayed double deletion of recommended solution

Adopt double delete cache strategy before and after update

Public void write (String key,Object data) {redis.del (key); db.update (data); Thread.sleep (1000); redis.del (key);}

Eliminate cache first

Write the database again

Hibernate for 1 second and eliminate cache again

You should evaluate the time-consuming business logic of reading data for your project. Then the dormancy time of writing data is based on the time-consuming of reading data business logic.

The purpose of this is to ensure that the read request ends, and the write request removes the cache dirty data caused by the read request.

Problem and solution:

1. How to deal with the loss of throughput after synchronous deletion

Delete the second time asynchronously and submit a delayed execution task

2. How to solve the deletion failure:

Add a retry mechanism, for example: delete the failed key and write it to the message queue; but it is a bit serious for business coupling.

The delay tool can be selected:

The most common blocking Thread.currentThread (). Sleep (1000)

Jdk schedules thread pools, quartz scheduled tasks, HashWheelTimer,Rabbitmq delay queues using delayQueue,netty that comes with jdk, and so on

Actual scene

We have a merchandise center scenario where services with more reads and less writes are provided, and MQ is sent to the write data to notify the downstream to get the data. This requires strict consistency between the cache and the database, and high reliable system service capabilities.

Write caching strategy

Cache key setting expiration time

First DB operation, then cache invalidation

Write operations are marked with key (Meituan middleware) to force the main library

Access Meituan middleware to monitor the changing data of binlog (Meituan middleware) before deleting the cache.

Read caching strategy

First judge whether to use the main library or not

If you use the main library, use the tag (Meituan middleware) to check the main library.

If not, check to see if there is data in the cache

If there is data in the cache, use the cached data as a result

If not, check the DB data and then write the data to the cache

Be careful

The question about cache expiration time

If the cache sets the expiration time, then all of the above inconsistencies are temporary.

However, if the expiration time is not set, the inconsistency will have to wait until the next time the data is updated.

So be sure to set the cache expiration time.

At this point, the study on "how to ensure cache consistency in Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report