Skip to content

Latest commit

 

History

History
48 lines (39 loc) · 2.45 KB

Publishing_Events_Using_Local_Transactions.md

File metadata and controls

48 lines (39 loc) · 2.45 KB

Publishing Events Using Local Transactions

Motivation

  • How to update the database and publish a message (event) atomically (either all success, or all fail).
  • Cannot use distributed transaction pattens to solve this problem.
    • Many modern message brokers don’t support distributed transactions.

Solution

Concepts

  • Uses the event table to store the events need to be published.
  • Bundles the operation of inserting new record into a business entity table and the operation of inserting a new event to the event table as a single local database transaction.
  • Event publisher publishes events by polling the event table periodically.

Implementation

Pros & Cons

Pros

  • The atomicity of updating the database and publishing a message (event) is guaranteed.

Cons

  • Frequently polling the database can be expensive.
  • Tricky to publish events in order.
  • Not all NoSQL databases support this pattern.

Consideration

Topic Consideration Possible Solution Options
Error handling The event publisher may publish a event more than once (For example, crash after publishing an event but before marking the corresponding event record as published).
  • Consider to let the event consumer be idempotent by recording the IDs of the events that it has already processed.
  • When To Use

    References