Search code examples
google-drive-realtime-api

Clarification of non-atomicity of compound operations


This guide page emphasizes that compound operations are "not atomic" and "not transactions", but the example given there, and copied below, doesn't seem to demonstrate that they aren't transactions. If Bob's edits arrive first, then the end result shown there is consistent with the sequence:

  • All Bob's edits were applied.
  • Then all Alice's edits were applied.

Is the issue that Bob's first two edits aren't actually applied, and so, for example, if we undo Alice's edits, we'll end up with just this?:

{
  'address' : 'Anytown, USA' // Bob's address!
}

The section of the guide that I'm referencing follows.


Although edits made in a compound operation are delivered together, they are not atomic. It is possible that, because of conflict resolution, some edits in a compound operation are never delivered. Imagine a collaboration scenario where two editors modify the same collaborative map at the same time. Alice runs this code:

model.beginCompoundOperation();
myCollaborativeMap.set('name', 'Alice');
myCollaborativeMap.set('phone', '555-5309');
model.endCompoundOperation();`

And Bob runs this code:

model.beginCompoundOperation();
myCollaborativeMap.set('name', 'Bob');
myCollaborativeMap.set('phone', '555-0000');
myCollaborativeMap.set('address', 'Anytown, USA');
model.endCompoundOperation();

If Bob's edits arrive at the server first, the map contents will ultimately resolve to:

{
  'name' : 'Alice',          // Alice's name
  'phone' : '555-5309',      // Alice's number
  'address' : 'Anytown, USA' // Bob's address!
}

If Alice's edits arrive at the server first, the map contents will be:

{
  'name' : 'Bob',            // Bob's name
  'phone' : '555-0000',      // Bob's number
  'address' : 'Anytown, USA' // Bob's address
}

Solution

  • Bob's edits are actually applied, so if you undo Alice's change then it should be Bob's change as a result.

    However, its still not transactional in the traditional database ACID sense. The only guarantee that is made is that the changes within a compound operation will be applied together with no other changes intermixed.

    For example, in a typical sql database transaction, if a problem occurs when applying of the changes, all of the changes are rolled back.

    In a compound operation, each change within it is transformed and applied separately. Its possible that due to conflicting changes in the document, one of the changes ends up being nulled out (e.g, a portion of a string that it tries to change has been deleted.) In this case, that portion of the change would seemingly not get applied, but all other changes within the compound operation would still happen.