Table of Contents
Option 1: Update cache, update database
Option 2: Update the database and update the cache
Option 3: Delete the cache and update the database
Option 4: Update the database and delete the cache
Comparison of Schemes
总结
推荐方案
延迟双删
实际场景
写缓存策略
读缓存策略
注意
Home Java javaTutorial How to ensure cache consistency in Java

How to ensure cache consistency in Java

May 02, 2023 pm 01:13 PM
java

Option 1: Update cache, update database

This method can be easily eliminated, because if the cache is updated successfully first, but the database update fails, it will definitely cause data inconsistency.

Option 2: Update the database and update the cache

This cache update strategy is commonly known as double-write. The problem is: in the scenario of concurrent database updates, dirty data will be flushed to the cache

updateDB();
updateRedis();
Copy after login

Example: If the database and cache are modified by subsequent requests between two operations, updating the cache at this time will already be expired data.

How to ensure cache consistency in Java

Option 3: Delete the cache and update the database

There is a problem:Before updating the database, if there is a query request, the dirty Data is flushed to the cache

deleteRedis();
updateDB();
Copy after login

Example:If a data query occurs between two operations, old data will be put into the cache.

How to ensure cache consistency in Java

This solution will lead to inconsistent request data

If there is one request A for an update operation and another request B for a query operation. Then the following situation will occur:

  • Request A to perform a write operation and delete the cache

  • Request B to query and find that the cache does not exist

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

  • Request B to write the old value into the cache

  • Request A to write the old value into the cache Writing new values ​​into the database

The above situation will lead to inconsistencies. Moreover, if you do not set an expiration time strategy for the cache, the data will always be dirty data.

Option 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 operation is performed between querying the database and updating the cache, then the dirty data will be flushed to the cache

updateDB();
deleteRedis();
Copy after login

Example: If there is a query between the database and the cache If data is updated and the cache is deleted during an operation, old data will be put into the cache.

How to ensure cache consistency in Java

Assume there are two requests, one requesting A to perform a query operation, and the other requesting B to perform an update operation, then the following situation will occur

  • The cache just expired

  • Request A to query the database and get an old value

  • Request B to write the new value into the database

  • Request B to delete the cache

  • Request A to write the old value found into the cache

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

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

So this situation is difficult to occur.

Comparison of Schemes

Common shortcomings of Scheme 1 and Scheme 2:

In the concurrent database update scenario, dirty data will be flushed to the cache, but Generally, the probability of concurrent writing scenarios is relatively small;

From a thread safety perspective, dirty data will be generated, for example:

  • Thread A updated the database

  • Thread B updated the database

  • Thread B updated the cache

  • Thread A updated the cache

Common shortcomings of options 3 and 4:

No matter which order is adopted, there are some problems with both methods:

  • Master-slave delay problem: Regardless of whether it is deleted first or last, the database master-slave delay may lead to the generation of dirty data.

  • Cache deletion failure: If the cache deletion fails, dirty data will be generated.

Problem solving ideas: delay double deletion and add a retry mechanism, which is introduced below!

Update cache or delete cache?

  • 1. Updating the cache cache requires a certain maintenance cost, and there will be problems with concurrent updates

  • 2. Write too much In the case of few reads, 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 placed in the cache may have been calculated complexly. , if the value written to the cache is calculated every time it is updated, it will waste performance

Deleting the cache Advantages:Simple, low cost, easy to develop; Disadvantages: Causing a cache miss

If the cost of updating the cache is small and there are more reads and fewer writes, and there is basically no write concurrency, you can update the cache. Otherwise, the general approach is to delete the cache.

总结

方案问题问题出现概率推荐程度
更新缓存 -> 更新数据库为了保证数据准确性,数据必须以数据库更新结果为准,所以该方案绝不可行不推荐
更新数据库 -> 更新缓存并发更新数据库场景下,会将脏数据刷到缓存并发写场景,概率一般写请求较多时会出现不一致问题,不推荐使用。
删除缓存 -> 更新数据库更新数据库之前,若有查询请求,会将脏数据刷到缓存并发读场景,概率较大读请求较多时会出现不一致问题,不推荐使用
更新数据库 -> 删除缓存在更新数据库之前有查询请求,并且缓存失效了,会查询数据库,然后更新缓存。如果在查询数据库和更新缓存之间进行了数据库更新的操作,那么就会把脏数据刷到缓存并发读场景&读操作慢于写操作,概率最小读操作比写操作更慢的情况较少,相比于其他方式出错的概率小一些。勉强推荐。

推荐方案

延迟双删

采用更新前后双删除缓存策略

public void write(String key,Object data){
  redis.del(key);
     db.update(data);
     Thread.sleep(1000);
     redis.del(key);
 }
Copy after login
  • 先淘汰缓存

  • 再写数据库

  • 休眠1秒,再次淘汰缓存

大家应该评估自己的项目的读数据业务逻辑的耗时。然后写数据的休眠时间则在读数据业务逻辑的耗时基础上即可。

这么做的目的,就是确保读请求结束,写请求可以删除读请求造成的缓存脏数据。

问题及解法:

1、同步删除,吞吐量降低如何处理

将第二次删除作为异步的,提交一个延迟的执行任务

2、解决删除失败的方式:

添加重试机制,例如:将删除失败的key,写入消息队列;但对业务耦合有些严重;

How to ensure cache consistency in Java

延时工具可以选择:

最普通的阻塞Thread.currentThread().sleep(1000);

Jdk调度线程池,quartz定时任务,利用jdk自带的delayQueue,netty的HashWheelTimer,Rabbitmq的延时队列,等等

实际场景

我们有个商品中心的场景,是读多写少的服务,并且写数据会发送MQ通知下游拿数据,这样就需要严格保证缓存和数据库的一致性,需要提供高可靠的系统服务能力。

写缓存策略

  • 缓存key设置失效时间

  • 先DB操作,再缓存失效

  • 写操作都标记key(美团中间件)强制走主库

  • 接入美团中间件监听binlog(美团中间件)变化的数据在进行兜底,再删除缓存

How to ensure cache consistency in Java

读缓存策略

  • 先判断是否走主库

  • 如果走主库,则使用标记(美团中间件)查主库

  • 如果不是,则查看缓存中是否有数据

  • 缓存中有数据,则使用缓存数据作为结果

  • 如果没有,则查DB数据,再写数据到缓存

How to ensure cache consistency in Java

注意

关于缓存过期时间的问题

如果缓存设置了过期时间,那么上述的所有不一致情况都只是暂时的。

但是如果没有设置过期时间,那么不一致问题就只能等到下次更新数据时解决。

所以一定要设置缓存过期时间

The above is the detailed content of How to ensure cache consistency in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

See all articles