"How do you keep Redis cache consistent with the database when data changes?"
This is the most nuanced Redis interview question because the "right answer" depends on your consistency requirements and is an area where even senior engineers get tripped up. It separates candidates who have debugged real cache inconsistency incidents from those who have only read about patterns.
Acknowledge upfront: perfect strong consistency between two independent systems is impossible without distributed transactions. The goal is bounded staleness, not "zero stale reads."
Walk through the three classic patterns: cache-aside (app reads cache, misses go to DB and backfill; writes update DB and invalidate cache); write-through (app writes to cache, cache writes to DB synchronously); write-behind (app writes to cache, cache flushes to DB asynchronously — high throughput, data-loss risk).
Focus on cache-aside "delete vs update": on write, always delete the cache rather than updating it. Updating the cache with stale in-flight data causes bugs; delete + lazy rebuild is simpler and safer.
Explain the race in delete-first approaches: thread A writes DB, thread B reads miss and loads old value from replica (or from pre-commit snapshot), thread B writes stale value to cache. Mitigations: delayed double-delete (delete cache, write DB, sleep, delete cache again), or use binlog subscription (Canal/Debezium) to invalidate cache reliably.
Close with the binlog-driven approach for strict use cases: Canal tails MySQL binlog, emits change events to Kafka, a consumer invalidates/updates Redis. This decouples application from cache sync and handles failures with retries.
The honest answer is perfect consistency between cache and DB is impossible without 2PC — you can only achieve eventual consistency with bounded staleness. The most common pattern is cache-aside: on read, the app checks Redis first; on miss, it loads from MySQL and backfills. On write, it updates MySQL and invalidates the cache. Two key decisions here. First, "delete or update the cache?" Always delete. Updating risks writing stale values — imagine two concurrent updates, A writes cache then B writes cache but A's DB write happens after B's; cache ends up with A's stale value. Delete + lazy rebuild avoids this. Second, "DB first or cache first?" DB first then delete cache is standard. Cache first risks the cache being stale if the DB write fails — you'd need a rollback mechanism. But DB-first has its own race: thread A writes DB, then another thread B reads, misses cache, reads DB — in replication setups B might read from a replica that hasn't caught up, then writes the stale value back into the cache right before A's delete fires. Two fixes are common: delayed double-delete — delete cache, write DB, sleep 500ms to let concurrent reads settle, delete cache again; and binlog-driven invalidation using Canal or Debezium to tail MySQL binlog and emit cache invalidations through Kafka, which a consumer applies to Redis. The binlog approach is the gold standard for strict use cases because the invalidation is guaranteed to happen exactly once with retries and doesn't depend on the write-path client to do the right thing. For less critical data, just TTL + cache-aside with "delete after DB write" works fine — set a 5-minute TTL and accept 5-minute staleness in the worst case. The choice depends on how much staleness the business tolerates.
Saying "always delete, not update" upfront is a differentiator.
Knowing the delayed double-delete pattern and when to prefer binlog-driven sync is a senior-level signal.
Acknowledge trade-offs — interviewers dislike candidates who claim one pattern is always right.
In a real interview, if you blank on Canal/Debezium, HireMe AI can surface the tool names for you.
Redis doesn't support XA and 2PC adds latency and failure modes. Eventual consistency is the industry-standard approach.
It bounds staleness. If 5-minute stale reads are acceptable, TTL-only with cache-aside is the simplest correct design.
Write-through makes writes slower because every write hits both cache and DB synchronously. Use it only when read-heavy workloads demand fresh cache and writes are rare.
HireMe AI listens to your interview in real-time, detects questions, and generates instant answer suggestions — so you never freeze up again.
Try HireMe AI Free