What is dirty data? Is it possible to generate dirty data in the cache? What should I do if dirty data occurs?

This is the back-end small class of the monastery. Each article is shared from

[background introduction] [knowledge analysis] [common problems] [solutions] [coding practice] [extended thinking] [more discussion] [References]

Eight aspects of in-depth analysis of back-end knowledge / skills. This article shares:

[what is dirty data? Is it possible to generate dirty data in the cache? What should I do if dirty data occurs?]

Hello, I'm Xu Dongjie, a student of Shanghai Branch of it Academy. I'm an honest, pure and kind java programmer. Today, I'd like to share with you the knowledge points in deep thinking about task 6 of Java (profession) on the official website of the Academy——

What is dirty data? Is it possible to generate dirty data in the cache? What should I do if dirty data occurs?

(1) Background:

Dirty data: the data extracted from the target is expired, wrong or meaningless. This kind of data is called dirty data.

Dirty read: read out dirty data is called dirty read.

(2) Knowledge analysis:

1. Concurrent transaction processing in database:

Dirty reading: in the case of concurrent access, different transactions operate on the same data. When the modified data of transaction a has not been submitted, transaction B reads the data and reads the modified data of transaction a, but transaction a has not been submitted. This is the dirty reading in the database

Update lost: updates are made to different transactions for the same row of data, and the results are overwritten

Phantom reading: transaction a reads two times before and after, and the data read in the last time becomes more. Transaction B has inserted data between the two reads

Non repeatable reading: transaction a reads the data of transaction B twice before and after modification, which does not conform to the isolation type

Isolation level: it can solve the above problems. MySQL defaults to the repeatedly readable isolation level, which will only cause the inconsistency between the read data and the database

2. Dirty data in mybati L1 cache:

First level cache of mybatis: the default is the sqlsession level. As long as the data checked through the session is placed on the session, the next time the data with the same ID is queried, it will be directly flushed out of the cache instead of going to the database.

Mybatis first level cache dirty data: when different sqlsessions are operating on the database, the first level cache can only ensure that the additions, deletions and changes in the current sqlsession are automatically updated in the first level cache, and dirty data will be generated.

3. Dirty data in mybati L2 cache:

Mybatis L2 cache: it is a sessionfactory level and is bound to a namespace. The same namespace is placed in a cache object. When a non sselect statement is executed in the namespace, all caches in the entire namespace are cleared.

Mybatis L2 cache dirty data: operations that cause dirty reads usually occur in multi table Association operations. For example, two different mappers involve the addition, deletion, modification and query of the same table. When one mapper queries this table, the other mapper updates the cache, and then the first mapper queries it again, Then the data queried this time is dirty data. The reason for dirty reads is that the cache of their operations is not the same.

Therefore, it is not recommended to use the built-in L2 cache of mybatis. It is recommended to use a third-party cache: memcached or redis.

(3) Frequently asked questions:

How to update the cache in redis to avoid dirty reading?

(4) Solution:

Reading and writing:

If (redis data exists){

Read redis data

}else{

Read the database and save redis + at the same time. Set the timeout

Update section:

If (database update){

Update redis + settings timeout

(5) Coding practice:

Demonstrate the read-write section and the update section

(6) Expand thinking:

What other methods are available to update redis data

1. Active update: click the update cache button in the background to find the latest data set from the DB, delete the original cached data, and store the new data to the cache (or use a scheduled task)

Problem: after deleting the cache during the update process, there is a business query. At this time, the returned data will be empty, which will affect the user experience. If high concurrency penetrates the DB, it may cause the server to crash

2. Update triggered by the user: when the foreground obtains data and finds that there is no cached data, it will go to the database to synchronize the data to the cache

Problem: when the cache data obtained by concurrent request does not exist, concurrent data query operations will occur

3. Load the data in advance: click the update cache button in the background to find the latest data set from the DB. The cache is not deleted here. The invalid data is overwritten and deleted by traversing the data

Problem: the logic is relatively cumbersome, and the update mechanism cannot be universal

(7) References:

Baidu Google

(8) More discussion:

Q1: what is the difference between database dirty data and redis dirty data?

A1: database dirty data is stored by the user. The stored data is inconsistent with the actual data. Redis dirty data is relative to database data. The inconsistency between redis data and data in the database will lead to dirty data Q2: what are the disadvantages of the redis update method in the code practice in this paper?

A2: disadvantages: the added judgment method is inefficient. When the concurrency is high, the impact on efficiency will be greater. Q3: how to implement redis update by active update? A3: in the background management, set a button to update the redis operation. Generally, when the user visits less at night, the data is found in the database and put into redis

(9) Thanks:

Thanks to elder martial brother Zhu Mingxing. This tutorial is based on their previous technology sharing.

(10) Conclusion:

That's all for today's sharing. You are welcome to like, forward, leave messages and make bricks~

Ppt link video link

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>