A C# application stores customer summary items, order items, and payment items in a container named customer that is partitioned by /customerId. Every item carries the customerId of the customer it belongs to. Order ids are unique GUIDs that differ from the customer id. Audit records are stored in a separate container named audit that is partitioned by /auditDate.
At checkout, the application must:
- Create the order item, create the payment item, and increment
orderCounton the customer summary item so that all three commit together or none do. - Write one audit record per order; the audit record may appear a few seconds after the order.
- Keep all write logic in C# application code that is deployed through the team's CI/CD pipeline.
Which design meets all the requirements?
A transactional batch commits atomically only for operations that target one logical partition key value, so the order, payment, and counter patch must be batched under the customer's partition key in the customer container. The audit record lives in a different container with a different partition key, so it cannot join that batch; because a short delay is acceptable, change feed processing can write it after the checkout commits. A stored procedure would also be transactional but must be written in JavaScript, and bulk execution provides no atomicity.
- Not correct: Run a transactional batch on
customerwith the order, payment, and counter patch, using the order id as the batch's partition key value. Every item in a batch must match the batch's partition key value; these items carry the customerId, so a batch keyed on the order id fails instead of committing. - Not correct: Use a CosmosClient with AllowBulkExecution set to true to write the order, payment, counter patch, and audit record concurrently. Bulk execution offers no transactional guarantee; each operation succeeds or fails independently, so a partial checkout could be committed.
- Not correct: Run a transactional batch on
customerwith the order, payment, counter patch, and audit record under the customer's partition key. A transactional batch is scoped to a single logical partition key value, so the audit record, which lives in another container and partition, cannot be part of it. - Not correct: Call a JavaScript stored procedure on
customerthat creates the order and payment and increments the counter, and write the audit record from a change feed processor. Stored procedures do provide a transactional scope, but they must be written in JavaScript, which breaks the requirement to keep write logic in C# application code. - Correct: Run a transactional batch on
customerwith the order, payment, and counter patch under the customer's partition key, and write the audit record from a change feed processor. The three items share the customer's partition key value in one container, so a C# transactional batch commits them atomically. In the C# change feed processor, handle order creations and use the unique order ID to make audit writes idempotent so that retries leave one audit record per order.