CertKeen

Azure AI Cloud Developer (AI-200) · Free practice question 5 of 12

Optimistic concurrency with ETag

Two instances of a profile service at Corriedale Bank sometimes overwrite each other's updates to the same Cosmos DB item. The team wants an update to fail if the item changed after it was read. How should the Python code replace the item?

  1. A.container.upsert_item(body) inside a try block that retries five times
  2. B.container.replace_item(item, body, etag=item['_etag'], match_condition=MatchConditions.IfNotModified)
  3. C.container.replace_item(item, body) after switching the account to Strong consistency
  4. D.container.patch_item(item, partition_key, patch_operations=[]) to take an exclusive lock on the item before replacing it
Show answer and explanation

Correct answer: B. container.replace_item(item, body, etag=item['_etag'], match_condition=MatchConditions.IfNotModified)

Why: Cosmos DB supports optimistic concurrency through the item's _etag: passing the ETag with an if-not-modified match condition makes the write fail with HTTP 412 if another writer changed the item, so the caller can re-read and retry. Retried upserts still overwrite blindly. Strong consistency controls read visibility, not write conflicts, and an empty patch doesn't lock anything.

More free Azure AI Cloud Developer (AI-200) questions