Beyond last write wins
Normal Cassandra writes are last write wins and cannot express conditions safely. Lightweight transactions (LWT) add compare and set semantics for a single partition.
The IF clause
LWTs use an IF condition on an insert or update.
- IF NOT EXISTS prevents overwriting an existing row, useful for unique registration.
- IF column equals value updates only when the current state matches, enabling safe state machines.
Paxos under the hood
To make conditional writes linearizable, Cassandra runs a Paxos consensus round across the replicas before applying the change.
- This adds four round trips, so LWTs are far slower than normal writes.
- A special SERIAL consistency level governs the Paxos phase.
When to use them
- Use LWTs sparingly for correctness critical operations like claiming a username.
- Contention on the same partition causes Paxos conflicts and retries, so avoid hot keys.
Diagram
Key idea
Lightweight transactions add conditional compare and set writes via Paxos for linearizable correctness, at a high latency cost, so use them only where needed.