Modern microservices are small, autonomous, and independently deployable — but this also creates a huge problem.

Typical pharmacy prescription management - payments, banking, logistics, supply-chain, or booking systems perform operations across multiple microservices

Each runs in its own database, service, and network boundary.

If one operation fails halfway, the entire business flow should not leave the system in an inconsistent state.

This is where the Saga Pattern becomes essential.

What is a Saga? Why Not Use ACID?

A Sagais a sequence of local transactions.
Each local transaction updates data within one service and publishes an event to trigger the next step.

If a failure occurs:

Why does ACID (2PC) not work in microservices?

Problem

Explanation

2PC is blocking

If the coordinator crashes, everything hangs.

Microservices have independent DBs

No shared commit log across distributed DBs.

Scales poorly

Locks span distributed systems.

Cloud services do not support XA transactions

DynamoDB, S3, Mongo, Kafka, RDS, etc.

A Saga avoids these issues with event-driven or orchestrated compensation logic.

Saga Architecture Diagram:

1. Use Case: Pharmacy Prescription Order Processing

Scenario

Imagine a Pharmacy Prescription Online platform where a customer places an order for a product.

The system involves three independent services:

  1. Order Service – Creates the order record.
  2. Inventory Service – Reserves the product in stock.
  3. Payment Service – Charges the customer.

Problem Without Saga

If one of these steps fails (e.g., payment fails), the previous steps might leave the system in an inconsistent state:

This is unacceptable in enterprise systems, especially when multiple microservices interact.

Solution With Saga

Using a Saga Orchestration Pattern, each service executes its local transaction, and in case of failure, compensation actions roll back previous steps

This ensures business-level consistency across services.

2. Why We Need the Saga Pattern

  1. Distributed Microservices: Traditional ACID transactions cannot span multiple microservices.
  2. Eventual Consistency: Ensures consistent outcomes without requiring global resource locking.
  3. Fault Tolerance: Automatically rolls back previous steps if a later step fails.
  4. Cloud-Native: Works seamlessly with scalable services in cloud environments.

Loose Coupling: Each service remains independent, communicating only through APIs.

3. Advantages of Using Saga

4. Step-by-Step Explanation of the Script

Below is a detailed walkthrough of the key script components from your Spring Boot Saga project.

4.1 Order Model (Order.java)


package com.example.orderservice;
public class Order {
    private Long id;
    private String product;
    private String status;
    public Order() {
        // default constructor (required for Jackson)
    }
    public Order(Long id, String product, String status) {
        this.id = id;
        this.product = product;
        this.status = status;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getProduct() {
        return product;
    }
    public void setProduct(String product) {
        this.product = product;
    }
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
}

Enterprise Ready: Demonstrates clear orchestration, compensation, and business-level consistency.

4.6 Verification of Outputs

  1. Success Case

  1. Payment Failure Case

  1. Inventory Failure Case:

**

5. Advantages Highlighted

6. Why Enterprises Need This Pattern