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

What are the four schemes for the final consistency of database caching?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

This article will explain in detail what are the four solutions for the final consistency of database cache. the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Background

Caching is a very useful concept in software development, and database caching is an inevitable scenario in the project. The guarantee of cache consistency is repeatedly asked in the interview, here is a summary, according to different requirements, choose the right consistency program.

What is caching?

The speed of storage is different. Caching is a technology that temporarily stores the results of low-speed storage in high-speed storage.

As shown in the figure, the storage above the pyramid can be used as a cache for the storage below. Our discussion mainly focuses on the database cache scenario and will take redis as the cache of mysql as a case study.

Why do you need caching

Storage such as mysql usually supports complete ACID features, because of reliability, persistence and other factors, performance is generally not high, high concurrent queries will bring pressure to mysql, resulting in database system instability. At the same time, it is prone to delay. According to the locality principle, 80% of requests will fall on 20% of the hot data. In the scenario of reading more and writing less, adding a layer of cache is very helpful to improve system throughput and robustness.

There are problems

The stored data may change over time, and the data in the cache will be inconsistent. The specific tolerable inconsistent time requires specific business-specific analysis, but the usual business needs to be ultimately consistent.

Redis as mysql cache

In the usual development mode, mysql is used as storage, while redis is used as cache to accelerate and protect mysql. But how can redis stay synchronized when mysql data is updated.

The cost of strong consistency synchronization is too high, if the pursuit of strong consistency, then there is no need to use caching, just use mysql. What is usually considered is the ultimate consistency.

Solution

Option one

Through the expiration time of key, when mysql is updated, redis is not updated. This approach is simple to implement, but it can take a long time to be inconsistent. If read requests are frequent and expire for a long time, a lot of long-term dirty data will be generated.

Advantages:

Low development cost and easy to implement

The management cost is low, and the probability of problems will be relatively small.

Deficiency:

Completely dependent on expiration time. If the time is too short, the cache will expire frequently, and if it is too long, there will be long update delays.

Option 2

Expand on the basis of scheme 1, through the expiration time of key, and update redis when updating mysql.

Advantages:

Compared with option 1, the update delay is smaller.

Deficiency:

If the update of mysql succeeds, but the update of redis fails, it degenerates to scenario one.

In high concurrency scenarios, the business server needs to connect with mysql,redis at the same time. This is the loss of double connection resources, easy to cause the problem of too many connections.

Option 3

Optimize the synchronous write redis of the second scheme, add the message queue, give the redis update operation to the kafka, ensure the reliability by the message queue, and then build a consumer service to update the redis asynchronously.

Advantages:

Message queue can use one handle, and many message queue clients also support local cache sending, which effectively solves the problem of too many connections in solution 2.

Logical decoupling is realized by using message queue.

The message queue itself is reliable and can be consumed to redis at least once by means such as manual submission.

Deficiency:

The timing problem still cannot be solved. If multiple business servers respectively process two requests for the same row of data, for example, a = 1; a = 5, if the first item in mysql is executed first, and the order of entry into kafka is the second, then the data will be inconsistent.

The introduction of message queue, at the same time to increase the service consumption of messages, the cost is high.

Option 4

Update redis by subscribing to binlog, and take the consumer service we built as a slave of mysql, subscribe to binlog, parse the updated content, and then update it to redis.

Advantages:

When the mysql pressure is small, the delay is low

Completely decoupled from the business

The problem of timing is solved.

Disadvantages:

It is costly to build a separate synchronization service and introduce binlog synchronization mechanism.

On the database cache final consistency of the four schemes are shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Database

Wechat

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

12
Report