Microservices: How to efficiently deal with data dependencies between microservices

I am developing an application using a medium stack microservices development approach. I am facing a situation where data needs to be shared across multiple microservices. For example, let's say I have user services, videos, messages (send / receive, inbox, etc.). Video and message recordings now belong to your account. Since users create video and send / receive message, there is a foreign key (userId) that must be associated with the generated videos and messages. I have scenarios where I need to display the first, middle and last name associated with each video, for example. Now let's say that on the front end, the user views the list of videos uploaded to the system, 50 at a time. In the worst case, I could see the situation where the pull occurs 50,when each video is linked to a unique user.

There seem to be two approaches to this problem:

First, I make an api call to the user service and each user is bound to each video in the list. This seems ineffective as it can get really chatty if I make one video call. In the second api call script I get a list of videos and send a separate list of user's foreign keys to request so that each user binds to each video. This seems to be more efficient, but it still feels like I'm losing performance by putting everything together to send them to the display, or it still needs to be manipulated.

Two, every time a new user is created, the account service sends a message with the user information that every other service needs in the fanout queue, and then it is the responsibility of the individual services to add the new user to the table thus keeping the link loose. The extreme drawback here would be data duplication and the need to have a fan queue to process when updates are to be made to ensure eventual consistency. Ultimately, though, this approach seems to be the most efficient in terms of performance.

I am torn between these two approaches as they both have their fair share of trade-offs. Which approach makes sense to implement and why?

+3


source to share


2 answers


I am also interested in this question.

First of all, the scenario you described is very common. Users, Videos, and Posts are definitely three different microservices. There is no problem in the way you broke the system to pieces.



Secondly, there are several options for how to solve the problem of data sharing. Take a look at this excellent article from auth0: https://auth0.com/blog/introduction-to-microservices-part-4-dependencies/

+1


source


Do not limit your design solution to the 2 options you listed. The hardest part about microservices is figuring out what a service is and how to cut your application into chunks / services that make sense to be implemented as a "microservice".

Just because you have these 3 objects (user, video and post) doesn't mean that you need to implement 3 services. If your actual use case shows that these services (or entities) are highly dependent on each other to fulfill a simple request from the front-end, than a pure signal that your cutting was wrong.

From what I can see from your example, I would develop 1 microservice that does the request. Remember, one of the fundamentals of microservice design is to be as independent as possible. There is no need to complicate services, this is not SOA.



https://martinfowler.com/articles/microservices.html -> great read!

Regards, Lars

+1


source







All Articles