# oli2861/saga_pattern architecture

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

- Interactive diagram: https://gitdiagram.com/oli2861/saga_pattern
- Repository: https://github.com/oli2861/saga_pattern

## Overview

- Accounting Service
- Customer Service
- Kitchen Service
- Order Service

Additionally, a shared library located in "orchestration/lib/event" encapsulates event handling and message-broker interactions (using RabbitMQ) for inter-service communication. The entire system is container-ready (with Dockerfiles per service and a docker-compose.yml for orchestration) and includes database initialization scripts, logging configuration, and test suites. This clearly identifies it as a microservices project implementing the saga (distributed transaction) pattern with orchestration, as described by Chris Richardson.

##### Main Components
b. Event Library: Represent the shared event module (or “broker”) as a distinct library that is utilized by each service for event creation, consumption, and state management related to sagas.
e. External Interfaces: If there is an API layer (notably routes in the services like “CustomerRouting”, “OrderRouting”, etc.), include an API gateway/entry point as applicable.
f. Infrastructure Components: Since Docker and docker-compose are part of the project, illustrate the container orchestration providing separate service containers.

##### Relationships and Interactions
c. Saga Flow: Illustrate the saga flow—how an order initiates a transaction that involves multiple microservices coordinated by the orchestration pattern. A dedicated section can annotate the saga manager and state management (saga definitions and state DAOs in the code).
d. Database Interaction: Each service interacting with its persistence layer (shown as a local database or shared DB component) should be connected.

##### Architectural Patterns and Design Principles
a. The microservices architecture with clear service boundaries is a key design aspect.
b. The saga pattern is central—depicting how local transactions across services are coordinated to achieve eventual consistency, with the Order Service initiating saga events and other services on-board.
c. Orchestration-based saga approach which implies that one central coordinator (or a defined orchestration workflow within a service) manages the saga.
d. Use of dependency injection frameworks (as indicated by the “Koin.kt” files in multiple services) and routing plugins to simplify inter-service communication.
e. Mention the use of Docker for containerization along with gradle for builds.

##### Specify Relevant Technologies, Frameworks, and Libraries
a. Gradle is used for builds, as per the multiple build.gradle.kts files.
b. Ktor (or a similar framework) might be used for building the services’ REST endpoints.
c. Koin for dependency injection (as evident from the “Koin.kt” files).
d. RabbitMQ is central for messaging (with classes such as RabbitMQConnectionManager and other event handling utilities).
e. Logback is used for logging (refer to logback.xml files).

##### Essential Elements
- Notations for patterns and critical files such as “Koin.kt”, “Routing.kt”, and “SagaDefinition.kt” to indicate areas where the orchestration and dependency injection mechanisms are implemented.
- Annotations referring to deployment aspects such as Dockerfiles and docker-compose setup.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Infrastructure Layer
    Orchestration["Docker Compose / Container Orchestration"]:::infrastructure

    %% Shared Components
    SharedEventLib["Shared Event Library"]:::library
    MessageBroker["Message Broker (RabbitMQ)"]:::external

    %% Client Entry Point
    Client["Client API Calls"]:::client

    %% Microservices Layer
    subgraph "Accounting Service"
        AService["Accounting Service"]:::service
        ARouting["Accounting Routing"]:::api
        ADB["Accounting DB((DB))"]:::db
    end

    subgraph "Customer Service"
        CService["Customer Service"]:::service
        CRouting["Customer Routing"]:::api
        CDB["Customer DB((DB))"]:::db
    end

    subgraph "Kitchen Service"
        KService["Kitchen Service"]:::service
        KRouting["Kitchen Routing"]:::api
        KDB["Kitchen DB((DB))"]:::db
    end

    subgraph "Order Service"
        OService["Order Service"]:::service
        ORouting["Order Routing"]:::api
        ODB["Order DB((DB))"]:::db
        OSaga["Saga Orchestrator"]:::saga
    end

    %% Connections - API Calls from Client to Service Routing Nodes
    Client -->|"calls"| ARouting
    Client -->|"calls"| CRouting
    Client -->|"calls"| ORouting
    Client -->|"calls"| KRouting

    %% Routing to Microservice Core
    ARouting -->|"routesTo"| AService
    CRouting -->|"routesTo"| CService
    ORouting -->|"routesTo"| OService
    KRouting -->|"routesTo"| KService

    %% Persistence layer connections
    AService -->|"persists"| ADB
    CService -->|"persists"| CDB
    KService -->|"persists"| KDB
    OService -->|"persists"| ODB

    %% Event Communication from Services to Shared Event Library
    AService -->|"publishes/subscribes"| SharedEventLib
    CService -->|"publishes/subscribes"| SharedEventLib
    KService -->|"publishes/subscribes"| SharedEventLib
    OService -->|"publishes/subscribes"| SharedEventLib

    %% Shared Event Library to Message Broker
    SharedEventLib -->|"integrates via"| MessageBroker

    %% Saga Orchestration: Order Service coordinating other services
    OSaga -->|"calls"| AService
    OSaga -->|"calls"| CService
    OSaga -->|"calls"| KService

    %% Infrastructure deployment connections
    Orchestration -->|"deploys"| AService
    Orchestration -->|"deploys"| CService
    Orchestration -->|"deploys"| KService
    Orchestration -->|"deploys"| OService
    Orchestration -->|"deploys"| SharedEventLib

    %% Click Events
    click AService "https://github.com/oli2861/saga_pattern/tree/main/orchestration/app/accounting_service"
    click ARouting "https://github.com/oli2861/saga_pattern/blob/main/orchestration/app/accounting_service/src/main/kotlin/com/oli/accounting/AccountingRouting.kt"
    click ADB "https://github.com/oli2861/saga_pattern/blob/main/orchestration/app/accounting_service/src/main/kotlin/com/oli/persistence/DatabaseFactory.kt"

    click CService "https://github.com/oli2861/saga_pattern/tree/main/orchestration/app/customer_service"
    click CRouting "https://github.com/oli2861/saga_pattern/blob/main/orchestration/app/customer_service/src/main/kotlin/com/oli/customer/CustomerRouting.kt"
    click CDB "https://github.com/oli2861/saga_pattern/blob/main/orchestration/app/customer_service/src/main/kotlin/com/oli/persistence/DatabaseFactory.kt"

    click KService "https://github.com/oli2861/saga_pattern/tree/main/orchestration/app/kitchen_service"
    click KRouting "https://github.com/oli2861/saga_pattern/blob/main/orchestration/app/kitchen_service/src/main/kotlin/com/oli/plugins/Routing.kt"
    click KDB "https://github.com/oli2861/saga_pattern/blob/main/orchestration/app/kitchen_service/src/main/kotlin/com/oli/persistence/DatabaseFactory.kt"

    click OService "https://github.com/oli2861/saga_pattern/tree/main/orchestration/app/order_service"
    click ORouting "https://github.com/oli2861/saga_pattern/blob/main/orchestration/app/order_service/src/main/kotlin/com/oli/order/OrderRouting.kt"
    click ODB "https://github.com/oli2861/saga_pattern/blob/main/orchestration/app/order_service/src/main/kotlin/com/oli/persistence/DatabaseFactory.kt"
    click OSaga "https://github.com/oli2861/saga_pattern/tree/main/orchestration/app/order_service/src/main/kotlin/com/oli/saga/"

    click SharedEventLib "https://github.com/oli2861/saga_pattern/tree/main/orchestration/lib/event"
    click MessageBroker "https://github.com/oli2861/saga_pattern/tree/main/orchestration/lib/event/src/main/kotlin/com/oli/broker"

    click Orchestration "https://github.com/oli2861/saga_pattern/blob/main/orchestration/docker-compose.yml"

    %% Styles
    classDef service fill:#AED6F1,stroke:#1F618D,stroke-width:2px;
    classDef api fill:#FAD7A0,stroke:#D68910,stroke-width:2px;
    classDef db fill:#A9DFBF,stroke:#27AE60,stroke-width:2px;
    classDef library fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
    classDef external fill:#F5B7B1,stroke:#C0392B,stroke-width:2px;
    classDef infrastructure fill:#D7BDE2,stroke:#7D3C98,stroke-width:2px;
    classDef saga fill:#CCD1D1,stroke:#566573,stroke-width:2px;
    classDef client fill:#E8DAEF,stroke:#8E44AD,stroke-width:2px;
```
