How to handle message order in nservicebus?

别等时光非礼了梦想. 提交于 2019-12-06 05:27:16

Out of the box one of the primary guidelines around NserviceBus is that you should build your systems in a way that order doesnt matter. Having said that i have built an ordered system with NSB before, heres how I decided to do it:

  • Add a sequence number to all messages
  • in the receiver check the sequence number is the last seen number + 1 if not throw an out of sequence exception
  • Enable second level retries (so if they are out of order they will try again later hopefully after the correct message was received)

This generally works pretty well, however sometimes stuff will go a bit out of whack if something is out of order for too long and manual intervention to resequence is required.

In your scenario there is probably a better way. Given you are only wanting ordering within the revisions which are made to orders. I think you can build this in a way that doesn't require ordered delivery.

  • Add a revision number to all the fields which you can modify with a revision
  • only update the field if the revision number in the message is >= the last revision in the db

This has a bunch of benefits.

  • It doesn't rely on order
  • It reduces load by only requiring the receiver to process each message once
  • it deals pretty well with errors by not stopping everything if there's an issue with a single message.

But it has the following drawbacks:

  • Added complexity in the db
  • Its eventually consistant, if you look at the db it may only contain some of the edits a user has done.
  • If rev2 errors and rev3 is processed correctly some of the users edits wont be there but some will
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!