Skip to content
Dev Dump

Distributed Transaction Handling

  • A transaction is a group of operations executed as a single unit, ensuring data integrity and consistency.
  • Key Principle: All or Nothing - if any operation fails, the entire transaction is rolled back.
  • Example: Transferring money:
    • Debit from Person A and credit to Person B. If any step fails, rollback the entire transaction.
  • ACID Properties ensure reliability in transactions.

Monolithic Architecture: Transaction Management

Section titled “Monolithic Architecture: Transaction Management”

Definition: A single, unified application where all components (database, business logic, UI) are tightly integrated.

  • Transaction Process:

    1. Inventory Check and Update
    2. Payment Processing
    3. Order Recording
  • Key Point: All steps occur in a single database transaction, ensuring ACID properties.

  • Advantage: Simple transaction management due to centralized data.

Monolithic Transaction

Microservices Architecture: Transaction Management

Section titled “Microservices Architecture: Transaction Management”

Definition: Independent services, each with its own data store, coordinating through APIs and events.

  • Transaction Process:

    1. Inventory Service: Checks and updates stock.
    2. Payment Service: Processes payment.
    3. Order Service: Records the order.
  • Key Point: Transactions span multiple services and databases, requiring compensating actions for failures.

  • Challenge: Complex transaction management due to distributed nature.

Microservice Transaction

ArchitectureTransaction ScopeComplexity
MonolithicSingle database, ACID guaranteedSimple
MicroservicesMultiple databases, requires coordinationComplex
  • A transaction that spans multiple services and databases in a microservices architecture.

A protocol ensuring all services agree to commit or roll back changes together.

  1. Prepare Phase: Coordinator asks all services to prepare (lock resources and verify readiness)
  2. Commit Phase: If all services respond “ready”, coordinator commits; otherwise, rolls back

2PC Implementation

When any service fails during prepare or commit:

  1. Coordinator detects the failure
  2. Sends rollback request to all participants
  3. Each participant undoes prepared changes
  4. Transaction aborts and client is notified

2PC Rollback

ProsCons
Strong consistencyBlocking (participants wait for coordinator)
Atomic guaranteesSingle point of failure
Simple mental modelComplex failure recovery

A series of local transactions with compensating actions to undo changes on failure. Focuses on eventual consistency rather than strong consistency.

SAGA Pattern

  1. Each service performs its local transaction and emits an event
  2. Next service listens, processes, and emits its own event
  3. On failure, compensating transactions revert previous changes

Example: Order Service creates order → emits “OrderCreated” → Payment Service processes payment → emits “PaymentBilled” → if Payment fails, Order Service reverts the order.

No central orchestrator. Services communicate via events directly.

SAGA Choreography

ProsCons
Decentralized and scalableComplex event management
No single point of failureHard to track transaction state
Good for simple workflowsDebugging is challenging

A central orchestrator manages the transaction flow and rollbacks.

SAGA Orchestration

ProsCons
Centralized controlSingle point of failure
Easier to manage and debugOrchestrator can become bottleneck
Clear transaction visibilityRequires orchestrator infrastructure
Aspect2PCSAGA
CoordinationCentralized coordinatorChoreography (decentralized) or Orchestration (centralized)
CommunicationSynchronousAsynchronous
ConsistencyStrong (ACID)Eventual
PerformanceSlower (blocking)Faster (non-blocking)
Failure HandlingRollback allCompensating transactions
Best ForShort transactions, few participantsLong-running, complex workflows
ScenarioRecommended Pattern
Financial transactions requiring atomicity2PC
E-commerce order with multiple servicesSAGA (Orchestration)
Event-driven microservicesSAGA (Choreography)
Cross-database transactions in same region2PC
Geo-distributed servicesSAGA
  • 2PC: Strong consistency through blocking coordination. Use when atomicity is critical and participants are few.
  • SAGA: Eventual consistency through compensating actions. Use for long-running, complex distributed workflows.
  • Choose based on consistency requirements vs. availability and performance needs.