This sample shows how a Spring Boot application can be deployed on ECS Fargate with Redis Caching Support to persist session data and improve application performance. The solution also provides the necessary templates to automatically provision the required infrastructure using CloudFormation to your desired AWS account.
For the deployment, you must have the following:
- An AWS account. If you don’t have an AWS account, sign up at https://aws.amazon.com.
- AWS Command Line Interface (AWS CLI).
The application architecture consists of a single ECS Fargate container and Redis instance with the potential to scale out if required across the two provided private subnets.
The Spring Boot application provides a basic interface to test the Redis Session Data and Cachable method call capabilities.
The Spring Boot Starter Data Redis dependency provides us with the necessary spring data redis dependency and lettuce-core driver to integrate with our Redis server.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
The Spring Session Data Redis allows the application to easily integrate the HttpSession with Redis.
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
The sample application provides a "view count" example that updates each time a user of the same session visits the page. The example demonstrates the ability to persist data, even when served by different Fargate containers within the cluster.
The cacheable example shows the usage of Redis for returning a cached response. The first call the method will take around five seconds to execute whilst a thread sleep is invoked. Subsequent calls will be returned from cache and avoid the need to wait 5 seconds for the same result.
Docker Compose is used to run the project locally as it provides the capability to bring up the application, redis instance, and mock Fargate metadata easily.
Simply run make deploy-local
to bring up a local running version of the project. Once running, you will be able to access the sample at localhost:8080
.
Simply run make deploy-aws
to create the base infrastructure, push the docker image to ECR and deploy the Java app into ECS. Once completed, visit the ALB FQDN name to access the Java spring boot running on ECS.
To avoid unexpected charges to your account, make sure you delete your stack after you have finished your testing.
See the contributing file to learn how to contribute to this project.
This library is licensed under the MIT-0 License. See the LICENSE file.