We have a design challenge here for a project we are working on and I wonder if folks from the community can provide some guidance:
Our product is built in a microservices structure. So we have different services caring for different aspects of the solution. Each service has its own database. Both databases run on Aurora PostgreSQL
There is one service called users which records all users, teams and the relationships between users and teams.
There is also another service which handles a gamification concern. In this second service, we have the idea of "games" and those games can be associated with different teams by their team_ids. In the gamification database we also have a points table which associates a user with some point they may have won for executing whatever action within the system.
So far so good. But here is where it gets tricky:
Imagine we are trying to list a leaderboard with all users from that game and their current points and we'd like to also include users with zero points in this list.
If all tables were together in the same database, this would be easy. We would just inner join teams and users, then left join with points and we would have what we need. A list of users that belong to those teams, together with a sum of their points.
Since these info are not on the same database, this is a little more complicated. So our team has came up with a few different ideas of how to solve for this:
Use an events architecture: every time a user joins or gets removed from a team, we would fire an event and the gamification microservice would use those events to keep its own copy of the user/team relationships.
Pros: seems like a clean way to handle this within microservices infrastructure
Cons: we'll create duplicate data, could lead to sync issues if we fail to process eventsMake an API call to users service every time we are building the leaderboard, grab all users from those teams and join that data with the points data in application runtime.
Pros: keep everything where it belongs
Cons: could be resource-expensive to run constantlyUse something like federated queries to query one database from the other
Pros: keep everything where it belongs
Cons: will only work for PSQL and if we have other services in other databases, this solution wouldn't work
In your experience, what are correct ways to handle this challenge? Or which are the angles we may not be exploring correctly here?
Thanks very much