RedisTemplate is the central class of Spring Data Redis. It gives you a typed, thread-safe API for running Redis commands from Spring, handles connections for you, and converts Java keys and values to bytes with pluggable serializers. You use it through operation views such as opsForValue(), opsForHash() and opsForZSet(), which map to Redis strings, hashes and sorted sets.
Spring Boot auto-configures a RedisTemplate<Object, Object> and a StringRedisTemplate when spring-boot-starter-data-redis is on the classpath. For caching specifically, Spring's cache abstraction with @Cacheable is usually simpler than using the template directly.
RedisConnectionFactory (Lettuce by default, Jedis optional) and releases it after each operation.RedisSerializer instances.DataAccessException hierarchy.MULTI and EXEC, scripting, and publish/subscribe.A single template instance is thread-safe once configured, so inject one and reuse it.
Add the starter and configure the server:
In Spring Boot 2.x these properties were under spring.redis.*; Boot 3 moved them to spring.data.redis.*.
Boot's default RedisTemplate<Object, Object> uses JdkSerializationRedisSerializer, which writes opaque Java-serialized bytes. That makes values unreadable in redis-cli, couples data to class versions, and is a security concern. Define your own template with explicit serializers:
String keys keep your data inspectable, and JSON values work across languages. GenericJackson2JsonRedisSerializer stores a class name with each value so it can read it back as the original type. Newer Spring Data Redis releases built on Jackson 3 add renamed equivalents, so check the names for your version. If you only store strings, inject StringRedisTemplate instead.
Always set a time to live on cache and session data, as above, so the keyspace does not grow without bound.
Other structures follow the same pattern:
| View | Redis type | Typical use |
|---|---|---|
opsForValue() | String | Cache entries, counters, locks |
opsForHash() | Hash | Objects with fields |
opsForList() | List | Queues, recent items |
opsForSet() | Set | Unique membership, tags |
opsForZSet() | Sorted set | Rankings, scheduled items |
opsForStream() | Stream | Event logs and consumer groups |
Pipelining sends many commands in one round trip and returns all results at the end:
For atomic groups, use a SessionCallback with multi() and exec(), so every command runs on the same connection. Setting setEnableTransactionSupport(true) lets @Transactional methods bind a Redis transaction, but be aware that reads inside a MULTI block return null until exec(). Redis transactions do not roll back on a command error; they only guarantee that the queued commands run together without interleaving.
keys("*") in production. It blocks the server; iterate with scan through the connection instead.StringRedisTemplate and RedisTemplate on the same keys with different serializers, which produces unreadable values.StringRedisTemplate is a RedisTemplate<String, String> preconfigured with string serializers for everything. Use it for text values and when you want readable data. Use RedisTemplate when you store objects.
Yes. With the default Lettuce client, connections are shared and thread-safe, and the template can be used from many threads. Avoid blocking commands and WATCH-based transactions on shared connections.
Use @Cacheable with RedisCacheManager for method-level caching. Use RedisTemplate when you need Redis data structures, counters, rate limiters, or full control over keys and expirations.
The default JdkSerializationRedisSerializer writes binary Java-serialized data. Switch to StringRedisSerializer for keys and a JSON serializer for values.
RedisTemplate is Spring Data Redis's main API for running Redis commands from typed Java code. Spring Boot sets up the connection and a default template, but you should configure string key serializers and JSON value serializers yourself. Use the opsFor* views for each data type, set expirations on cached data, pipeline bulk operations, and choose StringRedisTemplate or Spring's cache abstraction when they fit the job better.