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?
- A.container.upsert_item(body) inside a try block that retries five times
- B.container.replace_item(item, body, etag=item['_etag'], match_condition=MatchConditions.IfNotModified)
- C.container.replace_item(item, body) after switching the account to Strong consistency
- 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
- Importing a public image into ACR
- Event-driven Container Apps job
- Horizontal Pod Autoscaler on CPU
- Container TTL for chat history
- IVFFlat lists and probes tuning
- Entra ID connection to Azure Managed Redis
- Deferring Service Bus messages
- Event Grid system topic for storage events
- NCRONTAB timer trigger schedule
- User-assigned identity shared by apps
- KQL distinct counts with dcount