Client-Side Caching

Client-side caching (also known as local or near caching) provides an additional layer of caching in the application’s memory, reducing latency and network traffic.

Overview

Client-side caching stores frequently accessed data in local memory, providing:

  • Lower latency: No network round-trip for cached data

  • Reduced load: Fewer requests to Redis

  • Cost savings: Less network bandwidth usage

Basic Configuration

Client-side caching can be enabled by setting localCache(Map<String, Object> cache).

For example with a simple java.util.Map implementation:

RedisCacheConfiguration config = RedisCacheConfiguration.defaultConfig()
    .localCache(new HashMap<>());

Using Caffeine

For more control over the behavior of the local cache it is recommended to use an in-memory cache implementation like Caffeine:

Cache<String, Object> localCache = Caffeine.newBuilder()
    .maximumSize(10000)
    .expireAfterWrite(Duration.ofMinutes(5))
    .build();

RedisCacheConfiguration config = RedisCacheConfiguration.defaultConfig()
    .localCache(localCache.asMap());

See the Caffeine Documentation for more configuration options.

Caffeine Features

Caffeine provides advanced features:

  • Size-based eviction: Limit cache size by entry count

  • Time-based eviction: Expire entries after a duration

  • Reference-based eviction: Use weak/soft references

  • Statistics: Track hit rates and performance

  • Async loading: Load values asynchronously

Metrics

When client-side caching is enabled, additional metrics are available:

  • cache.local.gets - Local cache hits and misses

  • cache.local.evictions - Local cache evictions

See Metrics for details.