As per one of the discussions on Lagom conceptually, I read in this link
The entities are not persisted anywhere - that's the point of event sourcing, you just store the events, which can be implemented very simply, be easily distributed, and done with very high performance since it's just an append operation. When an entity needs to be loaded, the events for that entity are loaded, and the event handlers that you've declared to handle the events then process each event to produce the current entity state.
My question is how does lagom identifies which events belong to which entity. Lets take an example: I have a rest service that performs CRUD operation for users. For event sourcing, I create a UserEntity class where I define different command handlers for CUD operations. Now, for me when the application is running, lagom generates an instance of UserEntity class. Now if I fire create request for "Alice" and create request for "Bob" followed by update request for "Alice", here there are two entities, Alice and Bob. So how does lagom identifies for the entity "Alice", there are two events, created and updated whereas for Bob, there is only one event created. And how does it bind events to entities?
the misunderstanding on your description is that lagom doesn't create a UserEntity. The correct sentence would be: "Lagom creates one instance of UserEntity for each user in my system".
The trick is that anytime you want to send an event to a persistent entity instance you have to request the instance by type and ID:
PersistentEntityRegistry persistentEntities = ...;
PersistentEntityRef<UserCommand> refAlice =
persistentEntities.refFor(UserEntity.class, "Alice");
In the snippet above Lagom guarantees that all commands you send to refAlice
will only be processed by that instance.
Then, any event emitted by refAlice
will be bound to the class and the ID so they can't be mixed up with events emitted by other instances.