title: Microservices and Publishing State versus State Changes
body: <p>Let's say I want a system where a service <code>B</code>'s state should be updated according to changes in the state of service <code>A</code>. When <code>A</code>'s state changes it can either publish</p>
  
  <ul>
  <li>its entire state </li>
  <li>only its changes in state</li>
  </ul>
  
  <p>So let's say for example that <code>A</code>'s state at points in time looks like</p>
  
  <pre><code> t0  | [ ]
  ------------------
   t1  | [X,  Y]
  ------------------
   t2  | [X', Y]
  ------------------
   t3  | [X', Y, Z]
  ------------------
   t4  | [X',    Z]
  </code></pre>
  
  <p>It could publish each of those snapshots of its current state (right column) or it could publish only the changes like</p>
  
  <pre><code>-----------------------
   delta(t0, t1)| +X, +Y
  -----------------------
   delta(t1, t2)| X-&gt;X'
  -----------------------
   delta(t2, t3)| +Z
  -----------------------
   delta(t3, t4)| -Y
  </code></pre>
  
  <p>which in theory could create an optimization. But a few things I see that would have to be guaranteed are</p>
  
  <ul>
  <li>Published events are consumed in correct order</li>
  <li>Published events that were "missed" by a service need to be recognized and somehow compensated for</li>
  <li>There is no bug in the delta algorithm</li>
  </ul>
  
  <p>whereas if the entire state is published then </p>
  
  <ul>
  <li>If 2 published events are consumed in the wrong order then publishing a 3rd event corrects everything</li>
  <li>If a published event fails to be consumed then publishing a 3rd corrects everything</li>
  <li>No delta algorithm, so less complexity and compute resource on the publishing service</li>
  </ul>
  
  <p>A few solutions I can think of to make the delta approach a little more robust add a lot of complexity:</p>
  
  <ul>
  <li>Publish both the changes <strong>and</strong> the entire state and a pointer to the previous event. The services consuming the event can figure out whether to process the delta or the whole data set.</li>
  <li>"Cycle" between publishing deltas and entire state. Maybe publish changes 9/10 times and then the entire state the other 1/10 of the time just to help correct things. </li>
  </ul>
  
  <p>Both of those feel weird. I am interested in whether there exists a robust solution for notifying of "additions, removals and modifications" while ensuring eventual consistency in the system.</p>