Reputation: 27
Let's say I want a system where a service B
's state should be updated according to changes in the state of service A
. When A
's state changes it can either publish
So let's say for example that A
's state at points in time looks like
t0 | [ ]
------------------
t1 | [X, Y]
------------------
t2 | [X', Y]
------------------
t3 | [X', Y, Z]
------------------
t4 | [X', Z]
It could publish each of those snapshots of its current state (right column) or it could publish only the changes like
-----------------------
delta(t0, t1)| +X, +Y
-----------------------
delta(t1, t2)| X->X'
-----------------------
delta(t2, t3)| +Z
-----------------------
delta(t3, t4)| -Y
which in theory could create an optimization. But a few things I see that would have to be guaranteed are
whereas if the entire state is published then
A few solutions I can think of to make the delta approach a little more robust add a lot of complexity:
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.
Upvotes: 1
Views: 362
Reputation: 17144
A third option is for the published events to contain no state information, and the event consumer is responsible to pull the state from the producer. The event may contain a callback to the producer and/or an ID for the data that has changed.
Of course the least complex and least efficient solution is for the consumer to blindly pull data from the producer on a schedule.
Upvotes: 1
Reputation: 1794
This is a really good question. The semantics of publishing deltas is essentially what is coined the event driven architecture if you want to do further reading.
The main advantage that events (deltas, in your definition) give you is transparency. Events are small and confined as they define something that happened to your system and are traceable. If there is another behavior you need to capture, you can add an event type. While with state changes, it lacks the transparency as to what happened; you'll probably have to dig back to all the different states and figure what had happened and if they're out of order, it may be difficult to reason about.
Also to the following points,
There is no bug in the delta algorithm
I don't think its hard to assume. The implementation would be rather easy because you'll be checking the event type and operating on it so the processing in the branch should be rather small to handle.
If a published event fails to be consumed then publishing a 3rd corrects everything
What if the service is responsible for the state change has a bug and updates the record to an invalid state? Publishing the same 3rd state would reflect a consistent bug in downstream systems that depend on the state change. Using the events here decouples these two semantics, essentially state and what happened.
Upvotes: 1