当前位置:网站首页>[development of large e-commerce projects] cache - distributed lock - cache consistency solution -45
[development of large e-commerce projects] cache - distributed lock - cache consistency solution -45
2022-07-20 15:45:00 【Random stone light】
One : Cache consistency
problem : How to keep the data in the cache consistent with that in the database ?
1. Double write mode —— After the database is modified , Modify the data in the cache
1) shortcoming : It's possible to generate dirty data . But it can guarantee the final consistency .
2) Solution :
- Lock
- Temporary data inconsistency , Add expiration time to cached data
2. Failure mode —— Delete cache , Wait for the next active query to update
1) shortcoming : Will read dirty data . The second is to write if it is slow , It will lead to the third reading , Read the dirty data written first
2) Solution :
- Frequently modified data , Don't add cache , Put it directly into the database .
Two : Cache consistency - Solution
1. Whether it's dual write mode or failure mode , Will cause cache inconsistencies . That is, if multiple instances are updated at the same time, something will happen . What do I do ?
- 1、 If it is user latitude data ( Order data 、 User data ), This concurrency rate is very small , Don't think about it , Cache data plus Last expiration date , Trigger the active update of read at regular intervals
- 2、 If it's a menu , Basic data such as commodity introduction , You can also use canal subscribe binlog The way .
- 3、 Cache data + The expiration time is also sufficient to solve the cache requirements of most businesses .
- 4、 Ensure concurrent read and write by locking , Line up as you write . It doesn't matter to read . So it's suitable to use read-write lock .( Business doesn't care Dirty data , Allow temporary dirty data to be ignored );
2. summary :
- The data we can put into the cache should not be real-time 、 The requirement of consistency is very high . So when caching data, add expiration time , Protect Just get the latest data every day .
- We should not over design , Increase the complexity of the system
- When it comes to real-time 、 Data with high consistency requirements , You should check the database , Even if you slow down .
3. Get commodity service code
public Map<String, List<Catalog2Vo>> getCatalogJsonFromDbWithRedissonLock() {
//1. Inject redisson, Acquire distributed lock . The name of the lock , The finer the granularity of the lock , The faster the speed.
// Lock granularity : The specific cache is a certain data ,11 Item No
RLock lock = redisson.getLock("catalogJson-lock");
lock.lock();
Map<String, List<Catalog2Vo>> dataFromDb = null;
try {
dataFromDb = getDataFromDb();
} catch (Exception e) {
e.printStackTrace();
} finally {
// Release the lock
lock.unlock();
}
return dataFromDb;
}
3、 ... and : Cache consistency - solve -Canal
Canal: Alibaba open source middleware , It can simulate the slave server of a database .mysql Any change will pass binlog The binary log is synchronized to cannal,canal Will update redis The data in .
At present, the system is not used canal, Need extra maintenance .
Four : Our system consistency solutions
1) All cached data must have expiration time , When the data is expired, the next query can trigger active update
2) Reading and writing data , Plus distributed read-write locks
边栏推荐
- CUDA error in nchwtonchhw2 solution
- Bleu score implementation
- C# 在Word中添加多行多列文字水印
- 软件测试如何快速入门
- npm warn config global `--global`, `--local` are deprecated. Use ` --location solution
- SCCM2012R2网络部署重装系统
- Pytorch, initialize tensor
- Machine learning (1) environment configuration
- 面试官问:如何防超卖,有几种实现方式
- 基于 EasyCV 复现 DETR 和 DAB-DETR,Object Query 的正确打开方式
猜你喜欢
随机推荐
微信小程序(登录、分享、支付)
Wechat applet (login, sharing, payment)
Pymongo迁移MongoDB数据库的数据
Open the link in wechat and jump to the follow page of official account
【WSN通信】基于matlab生物地理学优化HWSN节能聚类协议【含Matlab源码 1989期】
【C语言】动态内存管理、柔性数组
【大型电商项目开发】缓存-分布式锁-缓存一致性解决-45
【MATLAB项目实战】基于MATLAB的发票识别(含GUI界面)
给3月1日至3月31日做60万条数据,VideoDate 要求1到31日StartTime要求00:00到24点,仅供笔记参考
Research on the best implementation scheme of feign
【综合笔试题】难度 4/5,字符处理的线段树经典运用
C# 在Word中添加多行多列文字水印
Troubleshooting ideas that can solve 80% of faults
PostgreSQL memory explosion programming summary
塔望3W消费战略-消费本位研究法:焦点座谈会(Focus Group)
How to operate international crude oil futures to open an account safely?
Machine learning (1) environment configuration
15. 简单工资管理系统设计
Adversarial Training的pytorch的实现
图像标注开源小工具-labelImg