← Lessons

quiz vs the machine

Platinum1850

Databases

Cassandra Lightweight Transactions

Conditional writes with Paxos for linearizable consistency.

6 min read · advanced · beat Platinum to climb

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.

Check yourself

Answer to earn rating on the learn ladder.

1. What consensus protocol backs Cassandra lightweight transactions?

2. Which clause makes an insert succeed only when the row is absent?

3. A main drawback of LWTs is