Redis is a powerful in-memory data store often used for caching, messaging, and real-time data management. This guide explains how to integrate Redis into a Spring Boot application using both Java and Kotlin.
Written by
Şuayb Şimşek
Backend-focused fullstack developer sharing practical notes on Spring Boot, security, microservices, and cloud-native architecture.
Add the @EnableCaching annotation to your main application class to enable Spring's caching abstraction.
🛠️ Step 4: Service Layer Example with Caching
Configuring TTL (Time-To-Live)
Set the cache expiration time in your application.properties file:
PROPERTIESapplication.properties
spring.cache.redis.time-to-live=600000
This sets the TTL to 10 minutes (600,000 milliseconds).
Controller Example
Create a REST controller to expose the caching functionality.
▶️ Running the Application
Run the application using the following command:
BASH
./mvnw spring-boot:run
🧪 Testing the API
You can test the API using cURL or Postman:
Fetch a User (cached):
BASH
curl -X GET http://localhost:8080/users/1
🏁 Conclusion
You now have a practical Spring Boot Redis Caching implementation with a clear, production-friendly Spring Boot structure. As a next step, adapt configuration and tests to your own domain, then validate behavior under realistic traffic and failure scenarios.
JAVARedisApplication.java
package com.example.redis;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching
public class RedisApplication {
public static void main(String[] args) {
SpringApplication.run(RedisApplication.class, args);
}
}
KOTLINRedisApplication.kt
package com.example.redis
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.cache.annotation.EnableCaching
@SpringBootApplication
@EnableCaching
class RedisApplication
fun main(args: Array<String>) {
runApplication<RedisApplication>(*args)
}
JAVAUserService.java
package com.example.redis.service;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Cacheable("users")
public String getUserById(String id) {
simulateSlowService();
return "User with ID: " + id;
}
private void simulateSlowService() {
try {
Thread.sleep(3000L);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
}
}
KOTLINUserService.kt
package com.example.redis.service
import org.springframework.cache.annotation.Cacheable
import org.springframework.stereotype.Service
@Service
class UserService {
@Cacheable("users")
fun getUserById(id: String): String {
simulateSlowService()
return "User with ID: $id"
}
private fun simulateSlowService() {
Thread.sleep(3000L)
}
}
JAVAUserController.java
package com.example.redis.controller;
import com.example.redis.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {
private final UserService userService;
@GetMapping("/{id}")
public String getUser(@PathVariable String id) {
return userService.getUserById(id);
}
}
KOTLINUserController.kt
package com.example.redis.controller
import com.example.redis.service.UserService
import org.springframework.web.bind.annotation.*
@RestController
@RequestMapping("/users")
class UserController(
private val userService: UserService
) {
@GetMapping("/{id}")
fun getUser(@PathVariable id: String): String = userService.getUserById(id)
}