How would you go about making sure that "events" sent as messages to any sort of broker (Kafka, RabbitMQ etc) from multiple instances of the same service (load balanced) are actually added to the "queue(s)" in a correct order?
So for instance.. Say we have a created an application/service. The service publish the following events/topics as a message to a broker:
- UserCreated
- UserUpdated
- UserRemoved
Now since we have a heavy load on the service, we decide to scale the service to multiple instances.
Some action is taken that ends up creating a UserCreated event, and directly after another action is taken which ends up creating a UserUpdated event.. however both the events are "created" by different instances of our service. Now lets pretend that the service creating the "UserCreated" event is for some reason running slower, so the UserUpdated event is actually added to the queue before the UserCreated event.. is there anyway to prevent this? Any patterns or something that could read up on to understand how to make sure that the order of messages/events actually ends up correctly in the queue in a that has multiple publishers?
Please notice that the above example is just that.. an example, Im fully aware that we could probably "design" our messages in the example above so that UserUpdated sends the fully updated user, and if it doesnt exist we create it.. and if the CreatedUser with the same ID/Username occurs after the UpdatedUser we could just discard that event, but thats not the problem I need to solve. Im just wondering of how to solve the issue of making sure that events are handled by a consumer in the correct order (which I assume required the messages to be published in a correct order?).