Following on from our last post, the next big feature in Apache Cassandra 2.0 that I will look at are the new type of lightweight transactions called compare and set operations.
Compare and Set (CAS)
The next big ticket item in Cassandra 2.0 are compare and set operations (CAS). These operations allow developers to perform updates, inserts and deletes when certain conditions are met in an atomic fashion. Not to be confused with batch operations or multi-statement transactions found in SQL, CAS operations allow you to perform operations like:
UPDATE test SET v1 = 5, v2 = 'foobar' WHERE k = 0 IF v1 = 3 AND v2 = 'bar';
which will only update values if, and only if, the conditions in the *IF* clause are met.
You can also use the NOT EXISTS syntax, which is pretty handy:
INSERT INTO users (name, email, hashed_password) VALUES ('ben', 'email@example.com', 'iamasupersecurehash') IF NOT EXISTS;
The above statement will only create row for the user named ben if it doesn’t already exist (assuming name is the row key).
These two examples are pretty basic, but give a good idea of what you can do with the new compare and set operations. They make common tasks, such as creating users, a lot easier!
CAS operations use the Paxos algorithm to ensure a level of consistency. However this consistency guarantee comes with a performance penalty as a single CAS operation requires all replica nodes for the row to have “a bit of a chat” about the operation before it succeeds.
The internal mechanics of CAS operations warrant their own discussion outside the scope of this post. If you are interested have a read of Paxos Made Simple for the gory details of the Paxos protocol. Additionally, you can also read the a great description of Paxos in Cassandra by Jonathan Ellis.
If you want to dive in and try the new CAS operations yourself check out the
2.0.0-beta1 branch from the Cassandra repository on GitHub.
Cassandra 2.0 Release Candidate 1 is out!