Attach a table between different microservices

I'm still trying to understand microservice architecture.

The idea of ​​sharing another application (including the database) excites me. But I'm still confused if there are two microservices, for example. Product and user. both the product and the user table product and user, respectively, in their database. According to the best practices in the field of microservice, we can only access the database from the service.

The problem is that we have a product table in which there is a user_id column. We want to search for a product that also returns the name of the user creating the product. This requires a connection between the product table in the product microservice and the user table in the user microservice. How do you deal with this?

+5
source share
3 answers

You need to make a call for each microservice and make a connection manually or pass the corresponding user IDs to each service.

UserMicroservice:

SELECT * FROM Users WHERE some condition is true 

Get a list of users, including their id s.

ProductMicroserivce:

 SELECT * FROM Products WHERE some condition is true AND userId IN (.........) 

Thus, users and products can still be in two different databases, products just have to have a userId concept.

You can also do the opposite, ProductMicroserivce:

 SELECT * FROM Products WHERE some condition is true 

Extract all UserIds, then call UserMicroservice:

 SELECT * FROM Users WHERE some condition is true AND id IN (.........) 
+2
source

While I think that there is nothing wrong with doing this as Ian said, I would like to add that difference microservices should be added to your system, they have a different character.

The above-mentioned segregation of services is what we saw a lot in the SOA world, and it turned out that it is too complex very quickly, without offering much value.

If you, and I understand this is just an example, you need to ask the user connected to the products - why split the service? Ultimately, you create a service on the db object instead of looking at what the requirements are for a given one, which seems to me a limited context.

-Lars

+4
source

The best answer to such scenarios is CQRS. Maintain materialized representations of the relationship between the user and the product. Metrized views can be any, which provides low latency when reading, for example, in NoSql DB. Whenever a team updates a user or product with its microservices, the corresponding events must be generated and captured by another service that manages this materialized view. This service will be used to receive your request. Always keep in mind, Microservices architecture believes in Eventual Consistency, which is definitely not so bad :). Hope this answers your question.

+1
source

Source: https://habr.com/ru/post/1268046/


All Articles