Copyright (c) 2025 MindMesh Academy. All rights reserved. This content is proprietary and may not be reproduced or distributed without permission.

4.1.2.2. Implement Cosmos DB Operations

First Principle: Azure Cosmos DB enables CRUD (Create, Read, Update, Delete) operations using SDKs for its various APIs. Understanding Request Units (RUs) and Optimistic Concurrency Control (OCC) is crucial for optimizing cost, performance, and data integrity.

What It Is: Cosmos DB operations refer to the fundamental data interactions (CRUD: Create, Read, Update, Delete) performed on Cosmos DB containers.

CRUD Operations:
  • Create: container.items.create(item) inserts a new document (or entity) into a container.
  • Read: container.item(id, pk).read() fetches a document by its unique ID and partition key. This is the fastest read operation (point read).
  • Update: container.item(id, pk).replace(updatedItem) replaces an existing document with a new version.
  • Delete: container.item(id, pk).delete() removes a document from the container.

Request Units (RUs) are Cosmos DB’s normalized measure of operation cost. Each operation (read, write, query) consumes RUs based on complexity and data size. You provision RU/s for containers or databases, which determines throughput and billing. Exceeding your RU/s quota results in throttling (HTTP 429), meaning requests are temporarily rejected until capacity is available. Efficient queries and indexing reduce RU consumption and cost.

Optimistic Concurrency Control (OCC) prevents conflicting updates in Cosmos DB. Each document has an ETag (entity tag) property, which is a version identifier. When updating a document, you can supply the ETag in the If-Match header. The update only succeeds if the ETag matches the current value on the server, ensuring no other process has modified the document since it was last read.

Example (OCC with ETag):
// Assume 'item' is the original document retrieved, and 'item.etag' holds its ETag
const updatedItem = { ...item, newProperty: "newValue" };
await container
  .item(id, pk)
  .replace(updatedItem, { accessCondition: { type: "IfMatch", condition: item.etag } });

Batch operations (transactional batch) allow multiple operations to be performed in a single request within the same partition key, ensuring atomicity (all or nothing).

Scenario: You are developing a mobile application that allows users to update their profiles in Cosmos DB. You need to ensure that updates to a user's profile are always based on the latest version of the document, preventing data inconsistencies if multiple updates happen concurrently. You also need to monitor and optimize the cost of these read/write operations.

Reflection Question: How do Cosmos DB operations (CRUD), combined with the concept of Request Units (RUs) for cost measurement and Optimistic Concurrency Control (OCC) (via ETag), fundamentally enable developers to optimize cost, performance, and data integrity for applications requiring low-latency, globally distributed NoSQL data?