분산 시스템에서 2PC(2-Phase Commit)는 성능과 가용성 문제로 마이크로서비스에 적합하지 않습니다. Saga 패턴은 각 서비스의 로컬 트랜잭션을 이벤트로 연결하고, 실패 시 이전 단계를 보상(취소)하는 방식으로 최종 일관성을 달성합니다.
주문 → 재고 예약 → 결제 → 배송 예약의 단계에서 어느 단계든 실패하면 이전 단계들이 자동으로 취소됩니다.
from("direct:placeOrder")
.saga()
.timeout(Duration.ofMinutes(5)) // 5분 내 완료되지 않으면 보상
.compensation("direct:cancelOrder") // 실패 시 실행할 보상 라우트
.option("orderId", header("orderId"))
.to("direct:reserveInventory") // 1단계: 재고 예약
.to("direct:processPayment") // 2단계: 결제
.to("direct:arrangeShipping") // 3단계: 배송 예약
.end()
.log("주문 완료: ${header.orderId}"); // 주문 취소 보상 라우트
from("direct:cancelOrder")
.log("주문 취소 보상 시작: ${header.orderId}")
.multicast()
.to("direct:releaseInventory") // 재고 예약 해제
.to("direct:refundPayment") // 결제 취소
.to("direct:cancelShipping") // 배송 취소
.end()
.log("주문 취소 완료: ${header.orderId}");
// 각 단계도 보상 라우트 가짐
from("direct:reserveInventory")
.saga()
.propagation(SagaPropagation.MANDATORY)
.compensation("direct:releaseInventory")
.to("bean:inventoryService?method=reserve")
.end(); Saga가 진행 중에 서버가 재시작되면 상태를 잃지 않아야 합니다. Camel Saga는 상태 저장소를 설정할 수 있습니다.
// DB 기반 Saga 저장소
JdbcSagaRepository sagaRepository = new JdbcSagaRepository(dataSource);
context.addService(sagaRepository);
// 또는 InMemory (테스트용)
InMemorySagaService sagaService = new InMemorySagaService();
context.addService(sagaService); from("direct:processPaymentStep")
.saga()
.propagation(SagaPropagation.MANDATORY)
.option("sagaId", exchangeProperty(Exchange.SAGA_LONG_RUNNING_ACTION))
.compensation("direct:refundPayment")
.idempotentConsumer(
exchangeProperty(Exchange.SAGA_LONG_RUNNING_ACTION),
MemoryIdempotentRepository.memoryIdempotentRepository(1000))
.process(exchange -> {
// 멱등 처리: 같은 sagaId로 재시도해도 중복 결제 안 됨
paymentService.charge(exchange.getIn().getBody(Order.class));
})
.end(); 대부분의 현대 마이크로서비스 시스템에서는 Saga가 더 적합합니다. 단, UI에서 즉시 결과를 보여줘야 한다면 낙관적 UI 업데이트와 함께 사용합니다.
2025년 Apache Camel의 최신 트렌드를 분석합니다. AI/LLM 통합 컴포넌트, 서버리스 배포, Camel K 진화, WebAssembly…
Camel in Action을 완독한 후 Apache Camel의 전체 그림을 다시 정리합니다. 핵심 철학, 학습 경로,…
Apache Camel 2.x에서 3.x, 4.x로 마이그레이션하는 단계별 가이드입니다. 주요 API 변경사항, 제거된 컴포넌트, 자동화 도구…
Apache Camel 라우트에서 발생하는 문제를 디버깅하고 해결하는 실전 기법을 설명합니다. 로그 분석, breakpoint 디버깅, Tracer,…
Apache Camel을 프로젝트에 도입하기 전 알아야 할 핵심 사항을 정리합니다. 학습 곡선, 도입 비용, 적합한…
엔터프라이즈 통합 패턴(EIP) 20가지를 Apache Camel 코드와 함께 한 번에 정리합니다. 메시징 채널, 메시지 라우팅,…