4
votes

I want to be able to save/update a document in DocumentDB and then publish an event on Azure Service Bus, and I want these to happen atomically.

Azure Service Bus does not support distributed transactions, so with a SQL database, I save my record and place a record in a message queue table, all within a SQL transaction. A process then reads the message queue table and places the messages on the queue and removing them from the queue when successful (utilizing Service Bus' deduplication in case a message is added twice).

But DocumentDB does not support transactions across collections, so I cannot update a document and then add a document to a message queue collection.

How can I ensure my changes to documents are published on the bus?

1
Paul, where do you see the documentation on how to use a DocumentDB collection as the message queue? If that allows you to configure filter parameters, then you may be able to queue the message in the same collection. Alternatively, if you are thinking that you'll have some other mechanism to pull it from a DocumentDB collection and push it on the bus, then you can definitely write that code to look for a particular type of document. I'm very interested in this topic myself. I've been implementing my own queues and topics using DocumentDB but I would love to have a real service bus.Larry Maccherone
As of 2019 cosmosdb does not support adding & updating a document in the same transaction unless you use stored procedures (And even then, they need to be in the same partition). Did you make use of stored procedures to perform these operations?Orestis P.

1 Answers

4
votes

I'm not entirely sure I understand the question, but... from a transactional standpoint, you can have both your data and your queue documents in the same collection (just make sure you have some type of differentiator such as a Type property, for filtering).

EDIT - to reflect triggers and transactions

Futher, DocumentDB supports pre- and post-trigger operations, so you could set up a post-trigger for writing an additional document after the data document insert is complete. As stated in the comments (per @Ryan): The post-trigger operations are included within the transaction.