0
votes

In a classical microservice architecture, you have relevant domain events published on some messaging system which allows other parts of the system to react.

Now imagine you have three microservices: Customers, Orders and Recommendation. The Recommendation microservice needs information from Customers and Orders to provide its functionality, such as the list of all customers and all the orders, which is going to be analyzed from some machine learning algorithm. Now, you need to have the state of Customers "join" Orders on the Recommandation microservice:

  1. You have the Recommandation microservice listen to domain events published by Customers and Orders and built its own state. This leads to logic duplication since you probably have that same logic inside Customers and Orders already

  2. On each relevant domain message from Customers and Orders, you just go to them and ask the state of a specific customer or order. This works fine, however if you have N services rather than just one which needs to build a materialized view, you will cause a big load on Customers and Orders

  3. You get Customers and Orders themselves publish "heavy-weight" events (not domain events) that allows any other microservice to build a materialized view without processing domain events. This allows you both a) not to duplicate the logic b) not to keep asking the same information

Has pattern n.3 some drawbacks we couldn't figure out and if not, how do you implement it in Lagom?

1

1 Answers

3
votes

I will try to explain a few more bits in the hope to give you some more perspective on that matter and how you can achieve it in a reliable way in Lagom.

We have a few concepts that we must keep in mind. The most important one which is the source of all is Event Sourcing itself. Event Sourcing means that any State in the system has its source in Events.

The first State that we will deal with is the State of the PersistentEntity. This State is prominent because, together with the Command and Event Handler, it defines the consistency boundary of your model.

But there other States in the system. Actually, we can create as much as we want because we have the Event Journal. A read-model is also a State and it’s also generated from the events.

There are many reasons why you shouldn’t publish the State of the PersistentEntity to other systems. The first one being a matter of avoiding coupling. You don’t want your data to leak to other services. That’s all about having an anti-corruption layer (ACL).

So, from here we could say: before publishing Order and Customer to Recommendation Service, I will transform it to OrderView and CustomerView (ACL 101).

The question now is when will you do it? If you try to publish it in Kafka after you have handled a command, you don’t have any guarantee that the State will be published. There are no XA transactions between the event journal and the Kafka topic. So, there is a chance that the events are persisted, but for some reason, the State is not published in Kafka.

If you want data to get out of a service in a reliable way and without creating coupling between services, you have the following options:

Use the broker API and publish the events to a topic. You should not publish the events as they are, but transform them into the format of your external API (ACL). Use a read-side processor to generate a view of it, again the external API format you want to make available. If you want, you can publish that ViewState to a topic so other services can consume it directly.

That said, there is nothing wrong in publishing something in a topic that is not a real event, but some derived State. The problem is how you can guarantee that it is effectively published. Doing that from inside the PersistentEntity is risky because you have at-most-once semantics. The most reliable way of doing it is a read-side process that gives you at-least-once semantics.

Further comments inline...

Listen to domain events from customer and orders and rebuild the state in the recommandation service. This is a horrible idea because you would need to duplicate the logic that handles events across different bounded context

That's not a horrible idea. That's how you make your services independent from each other. The logic that you will need to implement to consume the events are not the same. As you said, it's a different bounded context, as such it only gets what it needs.

Leaking the State from a BC to another is more problematic for the reasons I mentioned above (anti-corruption layer).

To achieve decoupling you do need more coding and there is nothing wrong with that. At the end of the day, the reason for building microservices is to avoid coupling and be able to let the services evolve and scale without interfering with each other. There is a price to pay for that and the price is to write more code. You need to evaluate the thread-offs.

You can consume your own events, produce an OrderView and CustomerView and publish into Kafka, but that's the same as consuming the events directly on the Recommendation Service.

Note that you also need to store OrderView and CustomerView somewhere in the Recommendation Service. So you end up storing it three times. On the original service (view table), in Kafka and in the Recommendation Services.

That's why publishing events in a topic is the best option to propagate data between services.

Every time we receive a domain event from customers or orders, go to them and ask them the state. This is horrible because if you have more than one microservice that needs their state, you will end up producing load on customers and orders

That is indeed a horrible idea because you will make the Recommendation Service be dependent on the other two services. If Order or Customer is down, the Recommendation will be down as well. That's what a broker helps to solve.

Have customers and orders not only publish events but also state and having all the services that need to build materialized views listen the state they need How do you apply the last pattern with Lagom? We found no way to listen to state changes, just to events. One solution we considered implied publishing with pubSub the state in the onEvent handler of a persistent entity but I am not sure this is the right place to make it happen.

Using pubSub in the onEvent handler is the worst solution of all. For the following reasons:

  1. pubSub has at-most-once sematincs (see comments above)

  2. Event handlers are called many times. Whenever you re-hydrate an Entity, the events are replayed and the the event handlers will be used for that. Which mean that you will re-publish the state each time. Actually, you would solve the at-most-once pubSub problem, but not the way you might expect/desire.

You could use the afterPersist callback for that, but that's not reliable neither because pubSub is at-most-once.

PubSub inside a PersistentEntity should not be used for something that you need to be reliable. It's a best-effort capability, that's all.