Integrate Amazon ElastiCache Redis with Spring Boot on Elastic Beanstalk¶
This recipe shows how to add Redis-backed caching to a Spring Boot application running on Elastic Beanstalk. Use this pattern when you need lower-latency reads, session offload, or cache-aside behavior inside the same VPC.
Prerequisites¶
- Running Java Elastic Beanstalk environment in a VPC.
- Existing Amazon ElastiCache for Redis cluster or serverless cache reachable from the environment.
- Security group rules allowing access to Redis on port
6379. - Spring Data Redis dependency.
What You'll Build¶
You will build:
- Redis endpoint configuration through environment properties.
- A Spring
RedisTemplatebacked by ElastiCache. - A cache validation endpoint.
flowchart LR
A[Elastic Beanstalk Instance] --> B[Redis Security Group Rule]
B --> C[ElastiCache Redis Endpoint]
D[Environment Properties] --> A
E[Spring Data Redis] --> C Steps¶
- Set Redis connection properties.
eb setenv SPRING_DATA_REDIS_HOST="mycache.xxxxxx.use1.cache.amazonaws.com" SPRING_DATA_REDIS_PORT=6379
- Add Spring Data Redis.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
- Create a simple cache controller.
package com.example.guide.controller;
import java.util.Map;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RedisController {
private final StringRedisTemplate redisTemplate;
public RedisController(StringRedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
@GetMapping("/cache-check")
public Map<String, String> cacheCheck() {
redisTemplate.opsForValue().set("health", "reachable");
return Map.of("redis", redisTemplate.opsForValue().get("health"));
}
}
- Deploy the updated application.
- Validate VPC placement and security groups if the check fails.
Verification¶
Use these checks after deployment:
Expected outcomes:
- Redis host and port are present in environment properties.
- The Spring Boot app can set and get a Redis value.
/cache-checkreturns a success payload.- Network controls allow only intended application-to-cache traffic.