Dual Write Problem

sequenceDiagram
    participant Req
    participant Microservice
    participant Database
    participant Kafka
    Req->>Microservice: 
    Microservice-->>Database: Save to DB
    Microservice-->>Kafka: Send Event

Transactional Outbox Pattern

  • Whenever we update database, we can also update outbox table in the same transaction
  • Have a separate process that asynchronously monitors the table
    • whenever it sees a new event, it can deliver it to Kafka
    • Once event is successfully delivered, it might be removed from outbox table
    • generally we have another thread to monitor the table in the same microservice
  • Delivery guarantee: at least once
    • We may retry to keep the Kafka in sync with outbox table, and retry sending events
    • Downstream system should be prepared to handle any duplicates