Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is CAS in NoSQL and how to use it?

Write operations on Couchbase accept a parameter cas (create and set). Also the return result object of any non-data fetching query has cas property in it. I Googled a bit and couldn't find a good conceptual article about it.

Could anyone tell me when to use CAS and how to do it? What should be the common work-flow of using CAS?

My guess is we need to fetch CAS for the first write operation and then pass it along with next write. Also we need to update it using result's CAS. Correct me if I am wrong.

like image 269
Shiplu Mokaddim Avatar asked Mar 24 '14 05:03

Shiplu Mokaddim


People also ask

What is CAS in database?

CAS actually stands for check-and-set, and is a method of optimistic locking. The CAS value is associated with each document which is updated whenever the document changes - a bit like a revision ID.

How is Cassandra NoSQL?

Cassandra is one of the most efficient and widely-used NoSQL databases. One of the key benefits of this system is that it offers highly-available service and no single point of failure. This is key for businesses that can afford to have their system go down or to lose data.

Is Cassandra free for commercial use?

Cassandra is a free and open-source, distributed, wide-column store, NoSQL database management system designed to handle large amounts of data across many commodity servers, providing high availability with no single point of failure.


1 Answers

CAS actually stands for check-and-set, and is a method of optimistic locking. The CAS value is associated with each document which is updated whenever the document changes - a bit like a revision ID. The intent is that instead of pessimistically locking a document (and the associated lock overhead) you just read it's CAS value, and then only perform the write if the CAS matches.

The general use-case is:

  1. Read an existing document, and obtain it's current CAS (get_with_cas)
  2. Prepare a new value for that document, assuming no-one else has modified the document (and hence caused the CAS to change).
  3. Write the document using the check_and_set operation, providing the CAS value from (1).

Step 3 will only succeed (perform the write) if the document is unchanged between (1) and (3) - i.e. no other user has modified it in the meantime. Typically if (3) does fail you would retry the whole sequence (get_with_cas, modify, check_and_set).

There's a much more detailed description of check-and-set in the Couchbase Developer Guide under Concurrent Document Mutations.

like image 182
DaveR Avatar answered Nov 22 '22 06:11

DaveR