Using FIFO Queues to Order Messages in Mule 4

Objective

Very often we come across requirements to maintain the order of messages that flow through the integration layer. For example, if Source System A updates a record, it needs to be updated accordingly in Target System B, but when there are multiple consecutive updates in System A, those need to be updated in the Target System B in the correct order. In a distributed multi-threaded environment, there is always a chance of a race condition where an older record may overwrite a newer record.

Scenario

If the status of an entity in Source System A, say #1234 is updated as “In-Progress” and subsequently updated again to “Complete”, the Source System A invokes an API implemented in Mule every time there is a status update to sync the record with Target System B. Most of the time, we would prefer implementing an asynchronous flow with queues in between to sync the records between the two systems. If the API invokes a publisher flow which publishes the messages into a Queue (say in Anypoint MQ) and there is a subscriber flow picks up the messages and updates the Target System B, then we can not guarantee the order of messages. Considering this scenario, there is always a chance that the status of “Complete” gets overwritten by “In-Progress” in the Target System B. The below diagram explains the scenario.