Skip to content

isaacnborges/saga-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

saga-example

This project shows how to implement the SAGA pattern using MassTransit. The application contains essential settings to work with observability using OpenTelemetry.

Dependencies

How to run

After cloning this repository go into the directory and run docker-compose up -d.

Debug

Informations

This solution contains some APIs and some workers (consumers/publishers) that communicate with each other using synchronous communication with HTTP request or asynchronous messaging with RabbitMQ to implementing the SAGA pattern.

Projects

  • Cart.Api: API responsible for management the carts;
  • Order.Api: API responsible for management the orders;
  • Order.Domain: Project responsible for orchestrating the saga;
  • Order.Worker: Project responsible for publish the commands and consume the events
  • OrderVendor.Worker: Project that simulate an integration with a partner;
  • Payment.Api: API responsible for management the payments;
  • Payment.Domain: Project responsible to maintain payment contracts (commands/events);
  • Payment.Worker: Project that simulate authorize and confirm payments;
  • Saga.Core: Cross project to facilitate commons implementations between the projects;

URLs

Diagram

Saga - Workflow

%%{init: {'theme':'forest'}}%%

sequenceDiagram

  participant FE
  participant Order.Api
  participant Cart.Api
  participant Payment.Api
  participant Order.Worker
  participant Payment.Worker
  participant OrderVendor.Worker
  autonumber
  
  rect rgb(191, 223, 255)
    note right of FE: order creation
    FE->>Order.Api: POST /order
    Order.Api->>Cart.Api: PUT - cart/finalize
    Cart.Api->>Order.Api: 200
    Order.Api->>Payment.Api: POST - Payment/pre-authorize
    Payment.Api->>Order.Api: 200
    Order.Api->Order.Api: Create Order
    Order.Api-->>FE: 202 - Accepted
  end

  rect rgb(230, 255, 204)
    note right of Order.Worker: saga execution coordinator
    Order.Worker-->>Payment.Worker: AuthorizePaymentCommand
    Payment.Worker->>Order.Worker: PaymentAuthorizedEvent
    Order.Worker-->>OrderVendor.Worker: IntegrateIndustryCommand
    OrderVendor.Worker->>Order.Worker: IndustryIntegratedEvent
    Order.Worker-->>Payment.Worker: ConfirmPaymentCommand
    Payment.Worker->>Order.Worker: PaymentConfirmedEvent
    Order.Worker-->>Order.Worker: OrderProcessedEvent
    Order.Worker-->>Order.Worker: Update Order
  end
Loading

Saga - Failure to integrate with the industry

%%{init: {'theme':'forest'}}%%

sequenceDiagram
  participant FE
  participant Order.Api
  participant Cart.Api
  participant Payment.Api
  participant Order.Worker
  participant Payment.Worker
  participant OrderVendor.Worker
  autonumber

  rect rgb(191, 223, 255)
    note right of FE: order creation
    FE->>Order.Api: POST /order
    Order.Api->>Cart.Api: PUT - cart/finalize
    Cart.Api->>Order.Api: 200
    Order.Api->>Payment.Api: POST - Payment/pre-authorize
    Payment.Api->>Order.Api: 200
    Order.Api->Order.Api: Create Order
    Order.Api-->>FE: 202 - Accepted
  end  
  
  rect rgb(255, 224, 204)
    note right of Order.Worker: saga execution coordinator
    Order.Worker-->>Payment.Worker: AuthorizePaymentCommand
    Payment.Worker->>Order.Worker: PaymentAuthorizedEvent
    Order.Worker-->>OrderVendor.Worker: IntegrateIndustryCommand
    OrderVendor.Worker->>Order.Worker: IndustryFailedEvent
    note right of Payment.Api: compensating transactions
    Order.Worker->>Cart.Api: PUT - cart/reopen
  end
Loading

About

Project implementing saga pattern

Resources

Stars

Watchers

Forks