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
- ACID (Atomicity, Consistency, Isolation, Durability) works because a single database engine owns the entire write path - locks, logs, and recovery are all enforced by one authority.
- Once a transaction spans multiple independently-deployed services, no single component can unilaterally decide commit or abort, so full ACID semantics become expensive or impossible to preserve.
- The CAP theorem formalizes the constraint: during a network partition, a system can provide at most two of Consistency, Availability, and Partition tolerance - traditional ACID systems implicitly pick Consistency over Availability.
- BASE (Basically Available, Soft state, Eventually consistent) is the design philosophy that emerged in response, trading immediate correctness guarantees for resilience and scale.
- In practice, teams replace blocking coordination protocols (like Two-Phase Commit) with the Saga pattern plus supporting techniques (Transactional Outbox, idempotency) to get correctness back without sacrificing availability.
Key Concepts
ACID's Four Guarantees
- Atomicity: all-or-nothing - a transaction's writes either all succeed or all roll back, no partial state.
- Consistency: a transaction moves the database between valid states, respecting constraints (foreign keys, checks).
- Isolation: controls what concurrent transactions can see of each other's in-flight changes (read committed, repeatable read, serializable).
- Durability: committed effects survive crashes, typically via write-ahead logging.
- These guarantees hold cleanly in single-instance engines like PostgreSQL, MySQL InnoDB, and Oracle Database - they break down across independent systems because atomicity needs cross-system coordination, isolation needs shared locking between systems that don't trust each other, and durability now depends on multiple storage subsystems succeeding together.
CAP Theorem and Two-Phase Commit
- CAP theorem (Brewer, proven by Gilbert & Lynch): a distributed system can't guarantee Consistency, Availability, and Partition tolerance all at once during a partition - partitions are physically inevitable, so every system has already made this trade-off, knowingly or not.
- Two-Phase Commit (2PC) tries to preserve ACID across nodes by having a coordinator hold locks on all participants until everyone acknowledges readiness.
- Failure mode: if the coordinator crashes mid-protocol, participants stay blocked indefinitely holding locks - a single slow or failed node can freeze an entire cluster's throughput, which is why 2PC is largely avoided at internet scale.
BASE and the Consistency Spectrum
- Basically Available: the system stays responsive even when degraded, prioritizing a response over a guaranteed up-to-date one.
- Soft state: state can change in the background without new input, as replicas converge.
- Eventually consistent: replicas converge given enough time and no new writes - but there's no bound on how long that takes.
- BASE is a spectrum, not a single weak guarantee: systems like Google Spanner achieve strong consistency (linearizability) globally via TrueTime (synchronized atomic clocks) + Paxos; causal consistency preserves ordering only for related operations; DynamoDB, Cassandra, and Riak let you tune consistency per operation (e.g., quorum reads/writes).
- Which model to use is a product decision: a "like" counter tolerates eventual consistency fine, but a wire-transfer ledger needs strong consistency because stale reads cause real harm.
The Saga Pattern
- Replaces one atomic cross-service transaction with a sequence of local transactions, each paired with a compensating action that can undo its effect if a later step fails.
- Two coordination styles: choreography (each service reacts to events independently) vs. orchestration (a central coordinator directs each step explicitly).
- Compensations are not perfect inverses - refunding a payment doesn't erase that the customer already saw the charge or got a receipt email, so saga design must account for user-facing consequences, not just data rollback.
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
- Solves the problem of atomically updating a local database and publishing an event about that update, when the write and the publish are two separate systems.
- The event is written to an outbox table in the same local transaction as the business data change; a separate relay process (e.g., built on Debezium's change-data-capture) reads the outbox and publishes to a broker like Kafka asynchronously.
- Guarantees the event is never lost or duplicated relative to the database state, because both writes share the same local ACID transaction.
Idempotency
- Message-driven and saga-based systems retry steps and redeliver events (at-least-once delivery is the norm for brokers like Kafka and SQS), so every event-triggered operation must be safe to execute more than once.
- Enforced via idempotency keys stored alongside processed-event records, letting a handler detect and skip duplicate executions.
Trade-offs / Caveats
- Saga-based, eventually consistent designs introduce failure modes absent in single-database ACID transactions: compensations that don't cleanly reverse real-world side effects, and debugging burden when true state is spread across services with different replication lag (requiring distributed tracing and manual reconciliation).
- 2PC remains a viable option only where blocking and limited scale are acceptable - it doesn't disappear entirely, but it's rarely chosen for cross-service transactions at scale.
- Forcing an entire system into "fully ACID" or "fully BASE" is itself an anti-pattern - the former drowns in coordination overhead, the latter accumulates correctness bugs.
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
- Event sourcing and audit-trail approaches for reconciliation
- Vector clocks and conflict resolution in eventually consistent stores
- Distributed tracing and correlation IDs for saga observability