Document Versioning for Event-Based Microservices Application

21 views Asked by At

I am developing a microservices application with NATS Streaming. I am tackling concurrency issues by adding a version property to my database documents and incrementing them by one every time they are updated.

But I have an edge case here. Let's say there are two types of update routes to a movie ticket document. The first route updates the title and price of the ticket (which publishes an event) and the other marks the ticket as archived (doesn't publish an event). Both route handlers increment the ticket version by 1.

Erroneous scenario:

  1. The user creates a ticket and a version number of 1 is assigned.
  2. The user archives the ticket and now its version is 2. (No events published so far)
  3. The user updates the ticket's title and price and the version is set to 3. (TicketUpdated event published)
  4. Orders service receives the event but in its DB, the ticket's version is 1, so the event is rejected.

How would you fix this issue?

Thank you!

0

There are 0 answers