Lightweight Transactions and ACID

Every part so far has described a database built so that no node ever has to ask another node for permission: any replica accepts any write, and timestamps sort it out afterwards. This part is about the one escape hatch from that design — the conditional write, IF NOT EXISTS / IF col = x — and what it costs to bolt agreement onto a leaderless system. The consequence worth internalizing is that an LWT is a single-partition compare-and-set paid for in four network round trips, not a transaction: it gives you linearizable "check then write" on one partition, and nothing that spans two.

Why ACID Is Expensive in a Leaderless System

ACID requires that a transaction is:

AtomicAll-or-nothing.
ConsistentThe database moves from one valid state to another.
IsolatedConcurrent transactions don't see each other's intermediate state.
DurableCommitted writes survive crashes.

Single-leader (e.g. Postgres)

The leader serializes all writes. Atomicity and isolation come "for free" because one node is the authority — there is no ambiguity about which write happened first.

Leaderless (Cassandra)

Any node can accept any write at any time. Two coordinators can process writes to the same row simultaneously, and neither knows about the other until after both are committed. There is no single authority to serialize conflicting operations.

Of the four letters, Durability is the one Cassandra already delivers on every write (the commit log, Part 4 §2). Isolation is the one the leaderless design gives up: with no authority, "which write came first" is answered after the fact by timestamps — last-write-wins (Part 3 §3) — not before the fact by anyone.

To achieve isolation in a leaderless system, you need a distributed coordination protocol that forces nodes to agree on a single ordering before any write is applied. That protocol is Paxos — and it is expensive.

  • Guarantees: Durability of every acknowledged write, and a deterministic winner (highest timestamp) when concurrent writes conflict.
  • Does not guarantee: Isolation for ordinary writes. Two concurrent writers are never told about each other; one simply shadows the other.

Lightweight Transactions (LWT)

Cassandra provides compare-and-set (CAS) semantics through Lightweight Transactions. The syntax:

Only insert if the row does not already exist

-- Only insert if the row does not already exist
INSERT INTO user_accounts (user_id, email)
VALUES ('abc123', 'user@example.com')
IF NOT EXISTS;

Only update if a condition holds

-- Only update if a condition holds
UPDATE reservations
SET status = 'confirmed'
WHERE reservation_id = 'xyz'
IF status = 'pending';
  • Guarantees: The condition is evaluated against the latest committed value of the partition, and the write is applied only if it still holds at that moment — no other LWT on that partition can interleave.
  • Does not guarantee: Anything across partitions, or protection from plain (non-IF) writes to the same row, which bypass Paxos entirely (see §3).

How LWT Uses Paxos

Cassandra implements single-partition Paxos — the guarantee only applies to operations on one partition. The four-phase protocol:

The four phases as the guide lists them. Phase 1, Prepare: the coordinator broadcasts that it intends to write and replicas report any in-progress proposals. Phase 2, Promise (read): the coordinator reads the current value from a quorum; this is the compare. Phase 3, Propose: if the condition holds, the coordinator proposes the new value and replicas accept or reject. Phase 4, Commit: if a quorum accepted, the write commits; if a competing Paxos round is in progress, the coordinator retries.
The four phases — compare, then set, with a quorum behind each step
Sequence for one uncontended LWT with RF 3. The client sends UPDATE … IF status = pending to the coordinator. Round trip 1, prepare and promise: the coordinator sends Prepare with ballot b and a quorum promises not to accept older ballots. Round trip 2, read: a quorum returns status = pending, and the coordinator finds the condition holds. Round trip 3, propose and accept: it proposes status = confirmed under ballot b and a quorum accepts. Round trip 4, commit: it sends Commit and gets acks. Only then does the client get applied = true. An ordinary write needs one round trip.
One uncontended LWT, RF = 3 — four round trips, each needing a quorum

The Latency Cost

Normal write at QUORUM  1 round trip to W replicas1 RT
LWT  Prepare + read + Propose + Commit4 RT
Bar widths are illustrative — round trips, not milliseconds. Contention adds retries on top of the LWT bar.

A normal write at QUORUM involves:

  • 1 round trip to W replicas

An LWT involves:

  1. 1 round trip for Prepare (to all replicas)
  2. 1 round trip for read (quorum of replicas)
  3. 1 round trip for Propose (all replicas)
  4. 1 round trip for Commit (all replicas)

LWT is roughly 4–6× the latency of a regular write at QUORUM, and significantly higher than that compared to LOCAL_QUORUM or ONE.

  • Guarantees: Linearizable compare-and-set among all LWTs on one partition, surviving coordinator failure mid-round.
  • Does not guarantee: Bounded latency (contention retries are unbounded in principle), multi-partition atomicity, or ordering against non-LWT writes.

When to Use LWT

Use LWT when:

  1. Uniqueness enforcement — you need to ensure a value is written only once and only if it doesn't already exist (e.g., user registration, idempotency keys).
  2. State machine transitions — a record can only move from state A to state B if it is currently in state A (e.g., IF status = 'pending' before confirming an order).
  3. Leader election — one node claims a resource and needs to win the race.

These use cases share a property: the correctness of the write depends on reading the current state. Without LWT, a read-modify-write cycle has a race condition.

Without IF NOT EXISTS: client A and client B both SELECT the row for email x and both get no row. Client A inserts email x for user A at timestamp 100, client B inserts it for user B at timestamp 101, and both are told success. Last-write-wins keeps B; user A’s registration silently disappears. With INSERT … IF NOT EXISTS, the check and the write are one Paxos round, so exactly one gets applied = true and the other gets applied = false with the current row.
The race LWT exists to close — both clients check, both write, both are told success

This is the same failure the read-a-value-then-write-it-back pattern produces in Part 3 §6: both writes "succeed", and one silently disappears. It is also the boundary drawn in Part 4 §5 — blind overwrites are fast; writes whose new value depends on the old one are not.

  • Guarantees: For each of the three patterns, exactly one competing writer wins, and every loser is told so via [applied] = false.
  • Does not guarantee: That the pattern is affordable at high write rates. Each use is a 4-round-trip operation, so it belongs on the rare, correctness-critical write, not the hot path.

When to Redesign Instead of Using LWT

LWT is not a general-purpose transaction mechanism. If you find yourself wanting LWT frequently, you likely have a data model that fights Cassandra's nature.

SituationBetter approach
Enforce a unique constraint across all usersUse a separate table as a "lock" table with IF NOT EXISTS — one row per unique value; write to it first
Decrement a counter atomicallyUse Cassandra's built-in counter columns (they use a different internal mechanism, not Paxos)
Multi-row transactions (update rows in multiple partitions atomically)LWT cannot do this. Redesign your model so the write is within one partition, or use a different database
High-throughput operations that occasionally need uniquenessUse application-level idempotency keys written with IF NOT EXISTS only on the first attempt
Complex business logic with many conditional writesConsider moving to a database with native transaction support for that part of the application
A decision ladder of three questions. First: does the write depend on the current stored value? If no, use a plain write, a blind upsert on the fast path with last-write-wins. If yes: is everything it touches in one partition? If no, LWT cannot do it; redesign into one partition or use a transactional database. If yes: is it rare relative to the table’s write rate? If yes, use LWT, IF NOT EXISTS or IF col = x. If it is the hot path, redesign the data model or move that part elsewhere.
Choosing between a plain write, LWT, and a redesign — three questions, in order
  • Guarantees: Each alternative in the table keeps the hot path on ordinary writes and confines Paxos (if any) to a narrow, low-contention partition.
  • Does not guarantee: Atomicity across the separate writes these patterns introduce (lock row then data row). Cassandra has no multi-partition transaction to fall back on — the application owns that gap.