Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      The Service Library Service

      July 21, 2025

      Node.js vs. Python for Backend: 7 Reasons C-Level Leaders Choose Node.js Talent

      July 21, 2025

      Handling JavaScript Event Listeners With Parameters

      July 21, 2025

      ChatGPT now has an agent mode

      July 21, 2025

      DistroWatch Weekly, Issue 1131

      July 20, 2025

      I ditched my Bluetooth speakers for this slick turntable – and it’s more practical than I thought

      July 19, 2025

      This split keyboard offers deep customization – if you’re willing to go all in

      July 19, 2025

      I spoke with an AI version of myself, thanks to Hume’s free tool – how to try it

      July 19, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      The details of TC39’s last meeting

      July 20, 2025
      Recent

      The details of TC39’s last meeting

      July 20, 2025

      Simple wrapper for Chrome’s built-in local LLM (Gemini Nano)

      July 19, 2025

      Online Examination System using PHP and MySQL

      July 18, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Argon ONE Up Laptop Runs on a Raspberry Pi CM5

      July 21, 2025
      Recent

      Argon ONE Up Laptop Runs on a Raspberry Pi CM5

      July 21, 2025

      KDE Plasma Adds Rounded Bottom Window Corners to Apps

      July 21, 2025

      Firefox 141 Adds AI Tab Grouping, Reduces Memory Use on Linux

      July 21, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Databases»Integrate your Spring Boot application with Amazon ElastiCache

    Integrate your Spring Boot application with Amazon ElastiCache

    April 16, 2025

    Amazon ElastiCache is a fully managed, Valkey-, Memcached-, and Redis OSS-compatible service that delivers real-time, cost-optimized performance for modern applications with 99.99% SLA availability. ElastiCache speeds up application performance, scaling to millions of operations per second with microsecond response time.

    Spring Boot provides a quick and straightforward way of building production-grade Spring Framework based applications. To accomplish this, Spring Boot comes prepackaged with auto configuration modules for most libraries typically used with Spring Framework. Open source Spring Boot adds auto-configuration on top of Spring Framework by following convention over configuration.

    In this post, we explore the basics of integrating a Spring Boot application with ElastiCache to enable caching.

    Solution overview

    Spring Framework supports transparently implementing caching in an application by providing an abstraction layer. The following code demonstrates a simple example of adding caching to a method by including the @Cacheable annotation. Before invoking the getCacheableValue method, Spring Framework looks for an entry in a cache named myTestCache that matches the myKey argument. If an entry is found, the content in the cache is immediately returned to the caller, and the method is not invoked. Otherwise, the method is invoked, and the cache is updated before returning the 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 provides modules to automatically integrate with a set of providers using convention over configuration. In the following example, adding two module dependencies to the project’s Maven POM file will implement caching:

    <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 to the application, whilst the spring-boot-starter-data-redis adds integration with Redis OSS or Valkey and declares that by default, all caches will exist here.

    For configurable values, the Spring Framework application.properties file is updated. In the following example, the endpoint address of a Serverless ElastiCache cache is provided, with all cached entries configured to have a Time-to-Live (TTL) of 10 minutes:

    spring.data.redis.host=cache1-XXXXX.serverless.euw2.cache.amazonaws.com
    spring.cache.redis.time-to-live=10m

    All Valkey or Redis OSS serverless caches have in-transit encryption enabled. To configure Spring Framework to use in-transit encryption, we add a configuration value to the application.properties file:

    spring.data.redis.ssl.enabled=true

    The demo code provided in this post implements this in a simple AWS Command Line Interface (AWS CLI) application. We demonstrate how to build and run this application in the next sections.

    Prerequisites

    You will build and run the demo application on an Amazon Elastic Compute Cloud (Amazon EC2) Linux instance, running Linux from AWS. To create an EC2 instance and connect to it using Session Manager, a capability of AWS Systems Manager, refer to Connect to an Amazon EC2 instance by using Session Manager. After you create the instance, note the following information:

    • The IDs of the subnets for the virtual private cloud (VPC) your EC2 instance lives in
    • The ID of the security group assigned to the instance
    • The ID of the EC2 instance

    To build the application, you must have the following prerequisites:

    • Java 17 – To install the Java Development Kit (JDK) 17, run sudo yum install -y java-17-amazon-corretto-devel on your EC2 instance
    • Maven – To install Apache Maven, run sudo yum install -y apache-maven on your EC2 instance

    To run the demo application, you also need an ElastiCache cache. We will create this in the next section of this post.

    Create ElastiCache Serverless cache

    We use the ElastiCache Serverless option because it allows you to create a cache in under a minute and instantly scale capacity based on application traffic patterns. We begin with the Redis OSS engine, then later upgrade to Valkey to demonstrate that Valkey is a drop-in replacement for Redis OSS with no alterations to the application parameters or code. The demo application will not require any additional changes if you choose to use a self-designed ElastiCache cluster instead of serverless.

    To create a serverless cache using the AWS CLI run the following command in AWS CloudShell, replacing <your VPC subnet IDs> with a comma separated list of the subnet IDs for the VPC containing your EC2 instance created earlier:

    aws elasticache create-serverless-cache 
    --serverless-cache-name spring-boot-demo 
    --engine redis 
    --subnet-ids <your VPC subnet IDs>

    Obtain and note the endpoint address for the cache:

    aws elasticache describe-serverless-caches 
    --serverless-cache-name spring-boot-demo 
    --query "ServerlessCaches[0].Endpoint.Address"

    The cache will have a security group. Obtain and note this security group ID:

    aws elasticache describe-serverless-caches 
    --serverless-cache-name spring-boot-demo 
    --query "ServerlessCaches[0].SecurityGroupIds"

    Your EC2 instance and ElastiCache cache exist in the same VPC. To allow access to the cache from the EC2 instance, you must permit this in the associated ElastiCache security group. To do this, add a rule to the ElastiCache security group permitting access to port 6379 from the EC2 instance security group:

    aws ec2 authorize-security-group-ingress 
        --group-id <elasticache security group> 
        --protocol tcp 
        --port 6379 
        --source-group <ec2 instance security group>

    Download and run the demo application

    On your EC2 instance, run the following commands:

    git clone https://github.com/aws-samples/amazon-elasticache-samples.git 
    cd blogs/spring-boot-demo

    Using your preferred editor on the Linux instance, update the src/main/resources/application.properties file to include 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 run the demo application with the following command:

    mvn spring-boot:run

    The demo application will build and run. You will see output on the console. An example is shown in the following screenshot.

    Spring Boot application runs and outputs cache hits and misses

    The output shows that for 100 attempts to invoke the getCacheableValue method, the first was a cache miss, causing the method to be invoked. The following 99 attempts were cache hits, returning the value from the cache without invoking the method. You can run the demo application again and see that there are now 100 cache hits and 0 misses (the cache is still populated from the previous run).

    Upgrade the cache to Valkey

    Valkey is an open source, in-memory, high performance, key-value datastore designed to be a drop-in replacement for Redis OSS. Existing Elasticache Redis OSS caches can be upgraded, in-place, to use the Valkey engine by following a simple process.

    The cache will have a major version. Obtain and note this value:

    aws elasticache describe-serverless-caches 
    --serverless-cache-name spring-boot-demo 
    --query "ServerlessCaches[0].MajorEngineVersion"

    Start the upgrade to Valkey:

    aws elasticache modify-serverless-cache 
    --serverless-cache-name spring-boot-demo 
    --engine valkey 
    --major-engine-version <major engine version>

    The cache will continue to operate throughout the upgrade. You can run the demo application again with mvn spring-boot:run at any time to see the same results as using the Redis OSS cache.

    We can check the status of the upgrade with the following command:

    aws elasticache describe-serverless-caches --serverless-cache-name spring-boot-demo --query "ServerlessCaches[0].Status"

    Once the status changes from modifying to available, the upgrade is complete.

    Run the demo application one more time with mvn spring-boot:run. You will see the same results as using the Redis OSS cache. Valkey is a drop-in replacement for Redis OSS—there are no changes needed to the application code or libraries to use Valkey.

    Cleaning up

    To avoid incurring future costs, you can delete the chargeable resources created as part of this post.

    Delete the ElastiCache serverless cache:

    aws elasticache delete-serverless-cache --serverless-cache-name spring-boot-demo

    Delete the EC2 instance:

    aws ec2 terminate-instances --instance-ids <your EC2 instance ID>

    Conclusion

    In this post, we showed how to integrate a Spring Boot application with ElastiCache to enable caching. You can choose between Redis OSS and Valkey engines without having to make any application code or module dependency changes.

    Adding caching to your application with ElastiCache can speed up application performance, enabling scaling to millions of operations per second with microsecond response time.

    To get started with ElastiCache, see the Amazon ElastiCache User Guide.


    About the Author

    Chris GillespieChris Gillespie is a UK-based Senior Solutions Architect. He spends most of his work with fast-moving “born in the cloud” customers. Outside of work, he fills his time with family and trying to get fit.

    Source: Read More

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleUsing Ollama to Run LLMs Locally [FREE]
    Next Article Unlocking BI Potential with DataGenie & MongoDB

    Related Posts

    Development

    Lead with Intelligence: Rethink QA Outsourcing for the AI Age

    July 21, 2025
    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-7913 – TOTOLINK T6 MQTT Service Buffer Overflow Vulnerability

    July 21, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    Threat Groups Aren’t Developing New Attack Vectors with GenAI: RSAC Presentation

    Development

    CVE-2025-24347 – CtrlX OS Network Interfaces HTTP Request Manipulation Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-1976 – Brocade Fabric OS Root Privilege Escalation

    Common Vulnerabilities and Exposures (CVEs)

    Best AI Tools to Book More Meetings in 2025

    Web Development

    Highlights

    CVE-2025-1137 – IBM Storage Scale Command Injection Vulnerability

    May 10, 2025

    CVE ID : CVE-2025-1137

    Published : May 10, 2025, 3:15 a.m. | 23 minutes ago

    Description : IBM Storage Scale 5.2.2.0 and 5.2.2.1, under certain configurations, could allow an authenticated user to execute privileged commands due to improper input neutralization.

    Severity: 7.5 | HIGH

    Visit the link for more details, such as CVSS details, affected products, timeline, and more…

    CVE-2025-48300 – Groundhogg Web Shell Upload Vulnerability

    July 16, 2025

    Rilasciato Wine 10.11: Preparazione per NTSync e correzioni per oltre 25 bug

    June 28, 2025

    Apple’s AI Race: Is the Tech Giant Falling Behind?

    June 2, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.