Search code examples
javacassandracassandra-3.0lagom

How should I model my Cassandra data for my Lagom service?


I am creating a service using Lagom framework and need some help in the architecture of the application. There is Employee service which contains information about all the employees. Each employee has an address. The models are like these,

class Employee {
      String firstName;
      String lastName;
      String email;
      Address address;
}

class Address {
    String apt;
    String street;
    String city;
    String state;
    String pin;
}

Right now I am creating a single service for employee and thinking of using Cassandra for the database. Should I create a single table for employee which contains a custom user type (UDT) of address or create a separate service for address and use this service in the employee service. Also can someone point me to a Lagom framework example which demonstrates the use of UDT in Cassandra.


Solution

  • The recommended way of persisting data in Lagom is to use event sourcing, described here:

    https://www.lagomframework.com/documentation/1.3.x/java/PersistentEntity.html

    So you don't store your state directly in tables, you store the events that led to that state. For example, you might have an EmployeeAdded event, and an EmployeeAddressChanged event, and so on, depending on what make sense to model in your business use case. These are the things that get persisted, and then when the employee is loaded, Lagom will replay all the persisted events to create the Employee type that you have above.