Lightweight Transactions (LWT) & Compare-and-Set
Understand how Cassandra achieves linearizable compare-and-set semantics despite its eventually consistent design.
Cassandra is fundamentally an eventually consistent, AP-leaning system, but certain use cases — like ensuring a username is unique, or implementing optimistic locking — require linearizable consistency: an operation that only succeeds if a condition is atomically checked and satisfied at the moment of the write. This is provided by Lightweight Transactions (LWT), using IF clauses such as IF NOT EXISTS or IF column = value.
A normal Cassandra write is like shouting an update to everyone in the room without waiting to see if it's already been changed. An LWT is like calling a formal meeting, taking attendance, checking the current state, voting on whether to accept the change, and only then confirming it — much slower, but guarantees no conflicting change slips through.