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.

dbaplus Community
dbaplus Community
dbaplus Community
How to Keep Redis Cache and MySQL Consistent: Strategies and Pitfalls

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

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

performancecaching strategiescache-asidedata-consistency
dbaplus Community
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.