Concurrency control keeps simultaneous operations from corrupting shared data. In Azure Cosmos DB, optimistic concurrency lets operations proceed without locking, then checks for conflicts before accepting an update. section 11.4.3
Cosmos DB uses a version-checking workflow:
- A read stores the document’s ETag, which represents its current version. section 11.4.3
- An update includes that ETag and succeeds only if the document has not changed since it was read. section 11.4.3
- If another operation changed the document, the ETag no longer matches and the update fails instead of overwriting newer data. section 11.4.3
- The application can retry the read-update cycle using the latest document version. section 11.4.3
Updating inventory during a busy sale
Suppose two shoppers trigger inventory updates for the same product. Each operation reads the product and its ETag. The first update changes the document and its ETag; the second update detects the mismatch, fails safely, and can reread the inventory before retrying. section 11.4.3
Figure 11.7 A read operation stores the ETag value for concurrency in future updates.
A read operation stores the ETag for use in a later conditional update. section 11.4.3
