# akshaynkotkar/calculator-load-balancer architecture

> Architecture diagram of the GitHub repository akshaynkotkar/calculator-load-balancer, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/akshaynkotkar/calculator-load-balancer
- Repository: https://github.com/akshaynkotkar/calculator-load-balancer

## Overview

##### Project Type & Purpose
- This is a full-stack backend application (with a minimal front-end for demonstration) focused on arithmetic operations performed via REST endpoints.
- The unique aspect is the load balancing mechanism: distributing incoming calculation requests among multiple server instances using various algorithms (round robin, least connections, etc.).
- Important frameworks/technologies include Spring Boot, Spring Cloud LoadBalancer, Maven, and Java (targeting Java 17 or higher).

##### Main Components
- Clients: External users or applications making HTTP requests (e.g., via a browser or curl).
- Load Balancer: The critical component that encapsulates:
– Health Monitor (Periodic checks of server status)
– Request Distributor (Implements load balancing algorithms to route requests)
– Server Selector (Decides which server instance should handle the incoming request)
- Server Instances: The calculator service running on multiple “instances”. Even though the codebase is the same (as seen in the controller and service packages), the load balancer simulates distribution among them.
- Application layers within the server instance:
– Controller: REST endpoints that expose calculator functions.
– Service: Business logic that performs the actual arithmetic operations.
– Configuration: Setup for load balancing specifics (via the config package).

##### Relationships and Interactions
- That clients send HTTP requests (for addition, subtraction, etc.) to the Load Balancer.
- The Load Balancer takes these requests, performs health checks and evaluation of the available server instances, then routes the request to one of them.
- Once a request is processed, the response is sent back through the load balancer to the client.

##### Architectural Patterns & Key Design Principles
- Microkernel with plug-in components: Each part (controller, service, config) works together to support load balancing.
- Separation of Concerns: Different Java packages manage distinct functionality (REST controllers for endpoints, services for computation, and configuration for load balancing algorithm).
- Fault Tolerance: Implementation of the circuit breaker and health checks to ensure high availability.
- Scalability: The ability to register/deregister server instances dynamically contributes to overall system resiliency.

##### Technologies, Frameworks, and Libraries
- Spring Boot for rapid application development.
- Spring Cloud LoadBalancer for client-side load balancing.
- Maven for dependency and build management.
- Circuit breaker pattern (likely using Resilience4j or similar) for fault tolerance.
- Java 17 as the language/runtime.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Clients Layer
    Client1("Client 1"):::client
    Client2("Client 2"):::client
    ClientN("Client N"):::client

    %% Load Balancer Layer
    subgraph "Load Balancer Layer"
        LB("Load Balancer"):::loadbalancer
        HM("Health Monitor"):::internal
        RD("Request Distributor"):::internal
        SS("Server Selector"):::internal
        CB("Circuit Breaker"):::internal
        LB -->|"monitors"| HM
        LB -->|"distributes"| RD
        LB -->|"selects"| SS
        LB -->|"protects"| CB
    end

    %% Flow from Clients to Load Balancer
    Client1 -->|"HTTP Request"| LB
    Client2 -->|"HTTP Request"| LB
    ClientN -->|"HTTP Request"| LB

    %% Server Instances Layer
    subgraph "Server Instance 1"
        AEP1("Application Entry Point"):::server
        CC1("Calculator Controller"):::server
        CSI1("Calculator Service Interface"):::server
        CS1("CalculatorServiceImpl1"):::server
        AEP1 -->|"initializes"| CC1
        CC1 -->|"forwards to"| CSI1
        CSI1 -->|"executes"| CS1
    end

    subgraph "Server Instance 2"
        AEP2("Application Entry Point"):::server
        CC2("Calculator Controller"):::server
        CSI2("Calculator Service Interface"):::server
        CS2("CalculatorServiceImpl2"):::server
        AEP2 -->|"initializes"| CC2
        CC2 -->|"forwards to"| CSI2
        CSI2 -->|"executes"| CS2
    end

    %% Load Balancer routes request to Server Instances
    LB -->|"RoundRobin"| AEP1
    LB -->|"RoundRobin"| AEP2

    %% Responses from Server Instances back to Load Balancer
    CS1 -->|"Response"| LB
    CS2 -->|"Response"| LB

    %% Load Balancer sends responses back to Clients
    LB -->|"HTTP Response"| Client1
    LB -->|"HTTP Response"| Client2
    LB -->|"HTTP Response"| ClientN

    %% Click Events for Component Mapping
    click LB "https://github.com/akshaynkotkar/calculator-load-balancer/blob/master/src/main/java/loadBalancer/calculator/config/LoadBalancer.java"
    click CC1 "https://github.com/akshaynkotkar/calculator-load-balancer/blob/master/src/main/java/loadBalancer/calculator/controller/CalculatorController.java"
    click CC2 "https://github.com/akshaynkotkar/calculator-load-balancer/blob/master/src/main/java/loadBalancer/calculator/controller/CalculatorController.java"
    click CSI1 "https://github.com/akshaynkotkar/calculator-load-balancer/blob/master/src/main/java/loadBalancer/calculator/service/CalculatorService.java"
    click CSI2 "https://github.com/akshaynkotkar/calculator-load-balancer/blob/master/src/main/java/loadBalancer/calculator/service/CalculatorService.java"
    click CS1 "https://github.com/akshaynkotkar/calculator-load-balancer/blob/master/src/main/java/loadBalancer/calculator/service/CalculatorServiceImpl1.java"
    click CS2 "https://github.com/akshaynkotkar/calculator-load-balancer/blob/master/src/main/java/loadBalancer/calculator/service/CalculatorServiceImpl2.java"
    click AEP1 "https://github.com/akshaynkotkar/calculator-load-balancer/blob/master/src/main/java/loadBalancer/calculator/CalculatorApplication.java"
    click AEP2 "https://github.com/akshaynkotkar/calculator-load-balancer/blob/master/src/main/java/loadBalancer/calculator/CalculatorApplication.java"

    %% Styles
    classDef client fill:#a9d08e,stroke:#333,stroke-width:2px;
    classDef loadbalancer fill:#ffbf00,stroke:#333,stroke-width:2px;
    classDef server fill:#add8e6,stroke:#333,stroke-width:2px;
    classDef internal fill:#f2f2f2,stroke:#333,stroke-width:2px;
```
