🏗️ Architecture

Saga

Saga Pattern

A pattern for managing distributed transactions in a microservices environment. Each service's local transactions are executed sequentially, and consistency is maintained through compensating transactions in case of failure.

📖 Detailed Explanation

The Saga pattern is a concept first proposed in a 1987 database paper, and it manages transactions that span multiple services in a microservices architecture. Unlike traditional 2PC (Two-Phase Commit), each service manages only its own local transaction, and the overall process is coordinated through a series of events.

There are two coordination approaches. Choreography is a distributed approach in which each service publishes events and other services subscribe and react to them. There is no central coordinator, so coupling is low, but it is hard to grasp the flow. Orchestration has a central Saga Orchestrator control the entire flow. The flow is easy to grasp, but it can become a single point of failure.

Compensating transactions are the key. If a failure occurs at any step of a Saga, the transactions that have already completed must be canceled in reverse order. For example, in an order -> payment -> shipping Saga, if shipping fails, the compensating transactions are executed in the order of payment cancellation -> order cancellation.

Idempotency is important. Because the same request may arrive multiple times due to network failures, each step and compensating transaction must produce the same result even if executed multiple times. A unique transaction ID is used to prevent duplicate processing.

Major implementations include Temporal, Camunda, Axon Framework, Apache Camel, and AWS Step Functions. When used together with Event Sourcing, all state changes can be tracked, which is advantageous for debugging and auditing.

💻 Code Example

# Saga 패턴 - Python Orchestration 방식
from dataclasses import dataclass
from enum import Enum
from typing import List, Callable, Optional
import uuid
import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


class SagaStatus(Enum):
    PENDING = "pending"
    RUNNING = "running"
    COMPLETED = "completed"
    COMPENSATING = "compensating"
    FAILED = "failed"


@dataclass
class SagaStep:
    """Saga의 개별 단계"""
    name: str
    action: Callable  # 실행 함수
    compensation: Callable  # 보상 함수
    status: SagaStatus = SagaStatus.PENDING


class SagaOrchestrator:
    """Saga Orchestrator - 전체 흐름 제어"""

    def __init__(self, saga_id: str = None):
        self.saga_id = saga_id or str(uuid.uuid4())
        self.steps: List[SagaStep] = []
        self.completed_steps: List[SagaStep] = []
        self.status = SagaStatus.PENDING
        self.context = {}  # 단계 간 데이터 공유

    def add_step(self, name: str, action: Callable, compensation: Callable):
        """Saga 단계 추가"""
        self.steps.append(SagaStep(name, action, compensation))
        return self

    def execute(self, initial_context: dict = None) -> dict:
        """Saga 실행"""
        self.context = initial_context or {}
        self.status = SagaStatus.RUNNING
        logger.info(f"🚀 Saga 시작: {self.saga_id}")

        try:
            for step in self.steps:
                logger.info(f"  ▶ 단계 실행: {step.name}")
                step.status = SagaStatus.RUNNING

                # 멱등성을 위한 트랜잭션 ID
                tx_id = f"{self.saga_id}-{step.name}"
                self.context['current_tx_id'] = tx_id

                # 단계 실행
                result = step.action(self.context)
                self.context.update(result or {})

                step.status = SagaStatus.COMPLETED
                self.completed_steps.append(step)
                logger.info(f"  ✅ 단계 완료: {step.name}")

            self.status = SagaStatus.COMPLETED
            logger.info(f"🎉 Saga 완료: {self.saga_id}")
            return {"status": "success", "saga_id": self.saga_id, "context": self.context}

        except Exception as e:
            logger.error(f"  ❌ 단계 실패: {e}")
            self._compensate()
            return {"status": "failed", "saga_id": self.saga_id, "error": str(e)}

    def _compensate(self):
        """보상 트랜잭션 실행 (역순)"""
        self.status = SagaStatus.COMPENSATING
        logger.info(f"🔄 보상 트랜잭션 시작 (완료된 {len(self.completed_steps)}개 단계)")

        for step in reversed(self.completed_steps):
            try:
                logger.info(f"  ↩ 보상 실행: {step.name}")
                step.compensation(self.context)
                logger.info(f"  ✅ 보상 완료: {step.name}")
            except Exception as e:
                # 보상 실패 시 수동 개입 필요
                logger.error(f"  ⚠️ 보상 실패: {step.name} - {e}")
                self._save_for_manual_intervention(step, e)

        self.status = SagaStatus.FAILED
        logger.info(f"🔚 Saga 롤백 완료: {self.saga_id}")

    def _save_for_manual_intervention(self, step: SagaStep, error: Exception):
        """수동 개입이 필요한 케이스 저장"""
        logger.warning(f"📋 수동 처리 필요: Saga={self.saga_id}, Step={step.name}")


# ============================================
# 주문 처리 Saga 예시
# ============================================

class OrderService:
    """주문 서비스"""

    def create_order(self, ctx: dict) -> dict:
        order_id = f"ORD-{uuid.uuid4().hex[:8]}"
        logger.info(f"    주문 생성: {order_id}")
        return {"order_id": order_id}

    def cancel_order(self, ctx: dict):
        logger.info(f"    주문 취소: {ctx['order_id']}")


class PaymentService:
    """결제 서비스"""

    def process_payment(self, ctx: dict) -> dict:
        payment_id = f"PAY-{uuid.uuid4().hex[:8]}"
        logger.info(f"    결제 처리: {payment_id}, 금액: {ctx.get('amount', 0)}원")
        # 실패 시뮬레이션
        # raise Exception("결제 실패: 잔액 부족")
        return {"payment_id": payment_id}

    def refund_payment(self, ctx: dict):
        logger.info(f"    결제 환불: {ctx['payment_id']}")


class InventoryService:
    """재고 서비스"""

    def reserve_inventory(self, ctx: dict) -> dict:
        logger.info(f"    재고 예약: {ctx.get('product_id')}")
        return {"inventory_reserved": True}

    def release_inventory(self, ctx: dict):
        logger.info(f"    재고 해제: {ctx.get('product_id')}")


class ShippingService:
    """배송 서비스"""

    def create_shipment(self, ctx: dict) -> dict:
        shipment_id = f"SHIP-{uuid.uuid4().hex[:8]}"
        logger.info(f"    배송 생성: {shipment_id}")
        return {"shipment_id": shipment_id}

    def cancel_shipment(self, ctx: dict):
        logger.info(f"    배송 취소: {ctx['shipment_id']}")


# Saga 실행
if __name__ == "__main__":
    # 서비스 인스턴스
    order_svc = OrderService()
    payment_svc = PaymentService()
    inventory_svc = InventoryService()
    shipping_svc = ShippingService()

    # Saga 정의
    order_saga = SagaOrchestrator()
    order_saga.add_step("create_order", order_svc.create_order, order_svc.cancel_order)
    order_saga.add_step("reserve_inventory", inventory_svc.reserve_inventory, inventory_svc.release_inventory)
    order_saga.add_step("process_payment", payment_svc.process_payment, payment_svc.refund_payment)
    order_saga.add_step("create_shipment", shipping_svc.create_shipment, shipping_svc.cancel_shipment)

    # Saga 실행
    result = order_saga.execute({
        "customer_id": "CUST-001",
        "product_id": "PROD-001",
        "amount": 50000
    })

    print(f"\n결과: {result}")
// Saga 패턴 - Node.js Choreography 방식 (이벤트 기반)
const EventEmitter = require('events');

// 이벤트 버스 (실제로는 Kafka, RabbitMQ 등 사용)
const eventBus = new EventEmitter();

// ============================================
// 각 서비스 정의 (이벤트 기반 반응)
// ============================================

class OrderService {
    constructor() {
        // 이벤트 구독
        eventBus.on('ORDER_REQUESTED', this.createOrder.bind(this));
        eventBus.on('PAYMENT_FAILED', this.cancelOrder.bind(this));
        eventBus.on('INVENTORY_FAILED', this.cancelOrder.bind(this));
    }

    async createOrder(data) {
        console.log('📦 OrderService: 주문 생성 시작');
        try {
            const order = {
                orderId: `ORD-${Date.now()}`,
                customerId: data.customerId,
                items: data.items,
                status: 'CREATED'
            };
            // DB 저장 (생략)
            console.log(`  ✅ 주문 생성 완료: ${order.orderId}`);

            // 다음 단계 이벤트 발행
            eventBus.emit('ORDER_CREATED', { ...data, ...order });
        } catch (error) {
            eventBus.emit('ORDER_FAILED', { ...data, error: error.message });
        }
    }

    async cancelOrder(data) {
        console.log(`  ↩ OrderService: 주문 취소 - ${data.orderId}`);
        // 주문 상태를 CANCELLED로 변경
        eventBus.emit('ORDER_CANCELLED', data);
    }
}

class InventoryService {
    constructor() {
        eventBus.on('ORDER_CREATED', this.reserveInventory.bind(this));
        eventBus.on('PAYMENT_FAILED', this.releaseInventory.bind(this));
    }

    async reserveInventory(data) {
        console.log('📦 InventoryService: 재고 예약 시작');
        try {
            // 재고 확인 및 예약 로직
            const available = true; // 실제로는 DB 확인

            if (!available) {
                throw new Error('재고 부족');
            }

            console.log(`  ✅ 재고 예약 완료`);
            eventBus.emit('INVENTORY_RESERVED', { ...data, inventoryReserved: true });
        } catch (error) {
            console.log(`  ❌ 재고 예약 실패: ${error.message}`);
            eventBus.emit('INVENTORY_FAILED', { ...data, error: error.message });
        }
    }

    async releaseInventory(data) {
        console.log(`  ↩ InventoryService: 재고 해제 - ${data.orderId}`);
        // 재고 복구 로직
    }
}

class PaymentService {
    constructor() {
        eventBus.on('INVENTORY_RESERVED', this.processPayment.bind(this));
    }

    async processPayment(data) {
        console.log('💳 PaymentService: 결제 처리 시작');
        try {
            // 결제 처리 로직
            const paymentResult = {
                paymentId: `PAY-${Date.now()}`,
                amount: data.amount,
                status: 'COMPLETED'
            };

            // 실패 시뮬레이션 (주석 해제하면 롤백 테스트)
            // throw new Error('잔액 부족');

            console.log(`  ✅ 결제 완료: ${paymentResult.paymentId}`);
            eventBus.emit('PAYMENT_COMPLETED', { ...data, ...paymentResult });
        } catch (error) {
            console.log(`  ❌ 결제 실패: ${error.message}`);
            eventBus.emit('PAYMENT_FAILED', { ...data, error: error.message });
        }
    }

    async refundPayment(data) {
        console.log(`  ↩ PaymentService: 환불 처리 - ${data.paymentId}`);
    }
}

class ShippingService {
    constructor() {
        eventBus.on('PAYMENT_COMPLETED', this.createShipment.bind(this));
    }

    async createShipment(data) {
        console.log('🚚 ShippingService: 배송 생성 시작');
        try {
            const shipment = {
                shipmentId: `SHIP-${Date.now()}`,
                orderId: data.orderId,
                status: 'PREPARING'
            };

            console.log(`  ✅ 배송 생성 완료: ${shipment.shipmentId}`);
            eventBus.emit('SHIPMENT_CREATED', { ...data, ...shipment });

            // 최종 완료
            eventBus.emit('SAGA_COMPLETED', data);
        } catch (error) {
            eventBus.emit('SHIPMENT_FAILED', { ...data, error: error.message });
        }
    }
}

// Saga 완료/실패 핸들러
eventBus.on('SAGA_COMPLETED', (data) => {
    console.log('\n🎉 Saga 완료!', data);
});

eventBus.on('ORDER_CANCELLED', (data) => {
    console.log('\n🔚 Saga 롤백 완료', data);
});

// ============================================
// 서비스 초기화 및 Saga 시작
// ============================================
const orderService = new OrderService();
const inventoryService = new InventoryService();
const paymentService = new PaymentService();
const shippingService = new ShippingService();

// Saga 시작 - ORDER_REQUESTED 이벤트 발행
console.log('🚀 주문 Saga 시작\n');
eventBus.emit('ORDER_REQUESTED', {
    customerId: 'CUST-001',
    items: [{ productId: 'PROD-001', quantity: 2 }],
    amount: 50000
});
# Saga 패턴 - Temporal Workflow (권장 프로덕션 방식)
from datetime import timedelta
from temporalio import workflow, activity
from temporalio.common import RetryPolicy
from dataclasses import dataclass


@dataclass
class OrderData:
    customer_id: str
    product_id: str
    amount: int
    order_id: str = None
    payment_id: str = None
    shipment_id: str = None


# ============================================
# Activity 정의 (실제 비즈니스 로직)
# ============================================

@activity.defn
async def create_order(data: OrderData) -> str:
    """주문 생성"""
    order_id = f"ORD-{activity.info().workflow_id[:8]}"
    # DB에 주문 저장
    return order_id


@activity.defn
async def cancel_order(order_id: str) -> None:
    """주문 취소 (보상)"""
    # DB에서 주문 상태 변경


@activity.defn
async def reserve_inventory(product_id: str) -> bool:
    """재고 예약"""
    # 재고 시스템 호출
    return True


@activity.defn
async def release_inventory(product_id: str) -> None:
    """재고 해제 (보상)"""
    # 재고 복구


@activity.defn
async def process_payment(data: OrderData) -> str:
    """결제 처리"""
    # 결제 게이트웨이 호출
    payment_id = f"PAY-{activity.info().workflow_id[:8]}"
    return payment_id


@activity.defn
async def refund_payment(payment_id: str) -> None:
    """환불 처리 (보상)"""
    # 결제 취소 API 호출


@activity.defn
async def create_shipment(order_id: str) -> str:
    """배송 생성"""
    shipment_id = f"SHIP-{order_id[-8:]}"
    return shipment_id


@activity.defn
async def cancel_shipment(shipment_id: str) -> None:
    """배송 취소 (보상)"""
    # 배송 취소 API 호출


# ============================================
# Saga Workflow 정의
# ============================================

@workflow.defn
class OrderSagaWorkflow:
    """주문 처리 Saga Workflow"""

    @workflow.run
    async def run(self, data: OrderData) -> dict:
        # 재시도 정책
        retry_policy = RetryPolicy(
            initial_interval=timedelta(seconds=1),
            maximum_interval=timedelta(seconds=10),
            maximum_attempts=3
        )

        compensations = []  # 보상 작업 스택

        try:
            # 1. 주문 생성
            data.order_id = await workflow.execute_activity(
                create_order,
                data,
                start_to_close_timeout=timedelta(seconds=30),
                retry_policy=retry_policy
            )
            compensations.append((cancel_order, data.order_id))
            workflow.logger.info(f"주문 생성 완료: {data.order_id}")

            # 2. 재고 예약
            await workflow.execute_activity(
                reserve_inventory,
                data.product_id,
                start_to_close_timeout=timedelta(seconds=30),
                retry_policy=retry_policy
            )
            compensations.append((release_inventory, data.product_id))
            workflow.logger.info("재고 예약 완료")

            # 3. 결제 처리
            data.payment_id = await workflow.execute_activity(
                process_payment,
                data,
                start_to_close_timeout=timedelta(seconds=60),
                retry_policy=retry_policy
            )
            compensations.append((refund_payment, data.payment_id))
            workflow.logger.info(f"결제 완료: {data.payment_id}")

            # 4. 배송 생성
            data.shipment_id = await workflow.execute_activity(
                create_shipment,
                data.order_id,
                start_to_close_timeout=timedelta(seconds=30),
                retry_policy=retry_policy
            )
            workflow.logger.info(f"배송 생성 완료: {data.shipment_id}")

            return {
                "status": "completed",
                "order_id": data.order_id,
                "payment_id": data.payment_id,
                "shipment_id": data.shipment_id
            }

        except Exception as e:
            workflow.logger.error(f"Saga 실패: {e}, 보상 트랜잭션 시작")

            # 보상 트랜잭션 실행 (역순)
            for compensation_fn, arg in reversed(compensations):
                try:
                    await workflow.execute_activity(
                        compensation_fn,
                        arg,
                        start_to_close_timeout=timedelta(seconds=30),
                        retry_policy=retry_policy
                    )
                    workflow.logger.info(f"보상 완료: {compensation_fn.__name__}")
                except Exception as comp_error:
                    workflow.logger.error(f"보상 실패: {compensation_fn.__name__} - {comp_error}")
                    # 보상 실패 시 알림 또는 수동 처리 큐에 저장

            return {
                "status": "failed",
                "error": str(e),
                "compensated": True
            }


# Workflow 실행 예시 (Client 코드)
"""
from temporalio.client import Client

async def main():
    client = await Client.connect("localhost:7233")

    result = await client.execute_workflow(
        OrderSagaWorkflow.run,
        OrderData(
            customer_id="CUST-001",
            product_id="PROD-001",
            amount=50000
        ),
        id="order-saga-001",
        task_queue="order-saga-queue"
    )

    print(f"Saga 결과: {result}")
"""

🗣️ Say It Like This in Practice

💬 In a System Design Meeting
"Order, payment, and shipping are each separate services, and 2PC has performance issues. Let's use the Saga pattern so that each service handles only its local transaction, and implement a compensating transaction that cancels the order when payment fails. Let's go with Eventually Consistent for consistency."
💬 In an Architecture Selection Discussion
"Since more than 5 services are connected, I think Orchestration would be better than Choreography. The flow is easy to trace, and the compensation logic can be managed in one place. If we use Temporal, we can implement it as a durable Workflow."
💬 In a Failure Response Discussion
"The payment went through, but the Saga stopped midway because of a failure in the shipping service. The compensating transactions are implemented idempotently, so it is safe to retry. You can check the current status on the Temporal dashboard and either manually resume or force compensation to run."

⚠️ Cautions & Best Practices

❌
Do Not Omit Compensation Logic

Every step must have a compensating transaction. Operations that cannot be compensated (such as sending emails) should be handled outside the Saga or placed in the last step.

❌
Do Not Leave Idempotency Unimplemented

The same request may arrive multiple times due to network failures. Each step and compensating transaction must prevent duplicate execution based on a unique ID.

❌
Watch the Isolation Level

While a Saga is in progress, other transactions may modify the same data (Dirty Read). Resolve concurrency problems with semantic locks or optimistic locks.

✅
Saga Best Practices

Use a dedicated framework such as Temporal/Camunda, combine with Event Sourcing, log all state changes, a monitoring dashboard for failed Sagas, and provide a manual intervention UI.

🔗 Related Terms

📚 Learn More