# swiggy/ddb-ondemand-protector architecture

> Architecture diagram of the GitHub repository swiggy/ddb-ondemand-protector, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-02-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/swiggy/ddb-ondemand-protector
- Repository: https://github.com/swiggy/ddb-ondemand-protector

## Overview

##### Project Type and Purpose
- This project is an AWS-based pipeline tool designed to serve as a cost guardrail by switching DynamoDB tables from on‑demand to provisioned capacity once a cost threshold is reached.
- Its core functionality is to monitor RCU/WCU usage (and cost) and then automatically trigger a mode switch via an AWS Step Function, which orchestrates Lambda functions and CloudFormation/SAM configuration.
- The design is intended to address cost management for DynamoDB tables by limiting excessive consumption through automated resource mode switching.

##### Analyzing the File Structure
- Top-level directories such as:
- cmd: Contains the main executable (main.go) serving as the entry point for the DdbSwitch command line tool.
- internal: Contains the business logic (ddb_switch.go and its tests) used by the tool.
- pkg: Houses additional reusable packages for handling billing modes, scaling events, and other DynamoDB utilities.
- config: Provides configuration management (e.g., config.go).
- sam: Contains the AWS SAM template (cu-limiter-ddb.yaml) which defines the AWS resources.
- test: Contains mocks for testing external components.
- The presence of a build/templating script (create_template.py) indicates that part of the system is responsible for generating deployment templates dynamically.

##### Insights from the README
- The README explains the motivation, detailing the cost implications of on‑demand capacity and the need for table-level throttling.
- The "Architecture" section describes the high-level pipeline using AWS Step Functions that orchestrate the switch between on‑demand and provisioned modes for DynamoDB.
- The deployment is managed via AWS SAM CLI with CloudFormation-defined resources:
- DdbSwitch (Lambda Function): Responsible for switching the table mode.
- DdbStep (Step Function): Orchestrates the switch workflow.
- Ddbalarm (CloudWatch Alarm): Monitors table capacity thresholds.
- DdbSwitchRule (EventBridge Rule): Triggers the step function once thresholds are met.

a. Main Components:
- Front-end/Command-line Component:
- cmd/ddb-switch: Entry point initiating the process.
- Business Logic & Utility Libraries:
- internal/ddb-switch: Core logic for processing switching requests.
- pkg/ddb-switch: Utility functions for billing mode, scaling events, and DynamoDB interaction.
- config: Application configuration.
- Deployment & Automation:
- create_template.py: Script that generates the SAM template.
- sam template (cu-limiter-ddb.yaml): Defines AWS resources required for deploying the pipeline.
- AWS Cloud Resources (as per SAM template):
- Lambda (DdbSwitch)
- Step Function (DdbStep)
- CloudWatch Alarm (Ddbalarm)
- EventBridge Rule (DdbSwitchRule)
- Associated SNS Topics and IAM Roles (as specified in the README parameters)

b. Relationships and Interactions:
- The command-line tool (cmd/ddb-switch) triggers the execution of the internal logic.
- The internal and pkg directories handle business logic and orchestrate interactions with DynamoDB.
- The generated SAM template ties these together with AWS resources:
- CloudWatch Alarm continuously measures RCU/WCU usage.
- When thresholds are breached, the Alarm triggers an EventBridge Rule.
- This rule invokes the Step Function (DdbStep), which executes the DdbSwitch Lambda Function.
- The Lambda function carries out the mode switch on the DynamoDB table and schedules a reversal after 24 hours.
- External services like SNS topics are integrated to send notifications and alerts.

c. Important Architectural Patterns or Design Principles:
- Separation of Concerns: Distinct directories for command entry point, internal business logic, and common functions.
- Infrastructure as Code: Use of AWS SAM and CloudFormation templates for deployment.
- Event-Driven Architecture: AWS CloudWatch, EventBridge, and Step Functions working together to react to predefined capacity thresholds.
- Testability: Presence of a test directory and mocks for unit/integration testing.

d. Relevant Technologies and Frameworks:
- Go for the core application logic.
- AWS Step Functions, Lambda, CloudWatch Alarms, EventBridge for the orchestration of capacity switching.
- AWS SAM for packaging and deploying this serverless stack.
- Python (create_template.py) for dynamic SAM template generation.

##### Elements
- Each major component (both the internal modules and AWS resources) should have a clear label:
- "cmd/ddb-switch (CLI Entry Point)"
- "internal/ddb-switch (Business Logic)"
- "pkg/ddb-switch (Utilities & Observability)"
- "SAM Template (cu-limiter-ddb.yaml)"
- "AWS Lambda (DdbSwitch)"
- "AWS Step Function (DdbStep)"
- "AWS CloudWatch Alarm (Ddbalarm)"
- "AWS EventBridge Rule (DdbSwitchRule)"
- "SNS Topics & IAM Roles"
- How the CLI triggers the internal logic.
- Data flow from DynamoDB capacity measurements to CloudWatch Alarm.
- Alarm triggering the EventBridge Rule, which then initiates the Step Function.
- Step Function invoking the Lambda function for mode switching.
- Another for cloud resources (Lambda, Step Functions, CloudWatch, etc.).

## Diagram (Mermaid)

```mermaid
graph TD
    subgraph "Local Application"
        A["CLI Entry Point (cmd/ddb-switch)"]:::app
        B["Business Logic (internal/ddb-switch)"]:::app
        C["Utilities & Configuration (pkg/ddb-switch & config)"]:::app
        D["Testing Mocks (test/mocks)"]:::test
    end

    subgraph "Deployment Tools"
        E["Deployment Template Generator (create_template.py)"]:::tool
        F["SAM Template (sam/cu-limiter-ddb.yaml)"]:::tool
    end

    subgraph "AWS Cloud Resources"
        G["AWS CloudWatch Alarm (Ddbalarm)"]:::aws
        H["AWS EventBridge Rule (DdbSwitchRule)"]:::aws
        I["AWS Step Function (DdbStep)"]:::aws
        J["AWS Lambda (DdbSwitch)"]:::aws
        K["SNS Topics & IAM Roles"]:::aws
    end

    %% Local Application Flow
    A -->|"triggers"| B
    B -->|"utilizes"| C

    %% Deployment Tools Flow
    E -->|"generates"| F

    %% AWS Cloud Workflow
    F -->|"deploys"| G
    G -->|"triggers"| H
    H -->|"invokes"| I
    I -->|"executes"| J
    J -->|"notifies"| K

    %% Click Events
    click A "https://github.com/swiggy/ddb-ondemand-protector/blob/main/cmd/ddb-switch/main.go"
    click B "https://github.com/swiggy/ddb-ondemand-protector/blob/main/internal/ddb-switch/ddb_switch.go"
    click C "https://github.com/swiggy/ddb-ondemand-protector/tree/main/pkg/ddb-switch"
    click D "https://github.com/swiggy/ddb-ondemand-protector/blob/main/test/mocks/ddb.go"
    click E "https://github.com/swiggy/ddb-ondemand-protector/blob/main/create_template.py"
    click F "https://github.com/swiggy/ddb-ondemand-protector/blob/main/sam/cu-limiter-ddb.yaml"

    %% Styling
    classDef app fill:#bbf,stroke:#333,stroke-width:2px;
    classDef aws fill:#cfc,stroke:#333,stroke-width:2px;
    classDef tool fill:#fcf,stroke:#333,stroke-width:2px;
    classDef test fill:#ffd,stroke:#333,stroke-width:2px;
```
