azureazure-eventhuboutbox-pattern

How to secure reliable publication when send event about successful db insertion to Event Hub?


Context:

  1. In Azure function with EventHubTrigger, I save data mapped from handled event to database (through the Entity framework). This action performs synchronously
  2. Trigger a new event about successful data insertion using event hub producer. This action is async
  3. Handle that triggered event at some other place

I guess it might happen that something fails during saving data, so I am wondering how to prevent inconsistency and secure that event is not sent if it should not. As far as I know Azure Event Hub has no outbox pattern implemented yet, so I guess I would need to mimic it somehow.

I am also thinking about alternative and a bit smelly solution to make this publish event method synchronous in step 2 (even if nature of the event-driven is to be async) and to add an addition check between step 1 and step 2 - to make sure that everything is saved in db. Only if that condition is fulfilled, event is going to be triggered (step 3).

Any advice?


Solution

  • There's nothing in the SDK that would manage distributed transactions on your behalf. The simplest approach would likely be having a column in your database that allows you to mark when the event was published, and then have your function flow:

    1. Write to the database with the "event published" flag unset; on failure abort.
    2. Publish the event; on failure abort. (the data stays in written)
    3. Write to the database to set the "event published" flag.

    You'd need a second Function running on a timer that could scan your database for rows older than XX minutes ago that still need an event, which then do steps 2 and 3 from your initial flow. In failure scenarios, you will have some potential latency between the data being written and the event published or may see duplicate events. (Event Hubs has an at least once guarantee, so you'll need to be able to handle duplicates regardless.)