Do I need to worry about a CAS value Error when doing a series of subdocument mutations

I am doing a subdocument mutation (javascript SDK) like so (psuedo code):

let mutateIn  = bucket.getMutateIn(path);
mutateIn.counter('subpathX.counter', 1)
mutateIn.replace('subpathX.lastUpdated', Date.now())
mutateIn.execute();

the docs say that subdoc mutations are atomic, but I’m not sure what that means exactly?

does it mean that the operation is an atomic unit that will bring the document from some state x to state y where a race condition is impossible and there will NEVER be CAS failure (because the database will handle the queuing).

Or does it simply mean that it’s an atomic transaction that will either succeed entirely or fail entirely, but can absolutely fail because of a cas problem, where some other process mutated the doc before this subdocument mutation completed.

Please help

anyone, anyone, bueller

there’s someone at cb who can answer this easily, please chime in

Ok I ran a test. I had two node processes make 100k of these mutations simultaneously on the same sub-document of the same document. no delay at all, and all the 200k mutations persisted.

Now, I tested against a one node setup (sitting on the lan) but I believe that even in multi-node setups, each document’s writings are the responsibility of one and only one node. If so, this test should speak also to a mult-node deployment.

So, at this point it’s reasonable to assume that the database handles the queuing and indeed, these operations will not fail because of cas.

I wish I could get some confirmation on this though.

UPDATE:

I ran a better test:

I had 3 processes do 100k simultaneous mutations on two fields on 2 different subdocuments within the same document, and all 300k mutations persisted without a hitch. (to be clear all 3 processes mutated the same two fields on the same 2 sub documents)

Essentially this.

It means that a subdoc mutation which modifies one or more paths in a document will always perform all updates, or none of them (if an error occurs).

So in your example, you will always see both subpathX.counter be incremented and subpathX.lastUpdated set to the current time, or neither (and an error code will be returned.

Note that you’ll only get a CAS error if your subdoc mutation attempts to modify a specific version of a document - i.e. you specify a CAS value in your mutation request. If another client modified the document (and the CAS changed) before your request was processed then that’s when you’d see a CAS mismatch error (and none of your changes would be applied).

1 Like

Note that you’ll only get a CAS error if your subdoc mutation attempts to modify a specific version of a document - i.e. you specify a CAS value in your mutation request. If another client modified the document (and the CAS changed) before your request was processed then that’s when you’d see a CAS mismatch error (and none of your changes would be applied).

this is gold, and exactly what I was looking for and it also fits in with my test results.

thank you for the response