Search code examples
javacassandralagom

Lagom persistentEntityRegistry.register doesn't work


I'm trying to configure my first Entity with Cassandra and Lagom.

I'm asking about how Lagom save the entity in Cassandra?

For example, this is my UserServiceImpl class:

public class UserServiceImpl implements UserService {

    private final PersistentEntityRegistry persistentEntityRegistry;

    @Inject
    public UserServiceImpl(PersistentEntityRegistry persistentEntityRegistry) {
        this.persistentEntityRegistry = persistentEntityRegistry;
        persistentEntityRegistry.register(UserEntity.class);
    }

    @Override
    public ServiceCall<NotUsed, String> getUserInfo(String id) {
        return (request) -> {
            // Look up the user entity for the given ID.
            PersistentEntityRef<UserCommand> ref = persistentEntityRegistry.refFor(UserEntity.class, id);
            // Ask the entity the Hello command.
            return ref.ask(new UserCommand.Hello(id, Optional.empty()));
        };
    }
}

So by executing:

persistentEntityRegistry.register(UserEntity.class);

Should I have a user table into Cassandra? because I only have:

enter image description here

I can't understand should I create the table user before starting my Lagom project or we only save the event?

Any help, please


Solution

  • Lagom does not require a table for the entity because it's not based on an object-relational mapping or anything similar. Its persistence layer is based on the principles of Event Sourcing.

    The messages table will contain the events that are emitted by your entity and its state is recovered each time you bring it back into memory.

    Basically, the events are being saved in json format in the messages table.

    We also have the concept of snapshots. The entity state may be saved as a snapshot (also using json). This happens after every 100 events and it's a small optimization to avoid to replay the events from scratch each time.

    I'll try to explain shortly the whole mechanism.

    A command is sent to an entity and events are persisted (that happens in the command handler). After the events are persisted, they are applied to the entity to mutate it (that happens on the event handler).

    You restart the service and send a new command to that same entity. At that point the entity is not in memory, so Lagom will bring it into memory but before handling that new command it will replay the history of events for this entity in order to bring it back to the state it had when the system went down.

    After that, the command is applied and new events are persisted.

    After 100 events a snapshot of the entity will be saved and next time, when it's needed to replay that same entity, we load first the snapshot and then we apply the events that took place after the snapshot. So, in that case we don't need to replay the whole event history.