0

I'm using NServiceBus with SQL Persistance and MSSQL Server.

I want to be sure that I don't get invalid data in my database in high concurrent scenarios.

From the docs I know that the SQL persister uses pessimistic concurrency since version 4.1.1. But I'm not sure how it works exactly because under "Starting Saga" and "Changes to Saga state" only optimistic concurrency is mentioned.

Does each saga instance only handle one message at a time and all the remaining messages are waiting in the queue to be handled by the saga?

Also is it possible to configure the concurrency to use optimistic concurrency instead of pessimistic concurrency for the SQL persister?

Stimmler
  • 322
  • 4
  • 15

1 Answers1

1

Both optimistic concurrency control and pessimistic locking result in a saga instance to only being able to handle 1 message.

Might multiple messages be handled then with :

  • Optimistic concurrency control: The 2nd+ writer will fail and the message will be retried.

  • Locking: THe lock ensures that multiple messages on the same saga instance will be delayed. Sometimes this can result in a deadlock due to lock escalation and then the message will be retried.

Regarding pessimisitic vs optimistic, you cannot choose. The latest version uses pessimistic locking. See https://docs.particular.net/persistence/sql/saga-concurrency:

Starting in version 4.1.1 conflicts cannot occur because the persistence uses pessimistic locking. Pessimistic locking is achieved by performing a SELECT ... FOR UPDATE or its dialect-specific equivalent.

Up to and including version 4.1, SQL persistence uses optimistic concurrency control when updating or deleting saga data.

Ramon Smits
  • 2,482
  • 1
  • 18
  • 20