Cache Update Strategies: Analysis, Drawbacks, and Improvement Solutions

This article examines three common cache‑update strategies, explains their concurrency and performance drawbacks, and proposes practical improvement techniques such as delayed double‑delete, asynchronous retries, and binlog‑based messaging to maintain consistency between cache and database.

Java Captain
Java Captain
Java Captain
Cache Update Strategies: Analysis, Drawbacks, and Improvement Solutions

Cache is widely used for its high concurrency and performance, but updating the cache consistently with the database remains a challenge. This article analyzes three typical update patterns, evaluates their disadvantages, and offers concrete solutions.

1. Update database first, then update cache – This approach can cause stale data when concurrent requests update the cache out of order, and it wastes resources when write‑heavy workloads repeatedly refresh the cache.

2. Delete cache first, then update database – A read request may fetch stale data from the database before the write completes, leading to inconsistency. The article recommends a delayed double‑delete strategy, illustrated by the following pseudo‑code:

public void write(String key,Object data){
    redis.delKey(key);
    db.updateData(data);
    Thread.sleep(1000);
    redis.delKey(key);
}

The delay (e.g., 1 second) should be tuned based on the expected read‑operation latency plus a safety margin.

3. Update database first, then delete cache (Cache‑Aside pattern) – While this pattern avoids most race conditions, it can still produce dirty data if the delete operation fails or if read‑write‑splitting causes stale reads. The article suggests adding cache expiration, asynchronous delayed deletion, or retry mechanisms.

For cases where the second delete fails, two robust solutions are presented:

Send the failed key to a message queue and retry deletion until it succeeds.

Subscribe to the database binlog (e.g., using Canal for MySQL), extract the affected keys, and retry deletion via a background worker.

Overall, the article provides a comprehensive summary of existing consistency solutions and practical advice for implementing reliable cache‑update workflows in backend systems.

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.

CacheConsistencystrategy
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java 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.