# sebastian-0912/ticket_backend architecture

> Architecture diagram of the GitHub repository sebastian-0912/ticket_backend, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sebastian-0912/ticket_backend
- Repository: https://github.com/sebastian-0912/ticket_backend

## Overview

##### Project Type & Purpose
- This is a distributed, containerized full-stack back-end system built with Flask and CockroachDB.
- It demonstrates a 3-node CockroachDB cluster, two Flask app instances behind Nginx, plus monitoring and an autoscaler.

##### Main Components
- Clients (API consumers, curl, browser)
- Nginx reverse proxy / load balancer
- Flask application (2 instances: flask_app1, flask_app2)
- CockroachDB cluster (3 nodes: crdb1, crdb2, crdb3)
- Monitoring stack: Prometheus + Grafana
- Autoscaler service

##### Relationships & Data Flows
- Clients → Nginx → Flask instances → CockroachDB cluster
- Prometheus scrapes metrics from Nginx, Flask apps, CockroachDB
- Grafana reads from Prometheus
- Autoscaler polls metrics & adjusts number of Flask containers

##### Architectural Patterns
- Load‐balanced stateless API layer (Flask behind Nginx)
- Distributed SQL data storage (CockroachDB replication)
- Infrastructure as code via Docker Compose
- Observability via Prometheus + Grafana
- Automatic scaling based on metrics

##### Simple ASCII Chart

[Clients]
|
v
+----------------+
|    NGINX LB    |
+----------------+
|
+-----+-----+
|           |
v           v
+-------+   +-------+
|Flask 1|   |Flask 2|
+-------+   +-------+
|           |
+-----+-----+
|
v
+---------------------+
| CockroachDB Cluster |
| +---+ +---+ +---+   |
| |N1 | |N2 | |N3 |   |
| +---+ +---+ +---+   |
+---------------------+
|
v
+------------------+
|  Docker Compose  |
+------------------+

Side services:
- Prometheus ──scrapes──> NGINX, Flask 1/2, CRDB 1/2/3
- Grafana  ──reads──────> Prometheus
- Autoscaler ──polls────> Prometheus & adjusts Flask replicas

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Grouped Docker Compose services
    subgraph "Docker Compose"
        direction TB
        NGINX["Nginx LB<br/>(HTTP:80)"]:::nginx
        F1["Flask App 1"]:::flask
        F2["Flask App 2"]:::flask
        subgraph "CockroachDB Cluster"
            direction TB
            C1["crdb1"]:::crdb
            C2["crdb2"]:::crdb
            C3["crdb3"]:::crdb
        end
    end

    %% Clients
    Clients["Clients<br/>(API consumers, curl, browser)"]:::nginx

    %% Monitoring and Autoscaler
    subgraph Monitoring
        direction TB
        PROM["Prometheus"]:::infra
        GRAF["Grafana"]:::infra
    end

    subgraph "Autoscaler Service"
        direction TB
        AUTO["Autoscaler"]:::infra
    end

    %% Request flow
    Clients -->|HTTP requests| NGINX
    NGINX -->|forwards| F1
    NGINX -->|forwards| F2
    F1 -->|SQL queries| C1
    F1 -->|SQL queries| C2
    F1 -->|SQL queries| C3
    F2 -->|SQL queries| C1
    F2 -->|SQL queries| C2
    F2 -->|SQL queries| C3

    %% Metrics flow
    NGINX ---|scrapes| PROM
    F1 ---|scrapes| PROM
    F2 ---|scrapes| PROM
    C1 ---|scrapes| PROM
    C2 ---|scrapes| PROM
    C3 ---|scrapes| PROM
    GRAF -->|reads| PROM
    AUTO -->|polls metrics| PROM
    AUTO -->|scales| F1
    AUTO -->|scales| F2

    %% Click Events
    click NGINX "https://github.com/sebastian-0912/ticket_backend/blob/main/nginx/nginx.conf"
    click NGINX "https://github.com/sebastian-0912/ticket_backend/tree/main/nginx/Dockerfile"
    click F1 "https://github.com/sebastian-0912/ticket_backend/tree/main/backend/"
    click F2 "https://github.com/sebastian-0912/ticket_backend/tree/main/backend/"
    click C1 "https://github.com/sebastian-0912/ticket_backend/blob/main/docker-compose.yaml"
    click C2 "https://github.com/sebastian-0912/ticket_backend/blob/main/docker-compose.yaml"
    click C3 "https://github.com/sebastian-0912/ticket_backend/blob/main/docker-compose.yaml"
    click AUTO "https://github.com/sebastian-0912/ticket_backend/blob/main/autoscaler/autoscaler.py"
    click AUTO "https://github.com/sebastian-0912/ticket_backend/tree/main/autoscaler/Dockerfile"
    click PROM "https://github.com/sebastian-0912/ticket_backend/blob/main/monitoring/prometheus.yml"
    click GRAF "https://github.com/sebastian-0912/ticket_backend/blob/main/monitoring/grafana/dashboards/nginx-dashboard.json"
    click GRAF "https://github.com/sebastian-0912/ticket_backend/blob/main/monitoring/grafana/dashboards/dashboard.yml"
    click GRAF "https://github.com/sebastian-0912/ticket_backend/blob/main/monitoring/grafana/datasources/datasource.yml"

    %% Styles
    classDef nginx fill:#add8e6,stroke:#000,stroke-width:1px
    classDef flask fill:#90ee90,stroke:#000,stroke-width:1px
    classDef crdb fill:#ffa500,stroke:#000,stroke-width:1px
    classDef infra fill:#d3d3d3,stroke:#000,stroke-width:1px
```
