Which Cache Update Strategy Guarantees Consistency? A Deep Dive into DB‑Cache Synchronization

This article examines three common cache‑update approaches—updating the cache after the database, deleting the cache before updating the database, and updating the database then deleting the cache—analyzes their drawbacks, and presents practical solutions such as delayed double‑delete and retry mechanisms to ensure data consistency.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Which Cache Update Strategy Guarantees Consistency? A Deep Dive into DB‑Cache Synchronization

Why Write This Article?

Cache is widely used for high‑concurrency reads, but updating cache after database changes is controversial. This article analyzes three update strategies, their drawbacks, and improvement methods.

Article Structure

Explain cache update strategies

Analyze drawbacks of each

Provide improvement solutions

Main Content

Three strategies are discussed:

Update database then update cache

Delete cache then update database

Update database then delete cache

Strategy 1: Update DB → Update Cache

Problems: thread‑safety issues can cause dirty data when concurrent requests update cache in different order; also inefficient for write‑heavy workloads.

Strategy 2: Delete Cache → Update DB

Can cause inconsistency because a read request may fetch stale data from the database before the write completes. Solution: delayed double‑delete – delete cache, write DB, sleep ~1 s, delete cache again.

Strategy 3: Update DB → Delete Cache

Based on the Cache‑Aside pattern; still vulnerable to race conditions, but probability is low. Mitigations include setting cache TTL or using the delayed double‑delete with asynchronous second delete.

Handling Delete Failures

Two retry mechanisms are proposed: (1) send failed keys to a message queue and retry until success; (2) use MySQL binlog subscription (e.g., Canal) to capture changes and retry deletions.

Conclusion

The article summarizes existing consistency solutions for internet‑scale systems and suggests practical ways to avoid cache‑DB inconsistency.

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.

BackendDistributed SystemsCacheConsistencycache invalidation
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

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.