# ducanhle1992/ticket-booking-app architecture

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

- Interactive diagram: https://gitdiagram.com/ducanhle1992/ticket-booking-app
- Repository: https://github.com/ducanhle1992/ticket-booking-app

## Overview

##### Project Type and Purpose
- This is a full-stack, microservices-based ticket-booking platform written in Node.js/TypeScript.
- It uses an event-driven architecture over NATS Streaming to decouple services and ensure eventual consistency.

##### Main Components
a. Client (Next.js frontend)
b. Auth Service
c. Tickets Service
d. Orders Service
e. Payments Service
f. Expiration Service
g. Common Package (shared npm module)
h. NATS Streaming Cluster (Event Bus)
i. Databases (MongoDB instance per service)
j. Redis (for Expiration service)
k. External Stripe API
l. Kubernetes Cluster (Ingress, Deployments, Services)
m. CI/CD (GitHub Actions workflows & Skaffold)

##### Relationships and Data Flows
- Client ↔ HTTP ↔ Auth/Tickets/Orders/Payments APIs
- Auth issues JWTs for secure calls to other services.
- Each service ↔ MongoDB (its own collection)
- Orders, Tickets, Payments, Expiration publish/subscribe events via NATS:
- Tickets publishes ticket-created/updated → Orders & Payments subscribe.
- Orders publishes order-created/cancelled → Expiration & Payments subscribe.
- Expiration publishes expiration-complete → Orders subscribes (cancels unpaid).
- Payments publishes payment-created → Orders subscribes (marks paid).
- Expiration service also uses Redis as an in-memory queue for TTL management.
- Payments service calls Stripe’s external API for real payments.

##### Architectural Patterns & Principles
- Microservices: each service is independently deployed (Docker+K8s).
- Event-Driven Architecture: loose coupling via NATS Streaming (pub/sub).
- Optimistic Concurrency Control in Mongo models.
- Single Responsibility per service (Auth, Tickets, Orders, Payments, Expiration).
- Shared code extracted into a “common” npm package for models, middleware, and event definitions.

##### Technologies & Frameworks
- Backend: Node.js, Express, TypeScript
- Frontend: Next.js (React)
- Databases: MongoDB (one per service), Redis (Expiration)
- Messaging: NATS Streaming
- Containerization: Docker
- Orchestration: Kubernetes (Deployments, Services, Ingress)
- CI/CD: GitHub Actions, Skaffold
- External Service: Stripe API

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Kubernetes Cluster"
        direction TB
        Ingress["Ingress Gateway"]:::infra
        AuthS["Auth Service"]:::service
        AuthDB[(MongoDB)]:::db
        TicketsS["Tickets Service"]:::service
        TicketsDB[(MongoDB)]:::db
        OrdersS["Orders Service"]:::service
        OrdersDB[(MongoDB)]:::db
        PaymentsS["Payments Service"]:::service
        PaymentsDB[(MongoDB)]:::db
        ExpirationS["Expiration Service"]:::service
        ExpirationDB[(MongoDB)]:::db
        Redis[(Redis)]:::db
        NATS["NATS Streaming Cluster"]:::messagebus
    end

    Client["Next.js Client"]:::frontend
    Stripe["Stripe API"]:::external
    subgraph "CI/CD Pipeline"
        direction TB
        GH["GitHub Actions"]:::ci
        Docker["Docker Images"]:::ci
        Skaffold["Skaffold"]:::ci
        GH --> Docker --> Skaffold --> Ingress
    end

    Client -->|"HTTP Calls"| Ingress
    Ingress --> AuthS
    Ingress --> TicketsS
    Ingress --> OrdersS
    Ingress --> PaymentsS

    AuthS --> AuthDB
    TicketsS --> TicketsDB
    OrdersS --> OrdersDB
    PaymentsS --> PaymentsDB
    ExpirationS --> ExpirationDB
    ExpirationS --> Redis

    AuthS ---|pub/sub| NATS
    TicketsS ---|pub/sub| NATS
    OrdersS ---|pub/sub| NATS
    PaymentsS ---|pub/sub| NATS
    ExpirationS ---|pub/sub| NATS

    PaymentsS -->|"Stripe API"| Stripe

    classDef service fill:#ADD8E6,stroke:#000,stroke-width:1px
    classDef db fill:#90EE90,stroke:#000,stroke-width:1px,shape:cylinder
    classDef messagebus fill:#FFA500,stroke:#000,stroke-width:1px
    classDef external fill:#F08080,stroke:#000,stroke-width:1px,shape:cloud
    classDef infra fill:#DDA0DD,stroke:#000,stroke-width:2px
    classDef frontend fill:#87CEFA,stroke:#000,stroke-width:1px
    classDef ci fill:#D3D3D3,stroke:#000,stroke-width:1px

    click Client "https://github.com/ducanhle1992/ticket-booking-app/tree/main/client/"
    click AuthS "https://github.com/ducanhle1992/ticket-booking-app/blob/main/auth/src/index.ts"
    click AuthDB "https://github.com/ducanhle1992/ticket-booking-app/blob/main/infra/k8s/auth-mongo-depl.yaml"
    click TicketsS "https://github.com/ducanhle1992/ticket-booking-app/blob/main/tickets/src/index.ts"
    click TicketsDB "https://github.com/ducanhle1992/ticket-booking-app/blob/main/infra/k8s/tickets-mongo-depl.yaml"
    click OrdersS "https://github.com/ducanhle1992/ticket-booking-app/blob/main/orders/src/index.ts"
    click OrdersDB "https://github.com/ducanhle1992/ticket-booking-app/blob/main/infra/k8s/orders-mongo-depl.yaml"
    click PaymentsS "https://github.com/ducanhle1992/ticket-booking-app/blob/main/payments/src/stripe.ts"
    click PaymentsDB "https://github.com/ducanhle1992/ticket-booking-app/blob/main/infra/k8s/payments-mongo-depl.yaml"
    click ExpirationS "https://github.com/ducanhle1992/ticket-booking-app/blob/main/expiration/src/index.ts"
    click Redis "https://github.com/ducanhle1992/ticket-booking-app/blob/main/infra/k8s/expiration-redis-depl.yaml"
    click NATS "https://github.com/ducanhle1992/ticket-booking-app/blob/main/infra/k8s/nats-depl.yaml"
    click GH "https://github.com/ducanhle1992/ticket-booking-app/tree/main/.github/workflows/"
    click Skaffold "https://github.com/ducanhle1992/ticket-booking-app/blob/main/skaffold.yaml"
```
