lagom / activator-lagom-cargotracker

Cargo tracker sample application for Lagom
Apache License 2.0
102 stars 102 forks source link

Event Sourcing Question #1

Closed jlhood closed 7 years ago

jlhood commented 8 years ago

I really enjoyed your book, Developing Reactive Microservices and appreciated the Cargo tracker example code. I had a question, specifically around event sourcing. My understanding is event sourcing provides a clean solution to the classic problem of ensuring that both state updates and events are sent in a single operation. It solves this problem by essentially removing a separate state update and instead having the events be the authority and state can be built from those events later. This article provides a great description:

https://www.nginx.com/blog/event-driven-data-management-microservices/

Given that, I'm confused by the cargo tracker example code. When I look at RegistrationServiceImpl.register(), a Cargo immutable is published to the "topic" topic, then the RegisterCargo command is sent to the PersistentEntity (CargoEntity).

https://github.com/lagom/activator-lagom-cargotracker/blob/master/registration-impl/src/main/java/sample/cargotracker/registration/impl/RegistrationServiceImpl.java#L65-L71

I have two issues/questions about this.

First, the Cargo is published to the topic before the CargoEntity has been called, which determines whether it's a valid command or not, given the user inputs. This causes invalid Cargo objects to be published to the topic, which seems like undesirable behavior to me. This is a minor point, but I wanted to bring it up to make sure I'm not missing something here.

Second, and more importantly, the topic appears to be the mechanism for sending events to other services (or in this case the same service). Is that correct or should I be viewing topics as a way to send messages between actors within the same microservice? If topics are the way to send events to other services, then don't you have the same problem of sending a command to the PersistentEntity and then separately publishing an event to a topic? If the process crashes between these operations, you would persist the event generated by the command, but the Cargo topic publish would be lost. Does Lagom somehow ensure both of these happen, maybe by retrying the request?

If topics are not the mechanism used to publish events to other services, then how would you do this in lagom? Are you supposed to publish events to a topic as a side effect in your event handler function in the persistent entity class, i.e., in here?

https://github.com/lagom/activator-lagom-cargotracker/blob/master/registration-impl/src/main/java/sample/cargotracker/registration/impl/CargoEntity.java#L70-L75

I'd appreciate any clarifications you can provide. I'm very excited about Lagom's potential and am trying to understand how I can leverage it.

jroper commented 7 years ago

Hi @jlhood, we've only just noticed this so probably you're not interested in an answer anymore, but the issue tracker is not a good place to ask questions. In future, please ask questions on the mailing list or in the gitter channel. Thanks!