DImitri Sturm
DImitri Sturm

Reputation: 1

Add data to database and queue without transactions using NServiceBus

I'm currently developing a REST api. The api performs basic crud operations. Data is synced to a legacy system using RabbitMQ. The api is running on SQL Server as a DB.

I'm wondering how to make sure data is saved in the DB and a message is put on the bus.

The fact you are missing distributed transactions looks like a very general issue to me so I'm wondering if there are any best practices using NServiceBus to solve this issue?

Upvotes: 0

Views: 66

Answers (1)

Mauro Servienti
Mauro Servienti

Reputation: 518

RabbitMQ doesn't support distributed transactions on its own, so there isn't much NServiceBus can do in this scenario. One option though is:

  • The endpoint is configured to use the Outbox feature
  • when the HTTP request is received by the REST endpoint a message is sent locally to self. No DB operations are performed at this stage
  • when the sent-to-self message is received you're now in the context of an incoming message and you can:
    • execute CRUD operations
    • send outgoing messages
  • The outbox will guarantee consistency even if there are no distributed transactions

Upvotes: 1

Related Questions