paulserban.eu

Writing Edition

Paul Serban

Writing, snippets & book notes

Cheat sheet - Post

Distributed Transaction Consistency: From ACID to BASE

← Back to Distributed Transactions Explained: Navigating the Shift from ACID to BASE

Distributed systems can't preserve traditional database transaction guarantees across independent services, forcing engineers to trade strict consistency for availability and scale using the BASE model and patterns like sagas.

Overview

Key Concepts

ACID's Four Guarantees

CAP Theorem and Two-Phase Commit

BASE and the Consistency Spectrum

The Saga Pattern

class OrderSaga {
  private completedSteps: SagaStep[] = [];
  async run(): Promise<void> {
    for (const step of this.steps) {
      try {
        await step.execute();
        this.completedSteps.push(step);
      } catch (err) {
        await this.rollback(); // runs compensate() for each completed step, in reverse
        throw new Error(`Saga aborted at step: ${step.name}`);
      }
    }
  }
}

Transactional Outbox Pattern

Idempotency

Trade-offs / Caveats

Example in Practice

A team building order placement across inventory, payment, and shipping services combines these patterns: each service commits locally and uses the Transactional Outbox to reliably publish its own events; an orchestrated Saga coordinates the steps (reserve inventory -> charge payment -> schedule shipment), with a defined compensating action for each; every event handler is idempotent via stored idempotency keys, since Kafka delivers at-least-once. Financial writes (the payment charge) may sit behind a stronger-consistency boundary, while less critical downstream effects (e.g., updating an activity feed) default to eventual consistency.

Related topics