Monday, May 21, 2018

Implementing Redis with Spring boot

Redis is one of the most used inmemory cache server used in most of the distributed system for saving the key value data structures in memory for defined time interval along with some other features like pub sub and more.

So In this article you will learn how to use redis server and store the data in it while working on spring boot.

So lets start (You can Download the project from github
First we will create simple spring boot project 

Adding Dependency

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""


 <description>Demo project for Spring Boot</description>

  <relativePath /> <!-- lookup parent from repository -->




In pom.xml you will see there are 2 dependency for redis ,

After that you need to define configurations

package com.programinjava.learn.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.programinjava.learn.model.User;

public class RedisConfigurations {
 public JedisConnectionFactory jedisConnectionFactory() {
  JedisConnectionFactory jedisConFactory
       = new JedisConnectionFactory();
     return jedisConFactory;
 public RedisTemplate<String, User> redisTemplate(){
  RedisTemplate<String,User> redisTempate = new RedisTemplate<>();
  return redisTempate;

In above Configuration we have defined 2 beans
JedisConfigurationFactory : it will show where the redis server is running and where it need to connect with the server.

RedisTemplate: is to do execution and do crud operation for redis .

Adding Repository

public interface UserRespository extends CrudRepository<User, Integer> {


User Model Class 

package com.programinjava.learn.model;



public class User implements Serializable{
 private Integer id;
 private String name;
 private Integer age;
 public Integer getId() {
  return id;
 public void setId(Integer id) { = id;
 public String getName() {
  return name;
 public void setName(String name) { = name;
 public Integer getAge() {
  return age;
 public void setAge(Integer age) {
  this.age = age;


Now we will add Service class

package com.programinjava.learn.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.programinjava.learn.model.User;
//import com.programinjava.learn.repository.UserRespositoryImpl;
import com.programinjava.learn.repository.UserRespository;

public class UserService {
 UserRespository userRespository;
 public void saveUser(User user) {;


After this we will see the Controller file

package com.programinjava.learn.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.programinjava.learn.model.User;
import com.programinjava.learn.service.UserService;

public class UserController {
 UserService userService;
 public String saveUser(@ModelAttribute User user ,ModelMap map) {
  map.addAttribute("message", "Successfully Added User");
  return "index";
 public String getIndex(ModelMap map) {
  map.addAttribute("user",new User());
  return "index";


Till now we have added all the files but to create user and store it in redis server , we have created one index.html
in this we are using thymeleaf template enginer.
let see index.html

<!DOCTYPE html>

<html xmlns:th="">
<meta charset="ISO-8859-1">
<form action="/userprocess" th:object="${user}" method="post">

<input type="text" th:field="*{name}" placeholder="Enter your name"></input>
<input type="number" th:field="*{age}" placeholder="Enter your age"></input>

<input type="submit"/>

<span th:if=${message}>
 <h2 th:text="${message}"></h2>

Thats it
After deploy it and start redis server we will hit the URL

After that when we see the redis console , we will be able to see it like below image

Hope this will help you in implementing redis with spring boot

Read Also
  1. Spring Boot and Hibernate Tutorials - Application using Spring Boot
  2. What is Spring boot ?
  3. How to read value from property file in spring boot ?
  4. First Rest API Application - Top Spring Framework Interview Questions
  5. File Uploading in Spring MVC using thymeleaf and spring boot
Thanks for reading


Post a Comment