Search code examples
javalagom

Can a Lagom service have only one entity?


Can a single Lagom service have only one entity? I could not find an example which use two entity types in a single service.


Solution

  • A single Lagom service can have as many entities as you please, there is no limit, just invoke PersistentEntityRegistry.register for each one.

    That said, there's a reason that there are no examples which have two entity types, and that is that a general principle behind microservices is that microservices should do one thing (and do it well). When a microservice has more than one entity, then it's likely that the microservice is actually doing more than one thing. Aggregates (entities) are usually a nice clean boundary to draw around a microservice.

    But that said, separating each entity out into a separate microservice incurs additional costs in complexity and performance. There's a trade off, and it's not an area that I believe is worthwhile being so black and white in. If you have two very closely related entities, and you are sure that the services for these entities are only ever going to be developed by one team, and will always have the same scaling and failure requirements, then it probably won't hurt to put them in the same service. Just remember that it's usually harder to separate services after the fact than it is to do up front - and also, by putting hard service boundaries between different concerns, you often end up with a better design, because you have to put more effort into thinking about the interfaces between the components when they are in different services compared to when they are in the same service.