Learning center

Keep old and new cache entries consistent when both releases write

With a versioned cache key, each release deletes the entry it knows about when the underlying data changes. The old release does not know the new key, so its writes leave the new release's entry stale until it expires. In our lab, the old release changed a phone number, and the new release showed the old number for the rest of the entry's hour. Ship the deletion of both keys one release before the new key is read.

The stale entry#

Release 2026.11.3 caches users under user:{id} and deletes that key after changing a user. Release 2026.11.4 caches under user:v2:{id}, as change a cached object's format during a rolling deploy recommends. Both run during the rollout. We ran this against Redis 7.0.15 from Python 3.11:

def old_update_phone(user_id, phone):            # 2026.11.3
    db_update_phone(user_id, phone)
    redis.delete(f"user:{user_id}")

Both releases had read user 42, so both keys held +49 30 1234. Then an old-release pod saved a new number:

old_update_phone(42, "+49 30 9999")
ReaderPhone shownEntry's remaining lifetime
2026.11.3+49 30 9999rebuilt
2026.11.4+49 30 12343,600 s

The user changed their number on one request and saw the old one on the next, whenever the load balancer sent them to a new-release pod. Nothing failed, and the cache's hit rate looked healthy.

Delete both keys first#

The old release cannot learn a key name after it has shipped, so the fix has to be in place before the new key exists:

  1. Ship a release that still reads and writes user:{id}, and deletes both user:{id} and user:v2:{id} on every write:

    redis.delete(f"user:{user_id}", f"user:v2:{user_id}")

    Deleting a key that does not exist costs one round trip and does nothing else.

  2. When that release is the oldest one that can run, including as the rollback target, ship the release that reads user:v2:{id}. It deletes both keys too.

Every write by either release now removes both entries. A rollback to step 1's release keeps the new entries current as well, so rolling forward again does not serve stale data.

If step 1 is too late#

When the new key is already in use and the old release cannot be changed, shorten the new entry's lifetime for the rollout, for example to a minute, so a stale entry ends quickly. The cost is a lower hit rate. Restore the normal lifetime when the old release is gone. Another way out is a cache that both releases can invalidate through a shared signal, such as a per-user version number stored in the database, but that also has to ship in the old release first.

The compatibility test#

With both releases on one Redis in staging, cache a user from each, change the user through each release, and read from the other. Pass when every read after a change shows the new value. The general sequence for data in two shapes is in moving data while two application releases share one database.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading