0
votes

I'm new to the microservice architecture, I learned a lot about it basics, CQRS and event-sourcing following this documentation http://microservices.io but I have some questions that are still unclear for me:

  1. Do you need to have different database servers for each CQRS read model, or you can put them in the same database but in different tables.

  2. If you have different databases then how are you supposed to join table. And if we use the same database then wouldn't our microservices be tightly coupled and that would defeat the whole purpose of using this architecture

2
You could put them in the same database, or you could have your query model be in a caching layer. Think of it this way, your 'query' model is a replicated form of the data -- maybe up to date, maybe not. - George Stocker

2 Answers

4
votes

Do you need to have different database servers for each CQRS read model, or you can put them in the same database but in different tables.

You would normally expect different logical databases. They might happen to be hosted on the same durable storage appliance, but none of the code should expect that data to be stored in the same place.

If you have different databases then how are you supposed to join table.

You are supposed to NOT join the tables. Instead, you copy the data -- translating from the representation that makes sense for writing to the representation that makes sense for reading.

if we use the same database then wouldn't our microservices be tightly coupled and that would defeat the whole purpose of using this architecture

The coupling really comes from the assumption that the different services are touching the same rows. In other words, we don't want the processes communicating through the database. Instead, we pass messages between the services, and the services manage their own data.

If you get that right, then the fact that you happen to store that data in the same appliance is irrelevant, because your services never actually look outside of their own little bubble.

Another way of expressing the same idea: you should be able to change "your" schema without needing to coordinate with anybody else.

let’s say we have a users microservice and a posts microservice, 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 to a common database where you can then create the reports that you need.

-1
votes

Command Query Responsibility Segregation (CQRS) is a pattern where you separate the models for Update from the model to Read your data. This means that the reading side of your microservice will be highly specialized and independent.

So talking about the reading side, you can have as many databases you want, usually, you have a specialized and optimized database according to the type of data and queries you need to perform. For example, you can use Elastsearch for fast searching or Neo4J for graph structure.

These databases are updated by the Events you microservice publish, so you do not join tables between different databases, they are independents.

Whether you use a relational database for each of your microservice and have a Query service to join data from multiple databases, this is a antipattern that will create a couple between services and be affected by lock tables.