Database / Apache Cassandra Intermediate and Advanced interview questions
What are lightweight transactions (LWT) in Cassandra?
Lightweight transactions give Cassandra a way to do compare-and-set style operations — "only apply this write if a condition holds" — which normal writes cannot express, since ordinary writes are unconditional and last-write-wins.
INSERT INTO users (user_id, email) VALUES ('u1', 'a@b.com') IF NOT EXISTS; UPDATE accounts SET balance = 150 WHERE account_id = 'acc1' IF balance = 100;
IF NOT EXISTSguarantees a row is only created if it doesn't already exist — useful for uniqueness constraints like usernames or reservations.IF <condition>applies an update only when the current stored value matches, enabling optimistic-concurrency patterns.
Under the hood, LWTs use the Paxos consensus protocol among replicas to agree on whether the condition holds and the write should proceed, rather than the normal quorum-write path. This makes them linearizable but noticeably slower and more resource-intensive than regular writes, so they should be reserved for the specific operations that truly need conditional semantics, not used as a default write pattern.
More Related questions...