Circuit-Breaker pattern implementation on AWS using Typescript and CDKTF
APACHE-2.0 License
This repo contains a simple reference implementation of a ciruit-breaker in AWS using Lambda, SNS, and DynamoDB.
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.
Basically a simple circuit-breaker has the following flow:
Designed in AWS it could look like this:
The application source is under application
and the infrastructure related stuff is under infrastructure
.
Prerequisite: Run yarn install
in the projects root
docker compose up -d
this will setup local SNS,SQS,Dynamodb using Localstack.cd application
yarn test
to run the functions locallyTo bundle the functions for Lambda run yarn build
infrastructure
yarn build:deploy:all
- this will bundle the Lambda and deploy all components to AWSinfrastructure
yarn destroy:all
- this will delete all componentsThe 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.