Hello
There are different ways in which the stock level of a product can be changed:
- A device sets the total stock amount
- A device increases or decreases the stock amount
- A device deletes a record of a product
Conflicts can arise from the interaction of these three actions.
Rule #1 is to avoid deletions. Instead, you should mark a product as deleted. This way, you can still track the history of the product. If you really need to delete a product, you should make sure that all devices have received the deletion before you actually delete the product.
Rule #2 is to avoid overwriting data. Instead, you should merge data (only do addition and subtraction).
Example:
When an off-line device needs to set the total stock amount of a product, instead of defining the total amount using a command like this:
UPDATE products SET stock_level = 300 WHERE product_id = 123
It should first subtract the current stock level and then add the new amount, like this:
UPDATE products SET stock_level = stock_level - 200 + 300 WHERE product_id = 123
On this way, the device can work off-line and merge its changes with those from other devices when it comes back on-line.
Note that this approach only works if the device is the only one that is changing the stock level of the product.
To simplify the process: only allow 2 types of changes on stock levels:
- Addition, when products are received
- Subtraction, when products are sold