title: How to keep data consistent when sync documents with cloud firestore triggers?
body: <p>I have a document <strong>MAIN</strong> which holds an array of objects. On every change of this array i want to update related documents. So in the example below If I add something to the targets of <strong>MAIN</strong> i want to grab all documents where the field &quot;parent&quot; holds a reference to <strong>MAIN</strong> and then update their targets accordingly. I wanted to do this with cloud functions so the client does not have to care about updating all related documents himself. But as stated in the <a href="https://firebase.google.com/docs/functions/firestore-events#limitations" rel="nofollow noreferrer">docs</a> cloud-funcion triggers do not guarantee order. So if f.e. a user adds a new object to the targets of <strong>MAIN</strong> and then removes it, cloud trigger would maybe receive the remove event before the add event and thus <strong>RELATED</strong> documents would be left with inconsistent data. As stated by Doug Stevenson in this stackoverflow <a href="https://stackoverflow.com/questions/57859663/do-firebase-firestore-transactions-create-internal-queues">post</a> this could happen, even when using transactions. Am I right so far?</p>
  <pre><code>  const MAIN = {
      ID:&quot;MAIN&quot;
      targets:[{name: &quot;House&quot;}, {name:&quot;Car&quot;}]
    }
  
    const RELATED_1 = {
      parent: &quot;MAIN&quot;,
      targets:[{name: &quot;House&quot;}, {name:&quot;Car&quot;}]
    }
  
    const RELATED_2 = {
      parent: &quot;MAIN&quot;,
      targets:[{name: &quot;House&quot;}, {name:&quot;Car&quot;}]
    }
  </code></pre>
  <p>If yes, I was thinking about adding a servertimestamp to object <strong>MAIN</strong> whenever I modify the Document. I would use this timestamp to only update <strong>RELATED</strong> Documents if their timestamp is smaller then the one of the parent. If yes I update the array and set the timestamp of the parent.</p>
  <pre><code>  const MAIN = {
      ID:&quot;MAIN&quot;,
      targets:[{name: &quot;House&quot;}, {name:&quot;Car&quot;}],
      modifiedAt: 11.04.2022 10:25:33:233
    }
  
    const RELATED_1 = {
      parent: &quot;MAIN&quot;,
      lastSync: 11.04.2022 10:25:33:233,
      targets:[{name: &quot;House&quot;}, {name:&quot;Car&quot;}]
    }
    
    
    const RELATED_1 = {
      parent: &quot;MAIN&quot;,
      lastSync: 11.04.2022 10:25:33:233,
      targets:[{name: &quot;House&quot;}, {name:&quot;Car&quot;}]
    }
  </code></pre>
  <p>Would that work? Or how could one sync denormalized data with cloud functions and keep data consistent? Is this even possible?</p>