Concurrent or distributed systems face an interesting challenge of correctly handling writes that write to the same object, table, data-structure or what you may have, at the same time. Usually concurrent writes occur when two actors read a value, modify the value based on what was read and then update the value. To understand the problem consider two applications that attempt to update a value count in the database. Initially count is set to 5. Consider the following sequence of events that result in loosing the changes made by one of the users. Both users set out to read the current value of count and increment it by one.

  1. User A reads the count value from the database as 5.

  2. User B reads the count value from the database as 5 too.

  3. User A increments the read value from 5 to 6.

  4. User A updates the count value in the database to 6.

  5. User B is unaware of the actions of user A and increments the value it read by one to 6.

  6. User B overwrites the value written by user A for count in the database to 6 again.

The above sequence is depicted by the image below:

Get hands-on with 1200+ tech skills courses.