Wednesday 15 January 2014

Atomically increment an integer in a document in Azure DocumentDB -



Atomically increment an integer in a document in Azure DocumentDB -

how can atomically increment integer in document in azure documentdb?

the increment operation must atomic in presence of concurrent writers. no lost increments allowed (they possible in naive read-modify-write algorithm).

from documentation:

how documentdb provide concurrency?

documentdb supports optimistic concurrency command (occ) through http entity tags or etags. every documentdb resource has etag, , documentdb clients include latest read version in write requests. if etag current, alter committed. if value has been changed externally, server rejects write "http 412 precondition failure" response code. clients must read latest version of resource , retry request.

one can utilize property implement cas loop:

while (true) { var existingdoc = readdoc(); existingdoc.int++; seek { writedoc(existingdoc); break; } grab { //concurrency violation continue; } }

also note transactions in azure documentdb run on snapshot of info (snapshot isolation).

btw: if bit more automated (auto increment when modifies document) can utilize trigger , separated collection current value of integer. triggers executed in same transaction consistent , automated.

azure azure-documentdb

No comments:

Post a Comment