Synchronization with integer values


We have a scenario where we need to update an inventory. In this scenario, our inventory data is synchronized with Couchbase Mobile.

If we were to update the same balance of an item in our inventory, would this result in a merge conflict?
Let’s say a coworker takes 5 items of an item category, and another coworker takes 2. We would like to have the balance adjusted to the now 7 fewer items inventory, but fear a merge conflict that will cause only one inventory withdrawal to be registered.

Do you have any recommended solution for this scenario? In our case, the inventory balance is stored as an integer.
A similar scenario would be an airlines company with their seat selection. How do we deal with such cases in an offline synchronization environment?


If the balance is stored in a document, and both clients update that document to decrement the balance, then you will definitely run into a conflict in this scenario.

In CBL 1.x you can look for and resolve the conflict. The safest way to do this is to store the balance as a [Conflict Resistant Positive-Negative Counter](, not a simple integer. That makes it possible to do a two-way merge between the local and server version of the document.

CBL 2.x does not have conflict resolution yet. The only approach that would work is to have each user use a separate document to store how many items they’ve taken. The inventory total can then be computed by a query.

1 Like

Thank you so much for the great answer, @jens !

We are using CBL 2.x.

I have a second question on the same subject; our users can make loans on items stored in the inventory,
Our previous approach was to update the current user and the new properties (such as date and quantity of the loan) by a REST API. We are trying to migrate to CB to avoid situations where bad reception prevents our user from making their loans.

My question:
If all the users have a revision of the “inventory item” document, and makes a loan on the same item, how will changes be tracked or merged if there’s no conflict resolution? Do you have a recomended approach for this case as well?

Your application appears to be one that absolutely requires conflict resolution, since you obviously can’t have two people checking our the same item. That’s not going to be feasible with the current version of Couchbase Lite, unfortunately. We do plan to add explicit conflict resolution in a future version, and our product manager @priya.rajagopal is soliciting input on what use cases people need it for.