Leveraging Outbox consistency mechanisms outside of the context of a message handler

First some background:

We are in the process of removing a home grown web api/messaging ‘synchronisation’ mechanism from our NSB based platform. This used custom message headers, a process id and a DB polling mechanism to allow us to support 200 style ‘synchronous’ web api calls over the asynchronous message based back end.

Before I go any further I will make it clear that we have long had this flagged as an anti pattern - it has just been so deeply embedded in our older API that it has been a challenge to factor it out till now.

To replace this we have implemented a command ‘executor’ pattern in our legacy web api to allow us to ‘handle’ commands synchronously in the controller code where absolutely necessary. Where this results in events being fired we are leaning on a shared subscription store so that the web api can publish events on to the appropriate consumers.

This is all going swimmingly - performance of the SPA apps that consume this legacy API has improved dramatically - the code is a huge step closer to a vanilla NSB implementation etc etc…

But there is one downside - we loose the transaction scope/unit work around these commands we handle directly within the web api. Events can go out ahead of business data fully committing to the DB, or worse could go out when the associated business data has failed to commit. At present we have had to accept this as the cost for the benefits we gain in eliminating the complex synchronisation mechanism, and it is a trade off we are quite prepared to make.

However, I want to know if the Particular team have made any progress towards supporting the outbox shared session scope outside the context of NSB message handlers - i.e. make it possible for these kinds of web api hosted business operations to be supported?

I found this page from awhile back with some suggestion of a solution - but the thread is cold: Redirecting to Google Groups

Jeremy Miller has tackled this with his implementation of outbox for his Jasper framework - he’s clearly been informed by NSBs outbox work but he has made the mental leap which decouples the outbox mechanism from a hard dependency on the handler running in the context of a back end worker: Jasper’s “Outbox” Pattern Support – The Shade Tree Developer

Any thoughts?

1 Like

Our outbox still has the same restrictions. We do have implementation ideas but currently no working solutions.

Some alternatives:

  • When using MSMQ, use distributed transactions but this does take a dependency on MSDTC.

  • Put your DB I/O and message sending in a handler, and invoke this handler from your current code.

  • In some scenario just setting a message ID to a deterministic value removes the need to use an outbox as long as your processing is idempotent.

  • Instead of invoking Send or Publish immediately, put these in a collection first with a deterministic ID, then after the DB operation committed send this collection of messages.

If these options are not possible then I’m afraid you currently have to enroll your own outbox pattern like store.

– Ramon

Hi Ramon, Thanks for your response.

We are on AWS SQS (competing consumers) + Raven. DTC would be a backward step for us.

I’m not sure I understand your second comment - you mean execute them in a handler in the context of the web api or push them back on to the bus (where they used to be). If it is the later that is certainly our fall back on a case by case basis where the need for consistency outweighs the synchronisation issues.

In that case we would use specific sagas (as opposed to the blanket custom mechanism we had) to tie together the overall process status and the API or client would revert to a polling strategy, or hook into client side notifications (we are starting to use PubNub for this).

This is ultimately the route we will go down where the specific circumstances warrant the coordination - it will be a bit of a journey though as we have 100s of command handlers triggered via the API.

I do think you guys should consider promoting the outbox ‘session’ so it can be used in either a bus or an api context (or perhaps support in ‘in process’ flavour of send local to handle initial commands synchronously. At the end of the day they are both just different flavours of application hosting processes - supporting different ‘inbound’ communication protocols.

Cheers,

Ed

1 Like

Hi,

We have exactly the same request, it would be very benificial to be able to use the outbox mechanism outside of a handler context. When you want the UI to be consistent as soon as possible, you need to work synchronously. Having an in-process SendLocal and handler that leverages the Outbox functionality would solve this. It’s not about the deduplication of messages, but for the transactionality it provides : saving business data and sending/publishing messages in one transaction.

Thanks
Karl

Could you please share which persistence and transport you are using?

Would it be an option to just do a SendLocal, where the handler does both the manipulation of the business data and a Publish and a Reply?

Imagine the database is slow, returns a deadlock exception, results in a timeout, or is even unavailable. How would you deal with this in this blocking context?

We are using the Azure Service Bus transport and SQLPersistence.

Using a SendLocal will go back to the bus, and means working asynchronously. Which delays the rendering of the UI. I agree that working asynchronously has a lot of benefits, such as the retry-mechanisms. But sometimes rendering the UI synchronously is preferred over the benefits of working asynchronously.

So how about a slow or unresponsive database?

I think that is you really want to have your process synchronous that that does not mean you cannot use asynchronous communication.

If you would use a handler that does both a publish + reply and combine that with callbacks that you have the best of both worlds:

Imagine that you would have an outbox that does supports it could still occur that the database is slow or unresponsive.

In the case with callbacks you can at least have it have a best effort including retry logic and even if the client disconnects or does not retry that the task will eventually be done.

So, if I understand correctly:

  • first do a SendLocal of a command from/to the web api endpoint
  • the commandhandler saves the business data to the database, publishes an integration event and replies to the sender (the web api endpoint itself). If we enable the outbox in the web api endpoint, these three actions will be done transactionally and will leverage retries.
  • the web api receives the reply either in a callback or in a handler and sends the HTTP response to the caller of the API

Is this correct? Or am I still missing something? :slight_smile:

Hi @ktheeten ,

We have released outbox support for ASP.NET Core scenarios with the transactional session package.

See for example this sample showing how to integrate SQL persistence with entity framework in ASP.NET Core

Regards,
Daniel

1 Like

Hi @ktheeten ,

We have updated the ASP.NET Core sample to show a better approach on how to deal with the transactional session integration into the ASP.NET Core pipeline.

The tweaked approach selectively opts in for transactional session by using parameter injection

[HttpGet]
public async Task<string> Get([FromServices] ITransactionalSession messageSession)

which makes sure the transactional session interaction only happens for routes that actually need it.

Regards,
Daniel