samedi 9 avril 2022

Transactions for long running multi-task activities on a message broker

I like to hand off the an order fulfillment task to a message broker but ensure that the fulfillment is transaction controlled on MongoDB using python. That is to say:

  • User should get fast feedback after placing the order
  • Fulfillment should run offline for a few minutes because multiple tasks need to complete
  • If fulfillment fails no order should be billed to user
  • Reduce DB writes for the order receive procedure

What is the best approach for such a scenario?

Option 1: Single transaction to either insert or not insert order

Receive API request
START transaction
Insert new order to Orders
Send task to message broker
Send API order received, fulfillment started
Wait for message broker to complete task
Send API fulfillment completed
END transaction 
Bill all orders of the day

Option 2: Two transactions using a dedicated order state field

Receive API request
START transaction
Insert order with status received
Send API order received
END transaction

START transaction
Send task to message broker
Send API fulfillment started
Wait for message broker to complete task
Update order with status fulfilled
Send API fulfillment completed
END transaction 
Bill all orders of the day in status completed

Or perhaps there is an entirely different approach?

Aucun commentaire:

Enregistrer un commentaire