当前位置:网站首页>Redis cache data consistency and problems
Redis cache data consistency and problems
2022-06-12 05:41:00 【Xicai pengyuyan】
How to ensure the consistency of cache and database data
Cache data insertion timing :
For the server : Query data steps :
1、 First, go to the cache to query the data , If the data exists, get the data directly and return
2、 If the cache does not exist , Need to query database , Get data from the database and insert it into the cache , Return the data to
3、 When the second query , Subsequent query operations can query cached data
When updating data :
1、 First delete the cache and update the database
When updating database data , So let's delete the cache , Then update the database , When subsequent requests read data again , The data will be read from the database and updated to the cache
Existing problems : After deleting the cache , Before updating the database , If there is a new request during this time period , The old data will be read from the database and written to the cache , Again, the data is inconsistent , And subsequent read operations are old data
2、 First update the database and delete the cache
Update operation , Update the database first , After success , Deleting cache , Subsequent requests write new data back to the cache
Existing problems : to update MySQL And delete the cache , The requested read is still old data in the cache , But when the database update is complete , It's going to come back together
3、 Update cache asynchronously
After the database update operation is completed, the cache will not be operated directly , Encapsulate the operation command into a message and put it in the message queue , Then from Redis Update the data yourself , Message queuing ensures the consistency of data operation data , Ensure that the cached data is normal
The cache problem
Cache penetration - We can't find it
Concept :
Cache penetration means that a user wants to query a data , Find out Redis There is no , That is, cache miss , It's like a persistent database initiating a query , It is found that the database does not have this data , So the query failed , When users request a lot , Cache miss , There's no data in the database , It will put a lot of pressure on the database , This is cache penetration 
Solution :
The first solution : Using the bloon filter 
After using the bloom filter , Put the stored data into the bloom filter , Each data query first queries the bloom filter , When the existence is judged in the filter, the query is cached in the database , If you do not enter the data query , If the filter does not exist , Then it directly returns to tell the user that the data cannot be found , This can greatly reduce the pressure of database query
Second option : Caching empty objects
When the database data does not exist , Empty objects returned in time are also cached , Also set an expiration time , After that, the access data will be obtained from the cache , Protect the database
Existing problems :
1、 Set expiration time for null values , There will be a period of time from updating the database data to invalidating the cached data , There is a problem with cached data , It will have an impact on the business that needs to ensure data consistency
2、 You will need more space to store more null values , A key that causes a large number of null values in memory
Cache breakdown - Too much , Cache expiration
Buffer breakdown refers to a key It's a hot spot key, Carrying a lot of concurrent , When key At the moment of failure , Continuous large concurrency will break through the cache , Request directly to the database . Cause instantaneous excessive pressure on the database
Solution :
First option : Hot data never expires
From a caching perspective , Expiration time is not set , There will be no problems after the cache expires
Second option : Add mutex lock
Time use distributed lock , Guaranteed for every key Only one thread can query back-end services at the same time , Other threads that do not have access to the lock can wait
Cache avalanche
A cache avalanche is a time period , The cache set expires or Redis Downtime
For databases , All request pressures will reach the database , This leads to a surge in database calls , It may also cause database downtime 
Solution :
First option :Redis High availability
The idea of this scheme is that the data is in Redis Is stored on the server , A server hangs up in time , Other servers can continue to work
Second option : Current limiting the drop
This idea is after the cache expires , The number of threads reading the database is controlled by locking or queuing, and the threads are queued in the queue , Control the overall request rate
The third option : Data preheating
Data preheating is timely before the deployment of services , First access the data , Most of the data can be loaded into the cache , Before the big concurrency happens, different key, Set different expiration times , Make cache invalidation time more uniform
边栏推荐
- Thesis reading_ Figure neural network gin
- Halcon 用点来拟合平面
- [go] Viper reads the configuration file in the go project
- XML parameter schema, the same MTK SW version is compatible with two different sets of audio parameters
- Performance & interface test tool - JMeter
- C language - how to define arrays
- CCF noi2022 quota allocation scheme
- 38. appearance series
- Matlab: image rotation and interpolation and comparison of MSE before and after
- 19. regular expression matching
猜你喜欢
![[fastapi] use pycharm to configure and use environment variables for fastapi projects](/img/a5/47cabfed3f12bf70b4b047ef29fc9d.jpg)
[fastapi] use pycharm to configure and use environment variables for fastapi projects
![[grpc development] go language builds simple server and client](/img/24/06c3c1219ecad7e117f4df152e9ce7.jpg)
[grpc development] go language builds simple server and client

March 4, 2021

Matlab: image rotation and interpolation and comparison of MSE before and after

Towards End-to-End Lane Detection: an Instance SegmentationApproach

个体工商户是不是法人企业

Role and understanding of proc/cmdline

The most commonly used objective weighting method -- entropy weight method

Performance test - GTI application service performance monitoring platform

Vivado HLS introductory notes
随机推荐
14- II. Cutting rope II
Detailed analysis of mathematical modeling problem a (vaccine production scheduling problem) of May Day cup in 2021
About architecture (in no particular order)
Save the object in redis, save the bean in redis hash, and attach the bean map interoperation tool class
The most commonly used objective weighting method -- entropy weight method
AddUser add user and mount hard disk
Flex / fixed Upper, Middle and Lower (Mobile end)
Is the individual industrial and commercial door a legal person enterprise
Halcon 3D 1 读取3d数据
38. 外观数列
Go 接口实现原理【高阶篇】
62. the last number left in the circle
[go] Viper reads the configuration file in the go project
yolov5
How does WiFi 802.11 correspond to 802.3
WebRTC AEC 流程解析
国企为什么要上市
20. string representing numeric value
Thesis reading_ Figure neural network gin
Matlab: image rotation and interpolation and comparison of MSE before and after