Spring Boot + Redis + PostgreSQL Caching
By: Erkan Güzeler
Tags: Cache, Web Development, Spring Boot, Redis, Web

Hi everyone,

In this post, I would like to show you how to use Redis for Caching with Spring Boot application.

Caching is a very important approach for web development. It helps you to give quick response for your request.

Table of Contents:

Getting Started

Redis is most popular tool to use for caching. It is widely usage in the web application development.

What is Redis?

Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache, and message broker. Redis provides data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes, and streams.

In this post, I have created a simple demo. It includes a Customer class. There are getAllCustomers(), getCustomerById() methods in Controller layer. These are cacheable. If you send requests for these methods, you can be wait for 3 seconds if Redis has no related data in memory. Scenario is like this. Let's begin.

Project Structure

Maven Dependencies

First of all, we need to add relevant dependencies to pom.xml file. I will use jedis client.

pom.xml :

Redis Configuration

I have created a configuration file for Redis configuration like below. I created redisCacheTemplate and cacheManager beans for Redis.

Spring Cache Annotations

I have used Spring Cache Annotations. I would like to explain you what they are.

@Cacheable triggers cache population

triggers cache population @CacheEvict triggers cache eviction

triggers cache eviction @CachePut updates the cache without interfering with the method execution

updates the cache without interfering with the method execution @Caching regroups multiple cache operations to be applied on a method

regroups multiple cache operations to be applied on a method @CacheConfig shares some common cache-related settings at class-level

Service Layer

I have created a service layer for customer. I have used Spring Annotaions for Caching. When you get all customers you will get all data from database if Redis has no related data. waitSomeSome() method is written for simulation. When add new Customer or update a Customer , Redis database related data will evict.

Docker & Docker Compose

I have created a Dockerfile and docker-compose.yml files for dockerize this application. I will use postgresql for RDBMS db. I added redis with cache tag to docker-compose.yml file. Docker compose is a very useful tool to run more than one containers.

Dockerfile :

docker-compose.yml :

Build And Run

Building and running this application, you need to run some cli commands like mvn and docker. Firstly, I made a fat jar for my application. After that I build my docker-compose file. Finally, you just need to up docker-compose file.

When you follow this road map, you application will be started successfully. You can just go to http://localhost:8080 or http://localhost:8080/swagger-ui.html . If you go to http://localhost:8080/swagger-ui.html link, you can see easily the endpoints of project and you can also test them with Swagger tool.

Build Java Jar.

Docker Compose Build and Run

Demo

I made a video on how to run and test this application on Youtube.