title: How to store json-patch operations in redis queue and guarantee their consistency?
body: <p>I have collaborative web application that handles JSON-objects like the following:</p>
  <pre><code>var post = {
    id: 123,
    title: 'Sterling Archer',    
    comments: [
      {text: 'Comment text', tags: ['tag1', 'tag2', 'tag3']},
      {text: 'Comment test', tags: ['tag2', 'tag5']}
    ]  
  };
  </code></pre>
  <p>My approach is using <a href="https://www.rfc-editor.org/rfc/rfc6902" rel="nofollow noreferrer">rfc6902 (JSONPatch) specification</a> with <a href="https://github.com/Starcounter-Jack/JSON-Patch#user-content-usage" rel="nofollow noreferrer">jsonpatch</a> library for patching JSON document. All such documents store in MongoDB database and as you know the last one very slow for frequent writes.</p>
  <p>To get more speed and highload application I use redis as queue for a patch operations like the following:</p>
  <pre><code>{ &quot;op&quot;: &quot;add&quot;, &quot;path&quot;: &quot;/comments/2&quot;, &quot;value&quot;:  {text: 'Comment test3', tags: ['tag4']}&quot; }
  </code></pre>
  <p>I just store all such patch operations in queue and at midnight run cron script to get all patches and construct full document and update it in MongoDB database.</p>
  <p>I don't understand yet what should I do in case corrupted patch like:</p>
  <pre><code>{ &quot;op&quot;: &quot;add&quot;, &quot;path&quot;: &quot;/comments/0/tags/5&quot;, &quot;value&quot;: 'tag4'}
  </code></pre>
  <p>The patch above don't gets applied to document above because <code>tags</code> array has length only 3 (according official docs <a href="https://www.rfc-editor.org/rfc/rfc6902#page-5" rel="nofollow noreferrer">https://www.rfc-editor.org/rfc/rfc6902#page-5</a>)</p>
  <pre><code> The specified index MUST NOT be greater than the number of elements in the array.
  </code></pre>
  <p>So when user is online he don't get any errors because his patch operations get stored in redis queue but next day he get broken document due broken patch that don't got applied in cron script.</p>
  <p>So my question if how could I guarantee that all patches that stored in redis queue is correct and don't corrupts primary document?</p>