Azure Cosmos DB provides full ACID transactions within a single logical partition via TransactionalBatch and stored procedures. Applications that need atomicity across multiple partitions or containers must hand-roll compensating patterns (Saga, event sourcing) at significant complexity cost. Two gaps exist today within a single database account:
-
Cross-partition write atomicity — there is no API to atomically commit mutations spanning multiple partition keys or containers. Any scenario (ledger transfers, order+inventory, multi-entity state machines) requires application-level compensating logic that is error-prone and non-portable across languages.
-
Consistent cross-partition reads —
ReadManyItemsAsyncfans out parallel queries per partition range with no snapshot guarantee, making it vulnerable to read skew: two items returned in the same call can reflect different logical instants (e.g., one item seen before a concurrent transfer and another after it).
The Cosmos DB Gateway exposes a distributed transaction coordinator at POST /operations/dtc. The same endpoint handles both atomic writes and consistent snapshot reads — the operationType field in the JSON request body ("Write" or "Read") selects the behavior.
- Add
CosmosClient.CreateDistributedWriteTransaction()returning a newDistributedWriteTransactionbuilder. - Add
CosmosClient.CreateDistributedReadTransaction()returning a newDistributedReadTransactionbuilder. DistributedWriteTransactionaccumulates Create, Replace, Delete, Upsert, and Patch operations across any partitions and containers within the same account, then commits them atomically viaCommitTransactionAsync().DistributedReadTransactionaccumulates ReadItem operations across any partitions and containers, then executes them as a single consistent server-side snapshot viaCommitTransactionAsync().- New wire operations:
CommitDistributedTransaction(reused for both write and read transactions; the distinction is in the request body). - Session tokens are merged into the client's
ISessionContainerafter every commit or execute. - Per-operation ETags are returned in both write and read responses, enabling ETag-gated follow-up writes.
distributed-write-transaction: AddsCosmosClient.CreateDistributedWriteTransaction(),DistributedWriteTransaction,DistributedTransactionResponse, and related types for cross-partition, cross-container atomic writes within the same database account.distributed-read-transaction: AddsCosmosClient.CreateDistributedReadTransaction(),DistributedReadTransaction, and related types for cross-partition, cross-container consistent snapshot reads within the same database account. ReusesDistributedTransactionResponseas the shared response type for both write and read transactions.
client-and-configuration:CosmosClientgains two new factory methods:CreateDistributedWriteTransaction()andCreateDistributedReadTransaction().
- Public API surface: New abstract classes
DistributedWriteTransactionandDistributedReadTransaction; a single sharedDistributedTransactionResponseresponse type for both (withIdempotencyTokenasGuid?, non-null for writes, null for reads); new options classes; two new factory methods onCosmosClient. Initially behind#if INTERNAL; promoted to public at GA. - Wire protocol: Uses
POST /operations/dtcfor reads and writes. - Gateway routing:
GatewayStoreModel,GatewayStoreClient, andRequestInvokerHandlergain awareness of both operation types;x-ms-session-tokenis suppressed for both (per-partition tokens travel in the body). - Existing behavior: No change for any existing API. Both features are entirely additive.
- Tests: New unit tests (serialization, response parsing, session token merge) and emulator integration tests for both write and read transactions.