Well, we all have used google docs.
Ever wondered, how it allows multiple people to edit the doc, that too in almost real-time while keeping a consistent state?
1. This is a common problem with any collaborative platform. There are multiple people — reading/writing and they might be editing at the same location of the document. How do we solve this issue?
2. One simple thing that comes into mind, is to create a lock on the document when a person is editing and prevent others from making any change during this time. This is a common approach, but this results in a loss of performance and the platform is not completely collaborative.
3. Another approach could be to maintain logs of changes made by each person, along with the timestamp, and use those to build the final state of the document. But, what happens if two people perform the operation at the exactly same time?
4. The problem is similar to resolving conflicts during a git merge, but this has to be done automatically without user intervention.
5. Google uses a collaborative protocol to provide near real-time collaboration on google docs. All the clients(our browsers) interact with the Google docs server and perform the required edits.
6. The clients keep a state of 4 things:1) changes present locally but not sent to the server. 2) the most recent version of the doc sent by the server 3) changes sent to the server but not acknowledged by the server 4) the current version of the doc visible to the user.
7. The servers keep a state of 3 things: 1)current state of doc for the server 2) changes it has received but not acknowledged 3) logs of changes applied.
8) Whenever a client receives an edit from the server, it performs changes on the current state of the document by applying operational transform on the changes which the client has not sent to the server and the ones not acknowledged by the server.
To understand in detail:
You can also visualize how the algorithm works here: