How to Keep Redis Cache and MySQL Consistent: Strategies and Pitfalls
This article explains why Redis is used as a cache, outlines common consistency problems such as cache miss, penetration, and avalanche, compares cache‑aside, read‑through, write‑through and write‑behind patterns, and presents practical solutions like delayed double‑delete, retry mechanisms and binlog‑based asynchronous deletion to maintain eventual consistency between cache and database.
Database and Cache Consistency
Consistency means the value stored in the cache equals the value in the database. When a cache miss occurs, the database must provide the latest value.
Cache value = Database value
Cache miss → read from database
Inconsistency occurs when the cache value differs from the database value or when stale data remains in either store.
Cache Access Patterns
Four common caching patterns are used to improve read‑write performance:
Cache‑Aside (旁路缓存) : The application reads/writes both the cache and the database directly.
Read‑Through: On a miss the cache component loads data from the database, writes it back, and returns it.
Write‑Through: Writes go to the cache first, which synchronously updates the database.
Write‑Behind: Writes go to the cache first and the cache updates the database asynchronously.
Cache‑Aside Details
Read flow
Application checks the cache.
If the key is present, return the cached value.
If the key is absent, query the database, write the result to the cache, then return the value.
Write flow
Write the new data to the database.
Invalidate (delete) the related cache entry.
Typical Java‑style pseudocode:
String cacheKey = "myKey";
String cacheValue = redisCache.get(cacheKey);
if (cacheValue != null) {
return cacheValue; // cache hit
} else {
cacheValue = getDataFromDB(); // cache miss
redisCache.put(cacheKey, cacheValue);
return cacheValue;
}Read‑Through
When a cache miss occurs, the cache itself fetches the data from the database, stores it, and returns it. The application only interacts with the cache, achieving separation of concerns.
Write‑Through
Writes are performed by the cache component, which synchronously updates the database. This eliminates application‑level failure handling but adds latency because each write must wait for the database commit.
Write‑Behind
Writes are first stored in the cache; the cache updates the database asynchronously. This improves write throughput but sacrifices strong consistency, making it unsuitable for scenarios that require up‑to‑date data.
Consistency Issues Under Cache‑Aside
Four write‑order scenarios are examined:
Update cache → update database
Update database → update cache
Delete cache → update database
Update database → delete cache
Analysis shows that only the last scenario (update database then delete cache) avoids most inconsistency problems, especially under high concurrency.
Consistency Solutions for Cache‑Aside
Delayed Double Delete : Delete the cache, write to the database, wait a configurable interval (commonly ~500 ms), then delete the cache again. The pause ensures that any stale value repopulated by a concurrent read is removed.
Delete‑Retry Mechanism : After the first delete, retry the cache deletion a fixed number of times (e.g., three attempts). If all retries fail, log the incident and optionally push a message to a message queue for asynchronous handling.
Asynchronous Binlog Deletion : Use a binlog listener such as Canal to capture MySQL binlog events, extract the affected keys, and delete the corresponding cache entries. Failed deletions are retried via a message queue.
All three mechanisms aim to keep the cache eventually consistent with the database while tolerating brief periods of stale reads.
Best Practices
Prefer the Cache‑Aside pattern for most use‑cases.
When writing, always update the database first, then delete the cache entry (do not update the cache directly if the write cost is high).
Apply delayed double delete or a robust retry mechanism to handle cache‑deletion failures.
Accept eventual consistency (BASE) rather than attempting strong consistency, which conflicts with the CAP theorem’s AP trade‑off for caching layers.
References
https://docs.aws.amazon.com/whitepapers/latest/database-caching-strategies-using-redis/caching-patterns.html
https://codeahoy.com/2017/08/11/caching-strategies-and-how-to-choose-the-right-one/
https://blog.cdemi.io/design-patterns-cache-aside-pattern/
https://hazelcast.com/blog/a-hitchhikers-guide-to-caching-patterns/
https://developer.aliyun.com/article/712285
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
dbaplus Community
Enterprise-level professional community for Database, BigData, and AIOps. Daily original articles, weekly online tech talks, monthly offline salons, and quarterly XCOPS&DAMS conferences—delivered by industry experts.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
