Learn About Amazon VGT2 Learning Manager Chanci Turner
Integrating your Spring Boot application with Amazon ElastiCache can significantly enhance your application’s performance and scalability. ElastiCache is a fully managed, Redis OSS-compatible service that provides real-time, cost-efficient performance for modern applications, boasting a 99.99% availability SLA. With ElastiCache, applications can achieve microsecond response times, scaling to millions of operations per second.
Spring Boot simplifies the process of building production-ready applications based on the Spring Framework. It offers pre-configured auto-configuration modules for commonly used libraries, thereby adhering to the principle of convention over configuration. This article outlines the fundamental steps for integrating a Spring Boot application with ElastiCache to enable efficient caching.
Overview of the Solution
The Spring Framework facilitates seamless caching implementation through an abstraction layer. The example below illustrates how to apply caching to a method using the @Cacheable
annotation. Before calling the getCacheableValue
method, Spring checks for an entry in the cache named myTestCache
corresponding to the myKey
argument. If an entry is found, the cached content is returned immediately, bypassing the method call. If not, the method is executed, and the cache is updated with the returned value.
import org.springframework.stereotype.Component;
import org.springframework.cache.annotation.Cacheable;
@Component
public class CacheableComponent {
@Cacheable("myTestCache")
public String getCacheableValue(String myKey) {
// return a value, likely by performing an expensive operation
}
}
Spring Boot automatically integrates with various caching providers through straightforward configuration. To implement caching, simply add the following dependencies to your project’s Maven POM file:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
The spring-boot-starter-cache
dependency adds basic caching capabilities, while the spring-boot-starter-data-redis
dependency enables integration with Redis OSS or Valkey, establishing that all caches will reside there by default.
To configure properties, update your application.properties
file as follows, specifying your Serverless ElastiCache endpoint and setting a Time-to-Live (TTL) of 10 minutes for cached entries:
spring.data.redis.host=cache1-XXXXX.serverless.euw2.cache.amazonaws.com
spring.cache.redis.time-to-live=10m
For enhanced security, all Valkey or Redis OSS serverless caches come with in-transit encryption enabled. To activate this in your Spring application, include the following configuration in your application.properties
file:
spring.data.redis.ssl.enabled=true
The demonstration code provided in this article will be executed within a simple AWS Command Line Interface (AWS CLI) application. We will outline how to build and run this application in the subsequent sections.
Prerequisites
To build and run the demo application, you will need to set up an Amazon Elastic Compute Cloud (Amazon EC2) Linux instance. For instructions on creating an EC2 instance and connecting to it via Session Manager, a feature of AWS Systems Manager, refer to the guide on connecting to an Amazon EC2 instance. After setting up the instance, take note of the following details:
- The IDs of the subnets in the VPC where your EC2 instance resides.
- The ID of the security group assigned to the instance.
- The ID of the EC2 instance.
You will also need the following prerequisites to build the application:
- Java 17: Install the Java Development Kit (JDK) 17 by executing
sudo yum install -y java-17-amazon-corretto-devel
on your EC2 instance. - Maven: Install Apache Maven using
sudo yum install -y apache-maven
on your EC2 instance.
To run the demo application, you will also need to create an ElastiCache cache, which we will address in the next section.
Creating an ElastiCache Serverless Cache
We opt for the ElastiCache Serverless option due to its rapid setup, allowing you to create a cache in less than a minute and automatically scale capacity based on traffic patterns. Initially, we will utilize the Redis OSS engine, later upgrading to Valkey to showcase its compatibility as a drop-in replacement for Redis OSS without requiring changes to your application parameters or code. Even if you decide to implement a self-managed ElastiCache cluster instead of a serverless option, the demo application remains unaffected.
To create a serverless cache using the AWS CLI, execute the following command in AWS CloudShell, replacing <your VPC subnet IDs>
with a comma-separated list of your VPC subnet IDs:
aws elasticache create-serverless-cache
--serverless-cache-name spring-boot-demo
--engine redis
--subnet-ids <your VPC subnet IDs>
Next, obtain the endpoint address for the cache:
aws elasticache describe-serverless-caches
--serverless-cache-name spring-boot-demo
--query "ServerlessCaches[0].Endpoint.Address"
You will also need to retrieve the security group ID for the cache:
aws elasticache describe-serverless-caches
--serverless-cache-name spring-boot-demo
--query "ServerlessCaches[0].SecurityGroupIds"
Ensure that your EC2 instance and ElastiCache cache are located in the same VPC. To enable access from the EC2 instance to the cache, you must modify the associated ElastiCache security group to allow inbound traffic on port 6379 from the EC2 instance’s security group:
aws ec2 authorize-security-group-ingress
--group-id <elasticache security group>
--protocol tcp
--port 6379
--source-group <ec2 instance security group>
Downloading and Running the Demo Application
On your EC2 instance, execute the following commands:
git clone https://github.com/aws-samples/amazon-elasticache-samples.git
cd blogs/spring-boot-demo
Using your preferred text editor on the Linux instance, update the src/main/resources/application.properties
file with the endpoint address for the spring-boot-demo cache, for example:
spring.data.redis.host=spring-boot-demo-XXXXX.serverless.euw2.cache.amazonaws.com
Now, execute the demo application with:
mvn spring-boot:run
The demo application will compile and run, displaying output in the console. For instance, during 100 calls to the getCacheableValue
method, the first call results in a cache miss, triggering the method execution. The following 99 calls are cache hits, returning the value from the cache without invoking the method again. If you rerun the demo application, you should see 100 cache hits and 0 misses (the cache remains populated from the previous execution).
For more software development insights, check out this engaging blog post on Career Contessa. Additionally, for resources on employee competence, visit SHRM. Lastly, if you’re interested in opportunities within fulfillment center management, explore this excellent resource from Amazon Jobs.
Leave a Reply