Saga Pattern distributed transactions


(Alfonso) #1

I dont understand how to store my registres if i used a SAGA and I saw a example about SQLPersistence.

I have a two databases (?)
MyDBSaga (only persist the saga)
MyDB (storage of my data, tables, sp, views, functions, etc.)

My saga

Method CreateBooking
	Call a endpoint to booking
		//Process in other endpoint
		Save booking entity in MyDBSaga or in MyDB?
		<--- return to IBookingCreated (only id or all entity)

Method IBookingCreated 	
	Call endpoint to generate a sale
		//Process in other endpoint
		Save sale entity in MyDBSaga or in MyDB?
		<--- return to ISaleCreated (only id or all entity)

Method ISaleCreated
	Call endpoint to save all entities (Booking and Sale)?
        MarkAsComplete();

Ass the saga pattern works, I do not fully understand how guarantee and use distributed transactions.
For example, what to do in case the sale is not completed?


(Daniel Marbach) #2

Good day Alfonso

You mentioned you do not fully understand how to guarantee and use distributed transactions. There are two sides of the coin here. There is the transactional guarantees that the SQL persistence provides you and the business transaction guarantees or SLAs you have to model with your sagas.

For the SQL persistence side of things if you have two databases like you outlined above as long as they are not hosted on different SQL Servers no distributed transaction is needed to guarantee that the saga data and the business data is written when the message completed or rolled back if the message was not successfully handled.

When it comes to the business transaction you need to track the state of the processing on the saga data. So whenever a message comes back from on of the other endpoints that is involved in processing the commands that are sent out by the saga the saga would check its state and then complete itself if all the steps needed to take are completed.

A saga should whenever possible only modify the saga state and send out messages for further processing or react to messages as a consequence of sent out messages that have been processed.

Our docs site has pretty intensive documentation around designing sagas that can be found here

https://docs.particular.net/nservicebus/sagas/

the part about accessing databases and other resources from a saga is covered in

https://docs.particular.net/nservicebus/sagas/#accessing-databases-and-other-resources-from-a-saga

Hope that helps

Regards
Daniel