# sacurio/microservices-order-system architecture

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

- Interactive diagram: https://gitdiagram.com/sacurio/microservices-order-system
- Repository: https://github.com/sacurio/microservices-order-system

## Overview

##### Project Type and Purpose
- The README clearly states that this is an “Order System” using microservices. It includes services for Users, Products, Orders, and Payments, along with an API Gateway.
- The project purpose is to simulate an e-commerce scenario through a microservices architecture that demonstrates various architectural patterns (e.g., API Gateway, CQRS, Event Sourcing, Circuit Breaker, Saga Pattern) and asynchronous communication (using RabbitMQ).

##### File Structure
- The top-level “src” directory contains separate folders for each microservice:
- ApiGateway: Acts as a single entry point for external requests. It uses Ocelot (as seen with the ocelot.json file) to route calls.
- OrderService: Manages order creation, status management, and publishes events (notably the “OrderCreated” event).
- PaymentService: Processes and validates payments. It subscribes to events like “OrderCreated” via a consumer.
- ProductService: Handles product listings, stock, and availability.
- UserService: Manages user registration, login, profiles, and authentication.
- There is also a “Contracts” directory meant for shared domain models or event definitions (Order, OrderCreated, etc.), which is typical in microservices to ensure consistency in event contracts.
- Several Dockerfiles and configuration files indicate containerization and a deployment-ready structure. Files like docker-compose.yml hint at local multi-container orchestration.

##### README for Additional Insights
- The README outlines the technical requirements, including the use of REST/gRPC and event-driven communication (RabbitMQ).
- It emphasizes employing API Gateway patterns, service discovery, CQRS, event sourcing, circuit breakers (Polly), and the Saga pattern.
- It includes the intended database (PostgreSQL) and authentication using IdentityServer with JWT.

a. Main Components:
- API Gateway: Front door for incoming requests, using Ocelot.
- Microservices: User Service, Product Service, Order Service, Payment Service.
- Shared Contracts: A common library for event definitions.
- Message Broker: RabbitMQ for asynchronous event communication.
- Database: PostgreSQL for persistent storage (likely each microservice might have its own data store).
- External Authentication: IdentityServer (JWT) for handling authentication across services.
- Infrastructure/Monitoring: Container orchestration with Kubernetes, monitoring with Prometheus and Grafana.
b. Their Relationships and Interactions:
- External clients send requests which hit the API Gateway.
- The API Gateway routes synchronous API calls to the appropriate microservice(s).
- Order Service triggers asynchronous events (e.g., OrderCreated) which are published to RabbitMQ.
- PaymentService (and potentially other services such as an InventoryService if extended) subscribe to those events.
- Each microservice interacts with PostgreSQL for data storage.
- Authentication flows route via IdentityServer and are enforced at the API Gateway and individual service levels.
- The services might also rely on patterns like CQRS for separating read and write operations, and the Saga pattern for distributed transactions.
c. Architectural Patterns and Design Principles:
- Microservices architecture, with each service independent and containerized.
- API Gateway pattern for unified access.
- Event-driven architecture using RabbitMQ.
- Resilience patterns like the Circuit Breaker (Polly) for fault tolerance.
- Distributed transaction management via the Saga pattern.
- Use of shared contracts to maintain consistency for event messaging.
d. Relevant Technologies:
- .NET 8 (for microservice development).
- Docker (for containerization) and Kubernetes (for orchestration).
- Ocelot API Gateway.
- IdentityServer with JWT.
- PostgreSQL as the database.
- RabbitMQ for messaging.
- Monitoring tools (Prometheus, Grafana) for observability.
- Additional middleware components like Polly for circuit breaking.

##### Specific Elements
- Clear labels: Each microservice (User, Product, Order, Payment, API Gateway), shared Contracts, RabbitMQ, PostgreSQL, IdentityServer.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Layer
    subgraph "External Layer"
        Client["External Clients"]:::external
        Identity["IdentityServer (JWT)"]:::external
    end

    %% API Gateway Layer
    subgraph "API Gateway Layer"
        APIGW["API Gateway (Ocelot)"]:::service
    end

    %% Microservices Layer
    subgraph "Microservices Layer"
        Order["Order Service"]:::service
        Payment["Payment Service"]:::service
        Product["Product Service"]:::service
        User["User Service"]:::service
        Contracts["Shared Contracts"]:::shared
    end

    %% Messaging and Database Layer
    subgraph "Messaging & Database"
        Rabbit["RabbitMQ"]:::messaging
        DB["PostgreSQL"]:::database
    end

    %% Infrastructure Layer
    subgraph "Infrastructure"
        Infra["Docker/Kubernetes"]:::infra
        Monitor["Prometheus & Grafana"]:::infra
    end

    %% Connections
    Client -->|"request"| APIGW
    APIGW -->|"routes"| Order
    APIGW -->|"routes"| Payment
    APIGW -->|"routes"| Product
    APIGW -->|"routes"| User
    APIGW -->|"auth"| Identity

    Order -->|"publish_OrderCreated"| Rabbit
    Rabbit -->|"subscribe_OrderCreated"| Payment

    Order -->|"uses_DB"| DB
    Payment -->|"uses_DB"| DB
    Product -->|"uses_DB"| DB
    User -->|"uses_DB"| DB

    Order -->|"contracts"| Contracts
    Payment -->|"contracts"| Contracts
    Product -->|"contracts"| Contracts
    User -->|"contracts"| Contracts

    %% Click Events for API Gateway
    click APIGW "https://github.com/sacurio/microservices-order-system/blob/dev/src/ApiGateway/ocelot.json"
    click APIGW "https://github.com/sacurio/microservices-order-system/blob/dev/src/ApiGateway/Program.cs"

    %% Click Events for Order Service
    click Order "https://github.com/sacurio/microservices-order-system/tree/dev/src/OrderService/"
    click Order "https://github.com/sacurio/microservices-order-system/blob/dev/src/OrderService/Endpoints/OrderEndpoints.cs"
    click Order "https://github.com/sacurio/microservices-order-system/blob/dev/src/OrderService/Services/OrderService.cs"

    %% Click Events for Payment Service
    click Payment "https://github.com/sacurio/microservices-order-system/tree/dev/src/PaymentService/"
    click Payment "https://github.com/sacurio/microservices-order-system/blob/dev/src/PaymentService/Consumers/OrderCreatedConsumer.cs"

    %% Click Events for Product Service
    click Product "https://github.com/sacurio/microservices-order-system/tree/dev/src/ProductService/"
    click Product "https://github.com/sacurio/microservices-order-system/blob/dev/src/ProductService/Program.cs"
    click Product "https://github.com/sacurio/microservices-order-system/blob/dev/src/ProductService/ProductService.http"

    %% Click Events for User Service
    click User "https://github.com/sacurio/microservices-order-system/tree/dev/src/UserService/"
    click User "https://github.com/sacurio/microservices-order-system/blob/dev/src/UserService/Program.cs"
    click User "https://github.com/sacurio/microservices-order-system/blob/dev/src/UserService/UserService.http"

    %% Click Events for Shared Contracts
    click Contracts "https://github.com/sacurio/microservices-order-system/blob/dev/src/Contracts/Contracts.csproj"
    click Contracts "https://github.com/sacurio/microservices-order-system/blob/dev/src/Contracts/Order.cs"
    click Contracts "https://github.com/sacurio/microservices-order-system/blob/dev/src/Contracts/OrderCreated.cs"

    %% Styles
    classDef service fill:#a8d8ea,stroke:#333,stroke-width:2px;
    classDef external fill:#caffbf,stroke:#333,stroke-width:2px;
    classDef messaging fill:#ffcb77,stroke:#333,stroke-width:2px;
    classDef database fill:#b5ead7,stroke:#333,stroke-width:2px;
    classDef shared fill:#d3c0cd,stroke:#333,stroke-width:2px;
    classDef infra fill:#fbc4ab,stroke:#333,stroke-width:2px;
```
