Design Strategies – Managing Multiple Clients Editing Same Data Simultaneously

Architecturedesignprogramming practices

How can I deal with situations when multiple clients might edit same object at the same time?

For example, I have a web app with admin console, which lets you edit profile data. Several users want to open the same profile and edit it.

If I just let the things go as they do, I`ll get "the last who saves wins" situation and all the other users will lose their changes.

Blocking object for edit on client, when someone enters edit page, seems relatively easy and overall acceptable for simple objects. However, for complex objects, when different users may want to change different parts of object, merging would be much better. I've read a paper on Conflict-free Replicated Data Types and it seems to be pretty complex and does not cover conflict situations.

Are there any other ways to merge objects? Or maybe there are totally different strategies to deal with the situation?

Best Answer

There is a technique that can be employed to handle this. There are some assumptions first. You (or your team) must to be in control of all software that can update the data. Based upon this, here is what you can do:

  1. Do not lock the data upon original download and while the user is editing it. The reason for this is because locks in general are very bad for performance - for example what happens if a user decides to take a break while an exclusive lock is held?

  2. When the user tries to save the data, then acquire an exclusive lock and download the data again. Compare the original downloaded data with the data just downloaded. If the data includes a timestamp for the last update, then just compare the timestamps. You might consider adding a last-update timestamp column for this purpose. If the data is the same, which indicates that no intervening updates have taken place, then update the database and release the lock. If the data is not the same, then release the lock and show the updated data and prompt the user to enter his updates again. You can save his original updates in another tab or window to make this easier. Another possibility is to have the software do a merge and highlight any conflicts.

Related Topic