Lets assume we have a microservice "A". We are now scaling it horizontally, meaning we have 3 instances of "A" working on the same db instance (and schemea, in general assume the 3 "A" instances might do Read and Writes on the same data).
Now I'll demonstrate the question with some pseudo code, we have the following update function in "A":
Product p = getProdFromDb(); // for example selecting
// from Postgresql db
p.updateInnerData(); // synch method that updates
// something inside the p model that takes significant
// amount of time
p.updateInDb(); // for example update back in postgresql
The problem here is that other "A" instances might change product p while we are updating it here (not in this function, but asdume there such other functions that change products in "A"). One solution that I know of is using a lock on the db (for example using "Select ... for Update") but it creates a performance bottleneck in this function. I would like to see better solutions that solves this problem without this bottleneck, real examples in Java (or JS) would be very helpful.
Edit: assume partioning is not an option