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:
| Atomic | All-or-nothing. |
|---|---|
| Consistent | The database moves from one valid state to another. |
| Isolated | Concurrent transactions don't see each other's intermediate state. |
| Durable | Committed 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 Latency Cost
| Normal write at QUORUM 1 round trip to W replicas | 1 RT |
| LWT Prepare + read + Propose + Commit | 4 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 round trip for Prepare (to all replicas)
- 1 round trip for read (quorum of replicas)
- 1 round trip for Propose (all replicas)
- 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:
- 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).
- 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). - 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.
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.
| Situation | Better approach |
|---|---|
| Enforce a unique constraint across all users | Use a separate table as a "lock" table with IF NOT EXISTS — one row per unique value; write to it first |
| Decrement a counter atomically | Use 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 uniqueness | Use application-level idempotency keys written with IF NOT EXISTS only on the first attempt |
| Complex business logic with many conditional writes | Consider moving to a database with native transaction support for that part of the application |
- 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.