0
votes

I'm trying to get familiar with CQRS and microservices architecture, I understand that CQRS consists of separating read models from domain models and you have a particular service with it database for every read model to optimize the query side.

But as someone who's only used to a monolithic architecture I don't understand how you deal with relational data if they live in different services; let’s say for example I have a users microservice and a posts microservice with their read models, how am I supposed to query the posts with each user that published them, how can I manage related data between different microservices?

1
If you have users and posts "microservices" you might go back and look at your boundaries... it looks like you just took entities and put each one into a "microservices" maybe this can help: youtube.com/watch?v=jdliXz70NtM - Sean Farmar
@SeanFarmar Having posts and users in the same bounded context wouldn't solve anything, I thought about having the needed user's data in the post aggregate but does this mean that would have to update each post published by the user if he updates his informations? - Ali Khiti
I wasn't suggesting putting them together, I was merely trying to suggest that you can find a better way to decompose your domain... When using a read model you can use multiple technics to push data to another data container to provide read data, happy to get on a call if you like, email me at [email protected] - Sean Farmar

1 Answers

1
votes

I have a users microservice and a posts microservice with their read models, how am I supposed to query the posts with each user that published them, how can I manage related data between different microservices?

You copy the data from the durable storage of the service that owns it to some common location that can build and update the view you want.

A simple implementation of the plumbing might be to have a timer that fires on some schedule, and each time the timer fires you pull updated information into your view, applying any transformations that you might need.

There's some latency, in that it takes time for the change made by the service to propagate to the views, but that's part of what you signed up for when you decided that users and posts should be serviced separately.

Let's say I have a view with the posts and the user's data needed. If the user updates his informations does this mean that I would have to update the data of each post published by that user?

"It depends". You might, for example, be copying the data into a relational database: if your views are described by relations, then updating the user would necessarily update the underlying data for all of the views; any representation of the view constructed after the database update would have the new data.

On the other hand, if you were using a document store, then you would probably need to either update all of the afffected documents, or design those documents with a link relation to the user document. This might be clearer if you think about the different ways that you can combine information on the web.