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.
Previous
What is data denormalization in Cassandra?
Next
What is Cassandra's vnodes (virtual nodes)?
More RabbitMQ & Cassandra Questions
View all →- Intermediate How does Cassandra's read path work?
- Intermediate What is Cassandra's compaction and its types?
- Intermediate What are RabbitMQ's quorum queues?
- Intermediate How do you model time-series data in Cassandra?
- Intermediate What is Cassandra's consistency vs availability trade-off with tunable consistency?