Kubernate

Microservices # Caching (Stale Data) - Outdated or no longer valid info

 


Stale data in caching means outdated or no longer valid information stored in the cache that doesn’t reflect the current state of the underlying database or source of truth.


🧩 Detailed Explanation

🔹 What is Stale Data?

  • When you cache data, it’s essentially a snapshot of the original source at a given time.
  • If the source data changes (e.g., a user updates their profile, product price changes), but the cache isn’t updated, the cached entry becomes stale.
  • Serving stale data leads to inconsistency between what the client sees and what’s actually stored in the database.

🔹 Causes of Stale Data

  • Long TTL (Time-to-Live): Cached entries live too long without refresh.
  • Missing invalidation: Updates/deletes in DB don’t trigger cache eviction.
  • Distributed systems: Multiple services update data independently, but cache isn’t synchronized.
  • Write-behind strategy: Cache updates DB asynchronously → DB may lag behind cache.

🔹 Why Stale Data is a Problem

  • User confusion: Customers see outdated product prices or stock availability.
  • Business risk: Financial systems may show incorrect balances.
  • Data integrity issues: Analytics or reporting may use wrong values.

🔹 How to Handle Stale Data

  • Cache Invalidation: Remove or update cache entries when source data changes.
  • TTL Expiry: Set appropriate expiration times so cache auto-refreshes.
  • Event-driven updates: Use message brokers (Kafka, RabbitMQ) to notify services to evict/update cache.
  • Versioning: Add version/timestamp to cache keys; refresh when version changes.
  • Refresh-ahead: Proactively refresh cache before expiry to reduce stale reads.

✅ Interview-Ready Bullet Points

  • “Stale data is cached information that no longer matches the source of truth.”
  • “It happens when the database changes but the cache isn’t updated.”
  • “We handle it with TTLs, cache invalidation, event-driven updates, or versioning.”
  • “The goal is to balance performance with consistency so users don’t see outdated results.”

In short: Stale data = outdated cache entries. The fix is invalidation + refresh strategies to keep cache aligned with the source of truth.

Sources:

No comments:

Post a Comment

Spring Boot - Bean LifeCycle

 Here is a clear, step-by-step lifecycle of a Spring Boot application , explained in a simple + interview-ready way. 🔄 Spring Boot Applica...

Kubernate