MySQL and Redis Dual-Write Consistency: Cache-Aside, Delayed Double Delete, and the Binlog Endgame
Stale cache data is a production incident, an interview topic, and an architecture decision all at once; the choice between a simple Cache-Aside with TTL and a full Canal/binlog pipeline determines how much latency, complexity, and dirty-data risk a team accepts.
Every backend team eventually hits the same phantom: the database says in stock, Redis says sold out. The cause is dual-write inconsistency between MySQL and Redis, and the standard remedy is Cache-Aside: update the database first, then delete the cache key after the transaction commits. The article walks through the race conditions that make the naive version unsafe, including the transaction-boundary trap where deletion runs before commit, and explains why delete beats update for invalidation.
When the basic pattern is not enough, two hardening layers apply. Delayed double delete closes the concurrent read-write backfill window with a second, asynchronously delayed deletion. For deletion failures, MQ retry or a local retry table written in the same transaction as the business SQL guarantees the eviction eventually happens. The terminal architecture is Canal subscribing to MySQL binlog: business code only writes the database, Canal feeds change events to MQ, and consumers delete cache keys with idempotency, retry, and dead-letter handling.
The article also covers read-side protections (TTL with jitter, mutex rebuild, Bloom filters, logical expiration), the cost of forcing strong consistency with Redisson read-write locks, and a selection matrix matching business tiers to solution combinations.
The article's core stance is that absolute consistency between MySQL and Redis is the wrong goal; the design objective is to shrink the inconsistency window until the business cannot feel it and let TTL converge the rest.
The local retry table is a genuinely useful pattern: putting the eviction task in the same transaction as the business update turns a two-phase problem into one atomic decision, at the cost of an extra table and scheduled job.
Binlog subscription changes the trust boundary: business code no longer needs to remember to invalidate cache, and even ad-hoc database edits or batch scripts get captured, which manual dual-write cannot match.
The article is aimed squarely at interview preparation and production troubleshooting in Chinese backend teams, so its recommendations are tuned to common Java/Spring stacks with Redisson, RocketMQ, RabbitMQ, and Canal.
Many of the failure modes described, like the transaction-boundary deletion trap and primary-replica lag backfill, are subtle enough that teams often discover them only after an incident.