Pro Tips — Taste of Compensating Transactions (Saga Pattern) in Distributed Architectures
Confinement within classic Monolithic native environments renders the solitary @Transactional annotation exceptionally omnipotent ensuring flawlessly structural Rollbacks natively. It dominates strictly because the Payment DB, Ordering DB, and Coupon DB natively reside homogeneously within the exact singular network envelope securely.
1. The Agonizing Fallacy of Microservice Distributed Transactions
When architecting disjointed MSA boundaries, inherently foreign server instances (Payment Server, Loyalty Points Server, Shipping Server) rigidly employ entirely independent siloed database engines.
- Firing native
POST /payAPI against the Payment Server (Success) - Firing native
POST /point/deductAPI targeting the Points Server (Success) - Firing native
POST /deliveryAPI at the Shipping Server, violently crashing heavily (Failure)
When frantically screaming: "Gasp, rollback the earlier Payment and Points transactions strictly!", the horrifying reality dictates that the independent Point and Payment systems have irrevocably executed explicit physical Commits against their DB hard disks.
Pleading isolated server modules to dynamically respond to a magical native @Transactional Rollback command is absolutely impossible.
2. The Practical Strategy: Compensating Transactions via the Saga Pattern
Forcefully bypassing brutally rigid and lethargic locking protocols like Global 2PC (Two-Phase Commits), scalable enterprise applications extensively utilize resilient Event-Driven Saga Pattern methodologies.
Categorically abandoning the impossibility of a literal "Rollback switch", this model necessitates structurally triggering proactive, inverse cancellation commands sequentially (Compensating Transactions) to effectively reverse previous functional states manually.
- Shipping Server catastrophically fails (Asynchronously emits
DeliveryFailedEventflag into event brokers). - A vigilant Central Orchestrator (or native Kafka Broker topology) explicitly captures the distress signal.
- Automatically triggers aggressive
POST /point/restore(Re-crediting earlier deducted balances forcefully) externally to the Points Server. - Consecutively fires
POST /pay/cancel(Pushing raw reversal payload to 3rd-Party Gateway logic) targeting the separated Payment Server.
💡 Core Production Mandate
Fundamentally dissect and aggressively separate synchronous external HTTP Client networking (RestClient) execution streams starkly away from pure native local DB transaction logic blocks. Imprisoning a vulnerable 30-second latent external HTTP timeout cancellation callback destructively inside an active database open Transaction aggressively decimates inner Tomcat thread pools completely. You must purposefully snap the active native physical transaction first, forcefully orchestrating asynchronous cancel webhook payload emissions elegantly through resilient @Async handlers or robust Message Queues (Kafka/RabbitMQ).