# manthanguptaa/load-balancer architecture

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

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

## Overview

##### Main Components
- Load Balancer Component (“lb” directory): This holds the main load balancer logic that distributes traffic, implements round-robin load balancing, and manages active-passive server health.
- Backend Server Component (“be” directory): These are the backend servers that handle requests. Each provides a `/healthcheck` endpoint for the load balancer to assess their status.
- Client/Request Source: Represent the incoming requests to the load balancer.
- Health Check Mechanism: The periodic health check functionality used by the load balancer to determine the status of each backend server.
- Configuration Element: (e.g., a list of backend servers configured within the lb/main.go file which could be visualized as part of the load balancer’s setup).

##### Relationships and Interactions
- Incoming client requests are first directed to the load balancer.
- The load balancer then uses a round-robin algorithm to forward requests to one of the configured backend servers.
- Meanwhile, the load balancer performs periodic health checks (e.g., via the `/healthcheck` endpoint) on each backend server.
- If a backend is marked as unhealthy (active-passive management), it is not included in the round-robin rotation until it recovers.

##### Architectural Patterns and Design Principles
- Separation of Concerns: The load balancing logic (lb) is separated from the backend server logic (be).
- Active-Passive Management: Only the servers in a healthy (active) state receive requests.
- Periodic Health Checks: A monitoring loop is implemented to periodically check the status of the servers.
- Round-Robin Scheduling: Simple load distribution algorithm is utilized.

##### Relevant Technologies and Libraries
- Golang (language/platform)
- HTTP server capabilities (for both load balancer and backend servers)
- Built-in libraries for HTTP handling, scheduling (for periodic checks), and possibly logging.

- Optionally, create a "configuration" note near the Load Balancer showing where the list of backend servers is defined.

Happy Diagramming!

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client Layer"
        C["Client / Request Source"]:::networking
    end

    %% Load Balancer Layer
    subgraph "Load Balancer Layer"
        LB["Load Balancer"]:::control
        LB_RR["Round-Robin Scheduler"]:::control
        LB_HC["Health Check Loop"]:::control
        LB_AP["Active-Passive Manager"]:::control
        CONFIG["Configuration (Backend List)"]:::control
    end

    %% Backend Server Pool
    subgraph "Backend Server Pool"
        BE1["Backend Server 1"]:::networking
        BE2["Backend Server 2"]:::networking
        BE3["Backend Server 3"]:::networking
    end

    %% Connections
    C -->|"request"| LB

    LB -->|"load_config"| CONFIG
    CONFIG --> LB

    LB -->|"init_scheduling"| LB_RR
    LB_RR -->|"forwards"| BE1
    LB_RR -->|"forwards"| BE2
    LB_RR -->|"forwards"| BE3

    LB -->|"start_healthcheck"| LB_HC
    LB_HC -->|"healthcheck"| BE1
    LB_HC -->|"healthcheck"| BE2
    LB_HC -->|"healthcheck"| BE3
    BE1 -->|"status_report"| LB_HC
    BE2 -->|"status_report"| LB_HC
    BE3 -->|"status_report"| LB_HC

    LB -->|"manage_servers"| LB_AP
    LB_AP -->|"excludes"| BE2
    LB_AP -->|"excludes"| BE3

    %% Click Events
    click LB "https://github.com/manthanguptaa/load-balancer/blob/main/lb/main.go"
    click LB_RR "https://github.com/manthanguptaa/load-balancer/blob/main/lb/main.go"
    click LB_HC "https://github.com/manthanguptaa/load-balancer/blob/main/lb/main.go"
    click LB_AP "https://github.com/manthanguptaa/load-balancer/blob/main/lb/main.go"
    click CONFIG "https://github.com/manthanguptaa/load-balancer/blob/main/lb/main.go"
    click BE1 "https://github.com/manthanguptaa/load-balancer/blob/main/be/main.go"

    %% Styles
    classDef networking fill:#cfe2f3,stroke:#0b5394,stroke-width:2px;
    classDef control fill:#f9cb9c,stroke:#bf9000,stroke-width:2px;
```
