Skip to content

Caching with Spring Boot & Redis

Furkan Kürşat Danışmaz edited this page Sep 19, 2018 · 4 revisions

Source Folder: Caching with Redis

Tech Stack:

  • spring-boot
  • spring-mvc
  • spring-data-redis
  • log4j2 (slf4j impl)
  • project lombok

This is a ready-to-use Maven Java project template with the libraries listed above.

Problems Solved

  • Reading data from cache (once it is read from the original source) until its TTL

Notes Taken on the Road

Create a Redis Docker Container

> docker pull redis
> docker run --name redis -p 6379:6379 -d redis

Jedis vs Lettuce

  • Both are mature redis clients
  • Lettuce is the default client that comes with the Spring-Boot-Starter-Data-Redis
  • Redis is single threaded in terms of data access
    • Jedis is not thread safe when applications want to share a single Jedis instance across multiple threads
    • The approach to use Jedis in a multi-threaded environment is to use connection pooling (each thread gets its own Jedis instance)
    • Lettuce is built in netty and connection instances can be shared across multiple threads

Redis Cache Manager In spring boot, RedisCacheManager is autoconfigured:

  • Spring cache configurations using "spring.cache.*"" property
  • redis cache configurations using "spring.cache.redis.*" property

NOTE: All default configurations of Spring-Boot are in the files:

spring-configuration-metadata.json
additional-spring-configuration-metadata.json