0

I'm still quite new to the Lagom framework and I'm seeking the best way to fetch all users of my distributed application.

In my app, I have a user service working on Cassandra DB. So to select all users I followed the Lagom Online Auction example application, in UserServiceImpl.java:

//...
@Override
public ServiceCall<NotUsed, PSequence<User>> getUsers() {
    // Note this should never make production....
    return req -> currentIdsQuery.currentPersistenceIds()
            .filter(id -> id.startsWith("UserEntity"))
            .mapAsync(4, id ->
                entityRef(id.substring(10))
                    .ask(UserCommand.GetUser.INSTANCE))
            .filter(Optional::isPresent)
            .map(Optional::get)
            .runWith(Sink.seq(), mat)
            .thenApply(TreePVector::from);
}
//...

I also checked this Question on SO

It is clear that the idea of the read-side works with one instance of user service. But, in the production environment, I should have many instances of the same service may run on multiple nodes. In these case, I think that each service should have its own DB.

So my question is: Does the read-side logic can cover only all users per service instance or it should cover all users of all service instances?

Also, is there any other solution to get all users of all service instances?

Many thanks.

Imen
  • 161
  • 2
  • 14

1 Answers1

2

If you have multiple instances of the same service, they should share a single database.

If you have multiple different services, they should each have their own database.

If you have data that is owned by one service, that another service must query, you can use Lagom's Message Broker API to publish events from the owning service to Kafka. Then, other services can subscribe to the Kafka topic to replicate the data into their own read-side database.

Tim Moore
  • 8,958
  • 2
  • 23
  • 34
  • Thank you Tim, According to Lagom docs "If instances of a service need to know about each other, they must join the same cluster" and you said that "If you have multiple instances of the same service, they should share a single database". Does that mean that Cassandra DB of the same service instances must be deployed in the same cluster of these instances or it could be deployed in another cluster? – Imen Jan 11 '18 at 16:25
  • The first statement is referring to the [Akka cluster](https://www.lagomframework.com/documentation/1.4.x/java/Cluster.html) formed by the Lagom services. This is independent from the Cassandra cluster. If you are asking whether they need to be deployed to the same physical servers, the answer is no. – Tim Moore Jan 12 '18 at 00:44