Search code examples
javac#serializationbackwards-compatibility

Redis object serialization backwards compatibility


We serialized the following java entity and stored into Redis:

public class Foo {
    public String bar;
}

then our application v2 modified the class Foo as:

public class Foo {
    public String bar;
    public Integer eggs;
    public Datetime happen;
}

Now when application v2 goes into production, in Redis, both Serialized Foo V1 and serialized Foo V2 are stored, and theirfore our application receives exceptions when deserialize objects from Redis. And now I've broken backwards compatibility.

It's not allowed to clean objects in Redis when our application upgrades from v1 to v2 because of business reasons.

Which approach is the best practice to avoid such backwards compatibility issue when design Redis data objects? Thanks.


Solution

  • I think you can fix it by configuring your JSON library

    If you are using Jackson library you have the option to ignore unknown properties by using

    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    

    and if you are using the gson library you can use a cool feature that it has as version, by annotating your java classes with @Version annotation and it gives you the flexibility to have two different version of JSON co-existing together

    Hope this helps