Giter Club home page Giter Club logo

java-spring-boot-ecs-fargate-redis-caching's Introduction

Java Spring Boot on ECS Fargate with Redis Caching

Introduction

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.

Pre-requisite

For the deployment, you must have the following:

Solution Architecture

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.

Architecture

Application Overview

The Spring Boot application provides a basic interface to test the Redis Session Data and Cachable method call capabilities.

App Screenshot

Spring Boot Redis Dependencies

Spring Boot Starter Data Redis

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>

Spring session data Redis

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>

Redis Session Data

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.

Redis Cacheable

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.

Deployment

Local

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.

AWS

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.

Clean up

To avoid unexpected charges to your account, make sure you delete your stack after you have finished your testing.

Security

See the contributing file to learn how to contribute to this project.

License

This library is licensed under the MIT-0 License. See the LICENSE file.

java-spring-boot-ecs-fargate-redis-caching's People

Contributors

shendriksen avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

java-spring-boot-ecs-fargate-redis-caching's Issues

java-spring-boot-service.yaml File Should use IAM Policy for Auto Scaling

Within the file java-spring-boot-ecs-fargate-redis-caching/cfn/applications/java-spring-boot-service.yaml there is a resource named AutoScalingTarget of type AWS::ApplicationAutoScaling::ScalableTarget. At issue here are two things:

  1. A AWS provided IAM Managed Policy with the ARN arn:aws:iam::aws:policy/aws-service-role/AWSApplicationAutoscalingECSServicePolicy already exists.
  2. The particular policy referenced above includes one permission (cloudwatch:DeleteAlarm) that the existing policy used in the document does not include.

I would suggest modifying the AWS::IAM::Role resource as follows:

AutoScalingRole:
  Type: AWS::IAM::Role
  Properties:
    RoleName: !Join ['', [ !Sub "${AWS::StackName}-${ServiceName}", AutoScalingRole]]
    AssumeRolePolicyDocument:
      Statement:
        - Effect: Allow
          Principal:
            Service: application-autoscaling.amazonaws.com
          Action: sts:AssumeRole
    ManagedPolicyArns:
      - arn:aws:iam::aws:policy/aws-service-role/AWSApplicationAutoscalingECSServicePolicy

Redis on Fargate vs ElastiCache

Could you provide some explanation on the differences in running Redis on Fargate as in this example, or using the same Spring Boot application with ElastiCache (AWS-managed Redis)? Is there any documentation on this, or articles discussing the benefits and tradeoffs?

Actually I'm a bit confused whether this example is showing running Redis in Fargate or via ElastiCache. There is a CloudFormation template for ElastiCache, but the fargate_metadata.json seems to refer to Redis as a Fargate image.

Could you give an explanation on which approach this example uses to access Redis, and what factors should be considered when choosing between Redis on Fargate vs ElastiCache?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.