What happens if saga receives two messages at the same time?


(Alexander) #1

What happens if an existing saga receives two messages of the same type at the same time? Will one of them fail? Will saga process them at the same time or one after another?
Thanks


(Sean Farmar) #2

The saga persistence is using optimistic concurrency, so it will try to process both (let’s say on two separate threads) one will succeed and the other will fail, the one that failed (lost the race) will be retried and now there is no conflicts it will succeed…

Does that make sense?


(Alexander) #3

oh, hi, Sean! long time no talk!
What makes the second one fail?


(Alexander) #4

What makes the second one fail?


(Sean Farmar) #5

Hey, sorry, edited my reply to:

one will succeed and the other will fail, the one that failed (lost the race) will

Does that make sense?


(Alexander) #6

I’m trying to understand what in particular makes the second one fail (i.e., locked saga data record?). Also, want to make sure this applies not only to the first message but also to all subsequent messages regardless whether the saga is started by the message handles it or simply handles it.


(Dennis van der Stelt) #7

Hi Alexander,

It will indeed fail with locking. Depending on the persistence used it will use pessimistic or optimistic locking and uniqueness constraints. It can happen inside a single endpoint with multiple threads or scaled out endpoints that multiple messages create and/or update the same saga instance. NServiceBus will always try to make sure this doesn’t happen.