circuit-breaker-cdktf

Circuit-Breaker pattern implementation on AWS using Typescript and CDKTF

APACHE-2.0 License

Stars
1

README

This repo contains a simple reference implementation of a ciruit-breaker in AWS using Lambda, SNS, and DynamoDB.

Technology

The problem

The problem to solve pops up when you are starting over with Serverless applications that need to integrate with external APIs. You are charged for the Lambda functions consumed resources (memory and cpu) multiplied by time the lambda is actually running. Given your function relies on external APIs that need to be fetched during runtime, latency of the 3rd. party service becomes a crucial part of your applications costs. Given a Lambda is running 600ms we would be charged for 600ms. If the API increases the latency or is down and the HTTP requests run into a timeout after 10 seconds we are charged for the entire 10,5s. Especially in a high traffic environment it is very relevant to optimize here.

The solution

Basically a simple circuit-breaker has the following flow:

Designed in AWS it could look like this:

How to use

The application source is under application and the infrastructure related stuff is under infrastructure.

Prerequisite: Run yarn install in the projects root

Run locally - Application

  1. Start local environment with docker compose up -d this will setup local SNS,SQS,Dynamodb using Localstack.
    Additionally there is a webUI for SQS and a webUI for DynamoDb running on localhost for debugging purposes.
  2. cd application
  3. Run yarn test to run the functions locally

To bundle the functions for Lambda run yarn build

Deploy

  1. Head over into infrastructure
  2. Run yarn build:deploy:all - this will bundle the Lambda and deploy all components to AWS

Cleanup

  1. Head over into infrastructure
  2. Run yarn destroy:all - this will delete all components

Infrastructure Unit tests

The infrastructure is based on cdktf. to run the jest tests use yarn test inside the infrastructure folder. Although no AWS service will actually be deployed, you need to have valid AWS AccessCredentials in order to meet Terraforms requirements.

References