What are Cassandra lightweight transactions (LWT)?

Answer

Lightweight Transactions (LWT) provide conditional writes in Cassandra using the Paxos consensus algorithm — the only form of compare-and-set (CAS) in Cassandra. Syntax: INSERT INTO users (id, email) VALUES (uuid(), 'alice@example.com') IF NOT EXISTS;. UPDATE users SET status = 'locked' WHERE id = ? IF status = 'active';. If the condition is not met, the operation fails and Cassandra returns the current values so the client can decide what to do. How Paxos works: LWT requires 4 round trips across replicas (prepare, promise, propose, commit) — much slower than regular writes. Performance cost: LWT is 4-10x slower than regular reads/writes. Use sparingly. Use cases: ensuring unique usernames/emails, implementing distributed locks, idempotent operations (create if not exists), optimistic locking patterns. Avoid: using LWT for frequently updated data — it is a significant performance bottleneck. If possible, design the data model to avoid the need for LWT entirely.