# semotpan/saga-orchestration architecture

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

- Interactive diagram: https://gitdiagram.com/semotpan/saga-orchestration
- Repository: https://github.com/semotpan/saga-orchestration

## Overview

##### Main Components

a. Microservices Layer:
- Reservation Service (Saga Orchestrator)
- Hotel Service
- Payment Service

b. Message Broker Layer:
- Apache Kafka
- Kafka Connect with Debezium

c. Data Layer:
- Individual PostgreSQL databases for each service
- Outbox tables for each service

##### Key Relationships to Highlight

a. Service Communication Flow:
- CDC flow from Outbox tables to Kafka via Debezium
- Saga orchestration flow from Reservation Service

b. Database Relationships:
- Each service connects to its dedicated database
- Outbox pattern implementation with CDC

##### Detailed Component Breakdown

For Reservation Service (Orchestrator):
**[Reservation Service]**
- API Layer (ReservationController)
- Saga Framework
- Saga Manager
- Saga State Machine
- Event Handlers
- Database
- Reservation Tables
- Saga State Tables
- Outbox Tables

For Hotel Service and Payment Service:
**[Service]**
- Domain Logic
- Event Handlers
- Database
- Domain Tables
- Outbox Tables

##### Implementation Details

a. Event Flow Patterns:
- Transactional Outbox pattern implementation
- CDC flows with Debezium
- Saga orchestration paths

b. State Management:
- Saga State Machine transitions
- Compensation paths
- Transaction boundaries

##### Technical Stack Indicators

- Java 21 & Spring Boot
- Apache Kafka
- Debezium
- PostgreSQL
- Docker containers

##### High-Level Architecture
- Services, databases, and Kafka
- Main communication paths

- Detail the reservation process
- Both happy and unhappy paths
- Compensation actions

##### Detailed Component Interaction
- Internal service components
- Outbox pattern implementation
- CDC flow with Debezium

Remember to:
- Version information
- Document all external dependencies

##### The eventual consistency model

This system design should clearly show how the project implements distributed transactions while maintaining data consistency across services through the Saga pattern.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Services
        RS[Reservation Service]
        HS[Hotel Service]
        PS[Payment Service]
    end

    subgraph "Message Broker"
        KF[Apache Kafka]
        DB[Debezium CDC]
    end

    subgraph "Data Layer"
        RDB[(Reservation DB)]
        HDB[(Hotel DB)]
        PDB[(Payment DB)]
    end

    subgraph "Saga Framework"
        SM[Saga Manager]
        SSM[State Machine]
        EH[Event Handlers]
    end

    subgraph "Outbox Pattern"
        ROT[("Reservation<br/>Outbox")]
        HOT[("Hotel<br/>Outbox")]
        POT[("Payment<br/>Outbox")]
    end

    %% Service to Database connections
    RS --> RDB
    HS --> HDB
    PS --> PDB

    %% Outbox connections
    RS --> ROT
    HS --> HOT
    PS --> POT

    %% CDC flows
    ROT --> DB
    HOT --> DB
    POT --> DB
    DB --> KF

    %% Service communication through Kafka
    KF --> RS
    KF --> HS
    KF --> PS
    RS --> KF
    HS --> KF
    PS --> KF

    %% Saga Framework connections
    RS --- SM
    SM --- SSM
    SM --- EH

    %% Styling
    classDef service fill:#3498db,stroke:#2980b9,color:white
    classDef database fill:#95a5a6,stroke:#7f8c8d
    classDef kafka fill:#f1c40f,stroke:#f39c12
    classDef framework fill:#2ecc71,stroke:#27ae60,color:white
    classDef outbox fill:#e74c3c,stroke:#c0392b,color:white

    class RS,HS,PS service
    class RDB,HDB,PDB database
    class KF,DB kafka
    class SM,SSM,EH framework
    class ROT,HOT,POT outbox

    %% Click events
    click RS "https://github.com/semotpan/saga-orchestration/tree/main/reservation-service/src/main/java/io/example/reservation/"
    click HS "https://github.com/semotpan/saga-orchestration/tree/main/hotel-service/src/main/java/io/example/hotel/"
    click PS "https://github.com/semotpan/saga-orchestration/tree/main/payment-service/src/main/java/io/example/payment/"
    click SM "https://github.com/semotpan/saga-orchestration/blob/main/reservation-service/src/main/java/io/example/reservation/saga/SagaManager.java"
    click ROT "https://github.com/semotpan/saga-orchestration/tree/main/outbox/src/main/java/io/example/outbox/"
    click HOT "https://github.com/semotpan/saga-orchestration/tree/main/outbox/src/main/java/io/example/outbox/"
    click POT "https://github.com/semotpan/saga-orchestration/tree/main/outbox/src/main/java/io/example/outbox/"
    click EH "https://github.com/semotpan/saga-orchestration/tree/main/reservation-service/src/main/java/io/example/reservation/framework/"
```
