Spring Boot Integration with Redis and Redis Configuration
Introduction
When talking about NoSQL databases, caching systems, message queues, token-based authentication, and similar use cases, Redis is almost always part of the discussion.
As an in-memory data structure store written in C, Redis is widely known for its high performance and simplicity. Thanks to its single-threaded design, it avoids lock contention issues common in multi-threaded environments, enabling it to handle hundreds of thousands of read and write operations per second.
In addition, Redis provides distributed solutions such as master–replica mode, Sentinel mode, and Cluster mode. These features improve availability and scalability while maintaining high performance, making Redis an indispensable component in modern application development.
Integrating Redis with Spring Boot
With the help of various starters in Spring Boot, configuring Redis becomes quite straightforward.
Step 1: Add Dependencies in pom.xml
1 | <dependency> |
spring-boot-starter-data-redisis the Spring Boot dependency for Redis. It includes the default Redis client Lettuce.- Redis clients can be either Lettuce or Jedis. Lettuce is the default, supports asynchronous operations, and performs better in concurrent scenarios. It is generally recommended.
jackson-databindis used for configuring Redis serialization later.commons-pool2is used when configuring Redis clusters.
Redis Configuration
1 |
|
Serialization Configuration
Before customizing Redis serialization, we should first understand why customization is necessary. Can Redis work without explicitly configuring a serializer?
Yes, it can.
By default, RedisTemplate and RedisCache use JdkSerializationRedisSerializer, which relies on Java’s built-in serialization mechanism (classes implementing Serializable).
However, there is a downside.
When data is stored in Redis using JDK serialization, the stored value is in binary format and not human-readable. This makes debugging and inspection more difficult.
For example, if we store a key-value pair ("k1", "v1") using the default serializer and inspect it via redis-cli, we will see a binary-encoded value with hexadecimal prefixes added by the JDK serialization mechanism.
To retrieve the original value, we must use:
1 | redisTemplate.opsForValue().get("k1"); |
Custom Serialization Options
Two commonly used serializers:
GenericJackson2JsonRedisSerializerJackson2JsonRedisSerializer
Both can correctly serialize:
- String
- Object
- Collections
- JSONObject
- JSONArray
Key Difference
When using GenericJackson2JsonRedisSerializer, an additional @class property is added to each serialized object. The value of this property is the fully qualified class name.
Jackson2JsonRedisSerializer does not include this @class metadata. As a result, forced type casting may cause errors during deserialization.
For this reason, GenericJackson2JsonRedisSerializer is generally recommended.
ObjectMapper Configuration
An ObjectMapper must be configured for GenericJackson2JsonRedisSerializer; otherwise, serialization of JSONObject may fail.
1 | objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); |
The setVisibility method configures the access level for fields and methods during serialization.
1 | objectMapper.activateDefaultTyping( |
activateDefaultTyping enables proper serialization and deserialization when handling polymorphic types.
LaissezFaireSubTypeValidatoris a permissive type validator.instanceis a static member returning a singleton instance.DefaultTyping.NON_FINALspecifies that type information should be included for all non-final classes.
Encapsulating a RedisUtils Class
1 |
|
This utility class provides simple set, get, and getAllKeys methods.
Test Class
1 |
|









