# moriarty178/backend-electronic-devices architecture

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

- Interactive diagram: https://gitdiagram.com/moriarty178/backend-electronic-devices
- Repository: https://github.com/moriarty178/backend-electronic-devices

## Overview

##### Project Type and Purpose
- This is a backend‐only, full‐stack support system for an e-commerce platform (“Selling Computer Equipment – Backend”).
- It is built with Java Spring Boot, containerized with Docker, fronted by an Nginx (OpenResty) reverse proxy/load balancer, uses Redis for rate-limiting and messaging, and employs PostgreSQL Primary-Replica streaming replication behind Pgpool-II.

##### High-Level Architecture Components
a. Clients (web browsers or mobile apps)
b. Nginx (OpenResty) Reverse Proxy & Load Balancer
- Rate limiting via Lua scripts + Redis (sliding-window logs)
- Auto-scale hook, upstream health checks
c. Spring Boot Back-End Service Instances (stateless)
- Auth, Cart, Order, Product, Chat WebSocket controllers
d. Redis
- Caching / session management
- Rate-limiting store
- Pub/Sub for WebSocket messaging & RedisKeyExpirationListener
e. Pgpool-II
- Connection pooling, failover/follow primary
f. PostgreSQL Cluster
- Primary node (read/write)
- Replica(s) (read-only, failover target)
g. Docker / Docker‐Compose
- Containerizes all above services

##### Relationships & Data Flows
- Clients → (HTTP/WebSocket) → Nginx
- Nginx → load-balances → Spring Boot Instances
- Spring Boot → queries/writes → Pgpool-II
- Pgpool-II → routes → Primary or Replica PostgreSQL
- Spring Boot → reads/writes → Redis (cache, rate limit counters, pub/sub)
- WebSocket connections (via Nginx) ↔ Spring Boot ↔ Redis pub/sub for real-time chat

##### Architectural Patterns & Principles
- Layered Architecture:
- Presentation layer (Nginx)
- Application layer (stateless Spring Boot services)
- Data layer (Pgpool-II + PostgreSQL, Redis)
- High Availability & Scalability:
- Stateless app containers behind a load balancer → easy horizontal scaling
- Streaming replication + Pgpool-II for DB failover & read-scaling
- Caching & Rate Limiting: Redis + Lua for hot data and protecting endpoints
- Real-time Messaging: WebSocket over Nginx, backed by Redis pub/sub
- Containerization: Docker to ensure reproducibility & simplified deployment

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Clients
    Clients["Clients\n(web browsers, mobile apps)"]:::external

    %% Proxy Layer
    subgraph "Proxy/Load Balancer" 
        direction TB
        NGINX["Nginx (OpenResty)\nReverse Proxy & Load Balancer"]:::network
        LuaRate["rate_limit.lua"]:::network
        LuaScale["auto_scale.lua"]:::network
        LuaUpdate["update_upstream.lua"]:::network
    end

    %% Application Layer
    subgraph "Application Layer\n(Docker Containers)" 
        direction TB
        App1["Spring Boot App Instance #1"]:::compute
        App2["Spring Boot App Instance #2"]:::compute
        BackEndApp["BackEndApplication.java"]:::compute
        ConfigDir["Config/"]:::compute
        Controllers["Controller/"]:::compute
        Services["Service/"]:::compute
        Repos["Repository/"]:::compute
        Listener["RedisKeyExpirationListener.java"]:::compute
        JWT["Jwt/"]:::compute
        DTOEntity["Dto & Entity/"]:::compute
    end

    %% Caching & Messaging
    subgraph "Caching & Messaging\n(Redis Cluster)" 
        direction TB
        RedisCyl["Redis"]:::cache
        RedisCtrl["RedisController.java"]:::cache
    end

    %% Data Layer
    subgraph "Data Layer\n(Docker Containers)" 
        direction TB
        Pgpool["Pgpool-II"]:::network
        subgraph "PostgreSQL Cluster"
            direction TB
            Primary["PostgreSQL Primary"]:::storage
            Replica["PostgreSQL Replica"]:::storage
        end
    end

    %% Containerization
    subgraph "Docker & Orchestration"
        direction TB
        DockerfileNode["Dockerfile"]:::infra
        Compose["docker-compose.yml"]:::infra
    end

    %% Connections
    Clients -->|HTTP/WS| NGINX
    NGINX -->|Load-Balance| App1
    NGINX -->|Load-Balance| App2
    Clients -. WS .-> NGINX

    App1 -->|SQL Queries| Pgpool
    App2 -->|SQL Queries| Pgpool

    Pgpool -->|r/w| Primary
    Pgpool -->|read-only| Replica
    Primary -. streaming replication .-> Replica

    App1 -->|cache, sessions, rate-limits| RedisCyl
    App2 -->|cache, sessions, rate-limits| RedisCyl

    App1 -. pub/sub .-> RedisCyl
    App2 -. pub/sub .-> RedisCyl

    %% Click Events
    click NGINX "https://github.com/moriarty178/backend-electronic-devices/blob/main/reverse_proxy_config/nginx.conf"
    click LuaRate "https://github.com/moriarty178/backend-electronic-devices/blob/main/reverse_proxy_config/rate_limit.lua"
    click LuaScale "https://github.com/moriarty178/backend-electronic-devices/blob/main/reverse_proxy_config/auto_scale.lua"
    click LuaUpdate "https://github.com/moriarty178/backend-electronic-devices/blob/main/reverse_proxy_config/update_upstream.lua"

    click BackEndApp "https://github.com/moriarty178/backend-electronic-devices/blob/main/src/main/java/selling_electronic_devices/back_end/BackEndApplication.java"
    click ConfigDir "https://github.com/moriarty178/backend-electronic-devices/tree/main/src/main/java/selling_electronic_devices/back_end/Config/"
    click Controllers "https://github.com/moriarty178/backend-electronic-devices/tree/main/src/main/java/selling_electronic_devices/back_end/Controller/"
    click Services "https://github.com/moriarty178/backend-electronic-devices/tree/main/src/main/java/selling_electronic_devices/back_end/Service/"
    click Repos "https://github.com/moriarty178/backend-electronic-devices/tree/main/src/main/java/selling_electronic_devices/back_end/Repository/"
    click Listener "https://github.com/moriarty178/backend-electronic-devices/blob/main/src/main/java/selling_electronic_devices/back_end/Service/RedisKeyExpirationListener.java"
    click JWT "https://github.com/moriarty178/backend-electronic-devices/tree/main/src/main/java/selling_electronic_devices/back_end/Jwt/"
    click DTOEntity "https://github.com/moriarty178/backend-electronic-devices/tree/main/src/main/java/selling_electronic_devices/back_end/Dto/"
    click RedisCtrl "https://github.com/moriarty178/backend-electronic-devices/blob/main/src/main/java/selling_electronic_devices/back_end/Controller/RedisController.java"
    click RedisCyl "https://github.com/moriarty178/backend-electronic-devices/blob/main/src/main/java/selling_electronic_devices/back_end/Config/RedisConfig.java"

    click DockerfileNode "https://github.com/moriarty178/backend-electronic-devices/tree/main/Dockerfile"
    click Compose "https://github.com/moriarty178/backend-electronic-devices/blob/main/pg_primary_replica/docker-compose.yml"

    click Primary "https://github.com/moriarty178/backend-electronic-devices/tree/main/pg_primary_replica/master/"
    click Replica "https://github.com/moriarty178/backend-electronic-devices/tree/main/pg_primary_replica/slave/"
    click Pgpool "https://github.com/moriarty178/backend-electronic-devices/tree/main/pg_primary_replica/pgpool/"

    %% Styles
    classDef external fill:#D6E9FE,stroke:#3178C6,color:#3178C6;
    classDef network fill:#E8DAEF,stroke:#9B59B6,color:#9B59B6;
    classDef compute fill:#D6F5D6,stroke:#28A745,color:#28A745;
    classDef cache fill:#FEF5E7,stroke:#D35400,color:#D35400;
    classDef storage fill:#E8F8F5,stroke:#1ABC9C,color:#117A65;
    classDef infra fill:#F2F3F4,stroke:#7F8C8D,color:#2C3E50;
```
