MongoDB: Handling Concurrent Updates from Asynchronous Events

Using timestamps for every property to deal with concurrent updates to the same properties when new data is coming in asynchronously and out of order

Robert Dempsey
Level Up Coding
Published in
3 min readMar 10, 2020

--

Photo by noor Younis on Unsplash

In this article MongoDB: Maintain Data Integrity with Atomic Operations we got to know the $set, $setOnInsert, $push, $pull and $inc operators and learned why they are essential for making sure your data is accurate when dealing with parallel updates to single documents.

The above operators and other atomic operators like it are sufficient for handling such a scenario, but only when the properties we are setting are different for each update, as seen in the following:

But these operators alone are not enough for my team’s requirements, where we could potentially be processing updates to the same property in parallel and out of order as they’ve come from the source. This could lead to data that originated earlier at the source overwriting updates we’ve performed with later data.

--

--