Check existing data in other domain

I’m new in CQRS and Axon and I have probably stupid question but I have to ask…
Is possible to check if data exists between different microservices/domains using Axon?

Domain1: User management
Domain2: Account management
When I’m creating new account in D2, I want to add reference to user from D1 which owns this account. I do this by sending creating command with user’s id.
But I would like to check if user with this id really exists in D1… Is possible to check this with Axon (ideally with gRPC usage) somehow? Or I have to use some other technique/client?

Thank you for answer

I would create a query in D1, say UserExists with a query handler returning a boolean. Then in D2, I would simply call something like:

void checkUser(UserId user, QueryGateway queryGateway) {
   boolean exists = queryGateway.send(new UserExists(user), Boolean.class).join();
   if (exists) {
      // ... do something creative
   } else {
      // probably throw an exception

In this example, both D1 and D2 must “share” the query class (there are other topic in the forum explaining how to do it). Then your problem can be narrowed down to the implementation of the query handler in D1.

The query handler can be implemented:

  1. using a projection such as JPA entity/repository (here you need to handle events in D1 and update your entity/repository accordingly)
  2. using Axon repository to load the user aggregate

Option (1) responds to the query much faster as it is a simple SQL select query. However, you have to implement the projection by yourself.
Option (2) will respond slower (depending on the number of events) but your query handler could be as trivial as:

boolean handle(UserExists query, Repository<UserAggregate> repository) {
    try {
        return !repository.load(query.getUserId()).isDeleted();
    } catch (AggregateNotFoundException e) {
        return false;

Also note, that option (1) is recommended CQRS approach.

1 Like