How do you keep Redis cache and database data consistent

  1. Update the cache first, then update the database
  2. Update the database first, then update the cache
  3. Delete the cache first, then update the database
  1. Update the database first, then delete the cache
  2. Delayed double delete: Delete the cache, update the database, then delete the cache again after a delay.

Why delete the cache instead of updating it?

Updating the cache has two pitfalls:

  1. The cached value may need complex computation to produce, and recomputing it on every update wastes resources. It’s cheaper to delete it and recompute on the next read.
  2. With concurrent updates, you can’t control the order. A sets the DB to 10 and B sets it to 20, but network jitter makes B’s cache update land first and A’s land later. The DB ends up at 20 while the cache holds 10.

Update Cache, then Update Database

Request A updates the cache to 10, then Request B updates the cache to 20. Because of network delays, B’s DB update runs first and A’s runs after. The DB ends up at 10 and the cache at 20, so they don’t match.

Worse, if the DB write fails after the cache is updated, the cache holds the new data and the DB holds the old data. There’s no transaction that can roll the cache back.

Update Database, then Update Cache

Same problem as above: concurrency plus network delay scrambles the order. A updates the DB first and the cache later, while B updates the DB later but the cache first. Again they end up out of sync.

Problems with deleting the cache first, then writing the DB

This scenario is more common:

  1. Request A deletes the cache and is about to update the DB.
  2. Request B reads, finds the cache empty, reads the old value 10 from the DB and writes it back to the cache.
  3. Request A updates the DB to 20.
  4. The DB now holds 20 and the cache holds 10, so every later read gets stale data.

Delayed double delete (delete the cache, write the DB, then delete the cache again after a delay)

To fix the concurrency problem with “delete the cache first, then update the database,” you delete the cache once more, after a delay, once the database update is done. Even if another request writes stale data back to the cache in between, the delayed delete clears it out.

The delay is usually set to the longest time a business read takes, plus a few hundred milliseconds (for example, 1 second). You can implement the delayed delete with a message queue, a scheduled task, or Redisson’s delayed queue:

Update the database first, then delete the cache

This is the most widely used approach in industry, also known as the Cache Aside Pattern. You update the database first and then delete the cache. The next read finds the cache empty, loads the latest data from the database and writes it back to the cache.

This approach can still fail in edge cases:

  1. The cache has just expired.
  2. Request A reads, misses the cache and reads the old value from the database.
  3. Request B updates the database and deletes the cache.
  4. Request A writes the old value back to the cache.

But the chance of this happening is extremely low. It requires the write to finish between the read’s “query the database” and “write the cache” steps, and writing to a database is usually much slower than writing back to a cache. It almost never happens in real systems, so this approach gives the best cost-benefit balance.