Video in progress

How do you ship a deploy where the cached object's class changes (a field name or type changed) without breaking the cache?

During a rolling deploy, the old and new versions read and write the same keys, so the format must be compatible in both directions, or a schema version must go into the key. My default approach is a version in the key like v2: the new code writes to new keys, and the old ones die through the TTL.

📖 Answer

A typical incident: ProductDto.price went from long to BigDecimal. A rolling deploy starts. The new pods write the cache in the new format, and the old pods can't read it and return 500. Or the other way around. If you roll back, it happens once more.

The options:

  1. A version in the key.
RedisCacheConfiguration.defaultCacheConfig()
        .prefixCacheNameWith("catalog:v3:")   // v4 if the schema changes
        .entryTtl(Duration.ofMinutes(30));

The new version fills its own cache from scratch. Simple and reliable. The cost: a cold start for this cache during the deploy; with a big cache you need DB protection and a staged rollout. On a rollback the old keys may still be alive, which is good.

  1. A format compatible in both directions (expand/contract), as in DB migrations:
  • in release 1 you add the new field and also keep writing the old one; in Jackson, FAIL_ON_UNKNOWN_PROPERTIES = false makes the old code ignore the new field;
  • in release 2 you read from the new field;
  • in release 3 you remove the old one.

The cache stays fully warm, but it takes three releases and discipline. It's worth it only for very large caches that are expensive to re-warm.

  1. Treat a deserialization error as a miss: in a CacheErrorHandler or your own serializer, return null on error and reload from the DB. This is a safety net that should exist in every case: a format error should never turn into a 500. But it doesn't stop the two versions from continuously overwriting each other's entries (ping-pong).

At the platform level I require this: a "serialization compat" test for cached DTOs. A JSON sample from the previous version is kept in the repository, and the test checks that the new code can read it. JDK serialization makes this kind of evolution almost impossible, so JSON or a schema-based format (Protobuf) is mandatory.