RedisTemplate is Spring Data Redis's main class for working with Redis from Java. It wraps a Redis connection, serializes your keys and values, and exposes typed operations for strings, hashes, lists, sets, sorted sets, streams and more, so you can read and write Redis data without handling connections yourself.
In Spring Boot, spring-boot-starter-data-redis auto-configures a RedisTemplate and a StringRedisTemplate that you can inject straight away. The template is thread-safe and meant to be a singleton.
RedisTemplate<K, V> sits on top of a RedisConnectionFactory (Lettuce by default in Spring Boot, Jedis optionally). For each call it obtains a connection, serializes the arguments, runs the command, deserializes the result and releases the connection.
It also provides:
opsForValue(), opsForHash(), opsForList(), opsForSet() and opsForZSet().execute(SessionCallback), multi()/exec()).expire, delete, hasKey, keys.DataAccessException hierarchy.Add the starter:
Then configure the connection in application.yml:
In Spring Boot 3.x the properties live under spring.data.redis; the older spring.redis prefix was removed in 3.0.
Boot gives you two beans:
| Bean | Type | Serialization |
|---|---|---|
redisTemplate | RedisTemplate<Object, Object> | JDK serialization for keys and values |
stringRedisTemplate | StringRedisTemplate | UTF-8 strings for keys and values |
The default JDK serialization writes opaque binary keys such as \xac\xed\x00\x05t\x00\x04user, which you cannot read in redis-cli and which breaks across class changes. For that reason, either use StringRedisTemplate or define your own typed template with readable serializers.
Keys stay human-readable and values are stored as JSON. Newer Spring Data Redis lines add Jackson 3 based serializers, so use the class that matches your version.
The set call with a Duration writes the value and its time to live in one step. Always set a TTL on cache data so Redis does not fill up with stale entries.
If you only want caching, you rarely call the template directly. Enable caching and Boot uses Redis behind @Cacheable:
@Cacheable("products") on a service method then stores results in Redis through RedisCacheManager.
StringRedisTemplate fixes the serializers to strings, which is ideal for plain text and easy to inspect. RedisTemplate is generic and uses JDK serialization by default unless you configure serializers.
Yes. Configure it once as a bean and share it. Connections come from the factory per operation.
The default JDK key serializer wrote binary bytes. Set a StringRedisSerializer for keys and hash keys.
Lettuce is the Boot default and shares one thread-safe connection across threads. Use Jedis only if you need it for compatibility.
RedisTemplate is the Spring Data Redis class for typed Redis operations. Boot auto-configures it, but you should set string key serializers and a JSON value serializer, use StringRedisTemplate for text, and give cached entries a TTL.