# m-jovanovic/saga-pattern-masstransit architecture

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

- Interactive diagram: https://gitdiagram.com/m-jovanovic/saga-pattern-masstransit
- Repository: https://github.com/m-jovanovic/saga-pattern-masstransit

## Overview

- Inventory Service: Contains consumers (e.g., ReserveInventoryConsumer) and configuration files. It is responsible for managing inventory-related operations.
- Order Service: Contains controllers (e.g., OrdersController) and the OrderSaga, along with a dedicated DbContext for saga state (OrderSagaDbContext). This service coordinates order processing.
- Payment Service: Contains consumers such as ProcessPaymentConsumer to handle payment-related operations.
- Message Contracts: A separate project that defines the shared contracts (Messages.cs) used by the various services to communicate.
- Infrastructure Components:
- RabbitMQ: Acts as the message bus for inter-service communication.
- PostgreSQL: Used for storing persistent data such as database migrations and saga states.

##### Relationships and Interactions
- The Order Service’s OrderSaga is central to the saga orchestration—coordinate the sequence of operations across multiple services. Illustrate how the saga initiates communication with the Inventory Service and the Payment Service, and how it handles compensating actions if needed.

##### Architectural Patterns and Technologies
- Microservices Architecture: Emphasize that the system is partitioned into independent services (Inventory, Order, Payment), each running in its own process or container.
- Saga Pattern (State Machine): Underline the use of a state machine pattern in the Order Service to manage long-running transactions and ensure eventual consistency.
- MassTransit: Note its role as the framework for implementing the state machine and handling messaging.
- Containerization and Orchestration: Point out the presence of Docker setups (Dockerfile, docker-compose.yml) which facilitate container-based deployments.
- Message-Based Communication: Illustrate how the Message.Contracts project defines the common language (messages) between the services.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Docker Deployment"
        subgraph "Microservices"
            subgraph "Inventory Service"
                INV["ReserveInventoryConsumer"]:::service
            end
            subgraph "Order Service"
                OC["OrdersController"]:::service
                OSaga["OrderSaga (MassTransit)"]:::service
                ODB["OrderSagaDbContext"]:::service
            end
            subgraph "Payment Service"
                PC["ProcessPaymentConsumer"]:::service
            end
        end
    end

    MC["Message Contracts"]:::contracts
    
    subgraph "Infrastructure"
        RM["RabbitMQ"]:::mq
        PG["PostgreSQL"]:::database
    end

    %% Saga orchestration flows
    OSaga -->|"ReserveInventoryMessage"| INV
    OSaga -->|"ProcessPaymentMessage"| PC

    %% Messaging interactions via RabbitMQ
    INV -->|"publish/consume"| RM
    OSaga -->|"send/receive"| RM
    PC -->|"publish/consume"| RM

    %% Order Service saga persistence
    OSaga -->|"persistState"| PG

    %% Shared message contracts usage
    INV ---|"contracts"| MC
    OSaga ---|"contracts"| MC
    PC ---|"contracts"| MC

    %% Click Events
    click INV "https://github.com/m-jovanovic/saga-pattern-masstransit/blob/main/Inventory.Service/Consumers/ReserveInventoryConsumer.cs"
    click OC "https://github.com/m-jovanovic/saga-pattern-masstransit/blob/main/Order.Service/Controllers/OrdersController.cs"
    click OSaga "https://github.com/m-jovanovic/saga-pattern-masstransit/blob/main/Order.Service/Sagas/OrderSaga.cs"
    click ODB "https://github.com/m-jovanovic/saga-pattern-masstransit/blob/main/Order.Service/Sagas/OrderSagaDbContext.cs"
    click PC "https://github.com/m-jovanovic/saga-pattern-masstransit/blob/main/Payment.Service/Consumers/ProcessPaymentConsumer.cs"
    click MC "https://github.com/m-jovanovic/saga-pattern-masstransit/blob/main/Message.Contracts/Messages.cs"

    %% Styles
    classDef service fill:#AED6F1,stroke:#1F618D,stroke-width:2px;
    classDef mq fill:#F9E79F,stroke:#B7950B,stroke-width:2px,stroke-dasharray: 5, 5;
    classDef database fill:#A9DFBF,stroke:#27AE60,stroke-width:2px;
    classDef contracts fill:#F5CBA7,stroke:#D35400,stroke-width:2px;
```
