Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To use Redis with Spring’s cache annotations, add Spring Data Redis and Spring Boot’s caching support, configure a Redis connection, enable caching with @EnableCaching, then annotate suitable service methods with @Cacheable. Spring Boot can auto-configure a Redis-backed cache manager when Redis is available and configured; set an expiration explicitly because entries do not expire by default.

How Spring Cache connects to Redis

Spring’s cache annotations are an abstraction: they describe when a method result can be stored or reused, while a cache manager supplies the storage implementation. Spring Data Redis provides RedisCacheManager, which connects those annotations to Redis. Spring Boot 3.4 documents automatic configuration of that manager when Redis is available and configured (Spring Boot 3.4 caching reference).

Use the dependency-management setup for your Spring Boot release rather than independently selecting Spring Data Redis versions. Configure the Redis connection using the application’s standard Boot Redis properties or a connection factory. Exact dependencies and property names should be checked against the documentation matching your Boot version.

Quick setup using Spring Boot properties

  1. Add caching and Redis support. Include Spring Boot’s caching starter and Spring Data Redis through the dependency management used by your project.
  2. Configure a Redis connection. Supply the connection settings through your application’s standard Spring Boot Redis properties, or configure a connection factory.
  3. Enable annotation-driven caching. Add @EnableCaching to a Spring configuration class or application class.
  4. Choose cache names and expiration. For a ten-minute example cache, the Spring Boot 3.4 reference shows:
    spring:
      cache:
        cache-names: "products"
        redis:
          time-to-live: "10m"
  5. Annotate a suitable service method. For example: @Cacheable(cacheNames = "products", key = "#id"). Apply it to a Spring-managed method whose result is safe to reuse for the same key.

The YAML configures a ten-minute TTL for configured caches; it is an illustrative value, not a universal freshness recommendation. The annotation is an example of the API shape, not a tested application. Confirm the exact dependencies, property names, and APIs against the versions in your project.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose between Boot defaults and custom configuration

For a straightforward cache, start with Boot’s auto-configured RedisCacheManager and properties. Add a custom RedisCacheConfiguration or manager when you need explicit serializers, distinct settings for named caches, null-value handling, or a different clearing strategy. A custom manager can be created with RedisCacheManager.create(connectionFactory), and more explicit construction supports default and per-cache settings. Avoid defining a custom manager just to repeat Boot’s defaults.

Approach Best fit Trade-off
Boot auto-configuration and properties A conventional Redis cache with shared settings such as cache names and TTL. Less setup; use custom configuration when individual caches need different behavior.
Custom Redis cache configuration or manager Explicit per-cache TTLs, serializer choices, null handling, or clearing behavior. More control, with more configuration to maintain and verify against the project’s Spring Data Redis version.

Spring Data Redis 4.0.7 documentation describes the cache manager and configuration surface, while noting 4.1.1 as the latest stable release at the time of that documentation. The Spring Data Redis 4.1.0 API lists methods including entryTtl(Duration), serializeKeysWith(...), serializeValuesWith(...), disableCachingNullValues(), and computePrefixWith(...). Use the version managed by your chosen Boot release and its matching API documentation (Spring Data Redis cache reference; RedisCacheConfiguration 4.1.0 API).

Set cache names, keys, and prefixes deliberately

A cache name identifies a group of entries, while a key identifies an entry within that group. In @Cacheable(cacheNames = "products", key = "#id"), the cache is products and the key comes from the method’s id argument. Choose keys that distinguish every input that can change the method result; otherwise, different calls can reuse the same cached value.

Spring Data Redis enables cache-name prefixes by default, using the cache name as the default prefix. Keep prefixes unless you have a specific reason not to: they reduce collisions when separate caches contain equal keys. The Boot and Spring Data Redis references describe this default (Spring Boot caching reference; Spring Data Redis cache reference).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Understand defaults before relying on them

  • Expiration: entries have no expiration by default. Configure a TTL if stale data must eventually be removed.
  • Null values: nulls are cached by default. A custom configuration can use disableCachingNullValues() to turn that off.
  • Serialization: keys use StringRedisSerializer by default; values use JdkSerializationRedisSerializer. Choose a different value serializer only when its data contract and compatibility needs are clear, and ensure all readers and writers agree on the representation.
  • Writer behavior: the default writer is non-locking. It favors throughput but does not make operations involving multiple Redis commands atomic. The manager is not transaction-aware by default.
  • Statistics: cache statistics are disabled by default. The builder can enable local hit/miss statistics, but these are local snapshots rather than a complete distributed observability solution.

These defaults and configuration options are documented in the Spring Data Redis cache reference and the RedisCacheConfiguration API.

Choose ordinary TTL or TTI-like expiration

TTL: expiration based on writes

With an ordinary TTL, creating or updating an entry starts or resets its expiration interval. Reading it does not reset the timer, so a frequently read entry can still expire if it is not rewritten before the interval ends. Set a fixed duration with entryTtl(Duration); Spring Data Redis also supports a dynamic per-entry TTL function since version 3.2.0 (Spring Data Redis cache reference).

TTI-like behavior: expiration refreshed by cache reads

Time-to-idle behavior is opt-in and still requires a TTL. Spring Data Redis simulates it by issuing Redis GETEX on cache reads, refreshing the expiration. Redis supports GETEX from version 6.2.0. On an older Redis server, enabling this behavior causes the command to fail.

Expiration refresh also depends on how the entry is read: cache reads can use GETEX, but plain RedisTemplate or repository reads may issue ordinary GET and leave the expiry unchanged. TTI-like behavior is therefore reliable only when the access paths that should refresh expiration actually participate in that mechanism (Spring Data Redis cache reference).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Plan for cache clearing and concurrent operations

The default cache-clearing strategy uses Redis KEYS and DEL. The Spring reference warns that KEYS can cause performance problems in large keyspaces. A SCAN-based batch strategy is available; the reference says it is fully supported with Lettuce, while Jedis support is limited to non-clustered modes. Select a strategy for the actual Redis driver and topology rather than assuming one configuration suits every deployment.

Because the default writer is lock-free, multi-command operations such as putIfAbsent and clean can involve overlapping, non-atomic commands. Do not treat @Cacheable(sync = true) as proof of a cluster-wide distributed lock; the coordination guarantees depend on the selected provider and require separate verification.

When this setup is a good fit

Use Redis-backed method caching for results that are safe to reuse for a defined period and whose keys reliably represent the inputs affecting the result. Before enabling it, decide how stale results may be, whether nulls should be cached, which serialization format the application will maintain, and how cache invalidation and clearing fit the Redis deployment. Spring’s cache abstraction and Spring Data Redis provide the mechanism; they do not decide whether a particular result is safe to cache.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.