In this tutorial, we will take a "Slow API" (that takes 3 seconds to load) and turn it into a "Fast API" (that loads in milliseconds) using Redis Caching.
We will simulate a product database. When a user asks for product details, it simulates a slow database query. We will then implement Redis to store this result in memory so that subsequent requests are instant.
1. Step 1: Run Redis using Docker
Instead of installing Redis manually, we use Docker. Create a docker-compose.yml file.1. File: docker-compose.yml
Run docker: docker-compose up -dxxxxxxxxxxservices: redis: image: redis:alpine container_name: practice-redis ports: - "6379:6379"2. Step 2: Add Dependencies (File: build.gradle)
We need the Spring Data Redis starter.
xxxxxxxxxxdependencies { implementation 'org.springframework.boot:spring-boot-starter-web' // The core dependency for Caching + Redis implementation 'org.springframework.boot:spring-boot-starter-data-redis' compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok'}2. Step 3: Configure Redis and Enable Caching
We need to tell Spring Boot where Redis is running and explicitly turn on the caching feature using @EnableCaching.
1. File: src/main/resources/application.properties
spring.application.name=RedisCacheDemo# Redis Configurationspring.data.redis.host=localhostspring.data.redis.port=63792. File: src/main/java/com/planetlearning/cache/CacheApplication.java
xxxxxxxxxxpackage com.planetlearning.cache;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cache.annotation.EnableCaching; // <--- IMPORTANT: This turns on the magic!public class CacheApplication { public static void main(String[] args) { SpringApplication.run(CacheApplication.class, args); }}3. Step 4: Create the Service (The "Slow" Logic)
We will create a service that simulates a slow database call using Thread.sleep(). We use the @Cacheable annotation to fix it.
1. File: src/main/java/com/planetlearning/cache/service/ProductService.java
xxxxxxxxxxpackage com.planetlearning.cache.service;import org.springframework.cache.annotation.CacheEvict;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Service;import java.io.Serializable;// A simple DTO (Must implement Serializable for Redis!)record Product(String id, String name, double price) implements Serializable {}public class ProductService { // 1. @Cacheable: If data is in Redis, return it. If not, run method and save to Redis. (value = "products", key = "#id") public Product getProductById(String id) { simulateSlowDatabase(); return new Product(id, "Iphone 15 Pro", 999.99); } // 2. @CacheEvict: When we update data, we MUST clear the old cache! (value = "products", key = "#id") public String updateProduct(String id, double newPrice) { return "Product updated! Cache cleared."; } // Simulates a 3-second database delay private void simulateSlowDatabase() { try { long time = 3000L; System.out.println("Going to sleep for " + time + " ms to simulate DB call..."); Thread.sleep(time); } catch (InterruptedException e) { throw new IllegalStateException(e); } }}4. Step 5: The Controller
Expose the endpoints to test the speed.
1. File: src/main/java/com/planetlearning/cache/controller/ProductController.java
xxxxxxxxxxpackage com.planetlearning.cache.controller;import com.planetlearning.cache.service.ProductService;import com.planetlearning.cache.service.Product;import org.springframework.web.bind.annotation.*;("/api/products")public class ProductController { private final ProductService productService; public ProductController(ProductService productService) { this.productService = productService; } ("/{id}") public Product getProduct( String id) { return productService.getProductById(id); } ("/{id}") public String updateProduct( String id, double price) { return productService.updateProduct(id, price); }}5. How to Test This? (The "Wow" Factor)
Call GET http://localhost:8080/api/products/1Call GET http://localhost:8080/api/products/1 again.Call PUT http://localhost:8080/api/products/1?price=1000@CacheEvict, which deletes the old data from Redis so the user doesn't see old prices.In a real job, if a page is loading slowly, your boss will ask you to fix it. This tutorial teaches you exactly how to solve that problem in 10 minutes.