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

Cache operation or database operation first

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "cache operation or database operation first". In the operation process of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Update the database before removing the cache problem

After the cache is deleted, the database has not been updated, and a read request is issued, the old value is read from the database, and the update to the cache causes subsequent requests to be old values.

time

From the top

To the next

More and more

The later

Update operation

Delete redis

Read operation

Cache has no data

Read old values from the database

Write back to redis

Update mysql

Return

Return

Solution method

Classic delayed double deletion. That is: first update the data line, then delete the cache, and then delay for a period of time to delete the cache.

time

From the top

To the next

More and more

The later

Update operation

Delete redis

Read operation

Cache has no data

Read old values from the database

Write back to redis

Update mysql

Return

Delay for a period of time

Delete redis

Read operation

Cache has no data

Read the new value from the database

Write back to redis

Return

Delay for a period of time, it is very important not to pat the head, two factors need to be considered

Consider query interface response time

time

From the top

To the next

More and more

The later

Update operation

Delete redis

Read operation

Cache has no data

Read old values from the database

Update mysql

Delay for a period of time

Less than the time it takes to read data from data

Delay policy failure

Delete redis

Write back to redis

Return

Consider mysql master-slave synchronization events

If the self-built mysql cluster is configured improperly, there will be a large synchronization delay between mysql master and slave.

time

From the top

To the next

More and more

The later

Update operation

Delete redis

Read operation

Cache has no data

Read old values from the database

Write back to redis

Update the main mysql library

Return

Delay for a period of time

Less than mysql master-slave synchronization time

Delay policy failure

Delete redis

Read operation

Cache has no data

Read from the library to the old value

Mysql master-slave synchronization completed

Write back to redis

Return

The delay time must be greater than the response time of the query interface.

And it should be longer than the time of master-slave synchronization of mysql to prevent master-slave synchronization delay from reading old values.

-Wen Anshi 20210225

Delete the cache before updating the database

Note: it is recommended that the query interface should be read from the main database

time

From the top

To the next

More and more

The later

Update operation

Update the main mysql library

Read operation

Cache the data and read the old value

Delete redis

Return

Read operation

Cache has no data

The main library read the new value.

Write back to redis

Return

Existing problems:

Wait for the cache deletion to complete, during which there will be inconsistent data in the database for a short time, but generally not to be dealt with.

Summary:

strategy

Potential problem

Solution method

Matters needing attention

Delete cache first

Then update the database.

Under the concurrency condition

Database update has not been completed

There is a concurrent read request, and the old value is read from the database

Delayed double deletion

Delay time must be

Greater than the response time of the query interface.

And it is longer than the time of master-slave synchronization of mysql.

Update the database first

Delete the cache again

Under the concurrency condition

Cache not deleted and read

Read the old value from the cache

Do not handle

Will eventually be consistent.

The query API is recommended to read the main database. There are inconsistencies.

But generally there is no need to deal with it.

This is the end of the content of "cache operation or database operation first". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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