Spring Data Redis

Serializers

SerializerDescriptionProsCons
GenericJackson2JsonRedisSerializerUses Jackson JSON library for serialization and deserialization.- Supports JSON format, widely used and flexible.
- Provides customization options.
- Good performance.
- Requires the Jackson library as a dependency.
- JSON-specific, may not be suitable for non-JSON data formats.
GenericToStringSerializerSerializes objects to their string representation using the toString() method.- Simple and easy to use.
- No additional dependencies required.
- Limited flexibility and customization.
- Performance may be slower compared to binary serializers.
Jackson2JsonRedisSerializerSimilar to GenericJackson2JsonRedisSerializer, uses Jackson JSON library for serialization and deserialization.- Supports JSON format, widely used and flexible.
- Provides customization options.
- Good performance.
- Requires the Jackson library as a dependency.
- JSON-specific, may not be suitable for non-JSON data formats.
JdkSerializationRedisSerializerDefault serializer provided by Java, uses Java’s built-in serialization mechanism.- Easy to use, no additional dependencies required.
- Supports any serializable Java objects.
- Lower performance compared to other serializers.
- Serialized data is not human-readable and not cross-platform compatible.
OxmSerializerUses JAXB (Java Architecture for XML Binding) for XML serialization and deserialization.- Supports XML format and Java object-to-XML mapping.
- Customizable using JAXB annotations.
- Requires JAXB as a dependency.
- XML-specific, may not be suitable for non-XML data formats.

@class

  • The @class field in Spring Data Redis is used to store the fully-qualified class name of an object. when it is serialized and stored in Redis. This field is added by Spring Data Redis to the serialized object to help with deserialization.
  • When an object is retrieved from Redis, Spring Data Redis uses the @class field to determine the type of the object and deserialize it accordingly. This is necessary because Redis stores all data as a simple key-value pair, so there is no inherent type information available when retrieving an object.
  • By including the @class field in the serialized data, Spring Data Redis can ensure that the correct deserialization logic is used for each object, even if the object type changes over time. This makes it possible to evolve the data model of an application without breaking compatibility with existing data in Redis.
  • It’s worth noting that the @class field is specific to Spring Data Redis and is not part of the Redis protocol or data model. If you are using Redis directly (without Spring Data Redis), you would need to handle serialization and deserialization yourself and find a way to include type information in your serialized data.