Cache Consistency Challenges and Practical Strategies for Database‑Redis Integration
The article explains why high‑traffic systems store data in databases but use fast caches like Redis, outlines the inherent consistency problems between cache and database, evaluates four update‑delete strategies with concurrency scenarios, and recommends practical solutions such as post‑update cache eviction, delayed double deletion, expiration policies, message queues, and binlog listeners to achieve eventual consistency.
In real‑world business scenarios, core data such as orders, members, and payments are persisted in relational databases (e.g., MySQL) for transactional guarantees, but the databases often cannot handle the high QPS demanded by large‑scale traffic, leading developers to introduce a fast cache layer (typically Redis) to serve the majority of read requests.
Cache significance : caching trades space for time by storing frequently accessed, relatively static data in a high‑speed storage medium, thereby improving overall read performance.
Consistency challenge : when data exists simultaneously in Redis and MySQL, any delay between updating the database and synchronizing the cache creates a window of inconsistency. Because there is no transactional guarantee across the two systems, writes may succeed in MySQL while the corresponding cache update fails, or the order of operations may cause stale values to be read.
Four common write‑path strategies are examined:
Update database then update cache – can cause temporary stale reads during the short interval before the cache is refreshed.
Update database then delete cache – generally safe; the stale‑read window is minimal (≈1 ms) and often negligible.
Update cache then update database – risky because if the database update fails, the cache holds incorrect data.
Delete cache then update database – may lead to read‑write race conditions where a read request repopulates the cache with old data before the database commit.
Sample code for the typical read‑through pattern:
data = queryDataRedis(key);
if (data == null) {
data = queryDataMySQL(key); // cache miss, query DB
if (data != null) {
updateRedis(key, data); // populate cache after DB read
}
}Examples of problematic write sequences:
updateMySQL();
updateRedis(key, data);and
updateRedis(key, data); // update cache first
updateMySQL(); // then update DBTo mitigate these issues, the article recommends the "post‑update delete" strategy (update DB then delete cache) for read‑heavy workloads, and the "update‑then‑update" strategy for write‑heavy scenarios. It also suggests using short cache TTLs (e.g., 1 minute) to bound the maximum inconsistency window, employing reliable message queues (at‑least‑once delivery) to asynchronously invalidate caches, and leveraging MySQL binlog listeners (e.g., Canal) to react to actual data changes and keep caches in sync.
For complex cases where a single DB record maps to multiple cache keys, the article proposes publishing cache‑invalidating or updating events to a message bus, allowing dedicated services to handle all related keys consistently.
Overall, the recommended practice is to aim for eventual consistency: ensure that any cache entry that becomes stale is either quickly evicted or refreshed, using a combination of expiration, delayed double deletion, MQ‑driven invalidation, and binlog‑based synchronization.
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.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.
