mohamed naser
mohamed naser

Reputation: 492

Microservices how database per service handle relations

So as I understand microservices database per service pattern, if I'm building social networking app

The feed will be in one database and the user will be in another database

So how to handle foreign key relation between user and their post, as any sn I need to return the user data with the post here's the different ways I can think of

First make a user_id field in the post table and in return of the data: 1- the apis make calls to get the data of the user from user apis

Won't it make the service less decoupled?

2- duplicate the user data (the part needed for example first name, last name, avatar and id) and using distributed system like kafka if user changes his data it'll also be changed in the feed database

But won't that just be pointless to have multiple databases?

So how to handle this relation?

Upvotes: 3

Views: 717

Answers (1)

Salil
Salil

Reputation: 9722

Whether you duplicate user data while storing post data depends on what you (or the customer) expects when the user data changes. If they are okay with some delay till the posts data is updated or it not getting updated at all, then it's okay to duplicate user data in posts for efficiency.

But, for microservice design, we need to set aside database schema concerns, and not design from bottom to top (from the data layer to application layer), but from top to bottom (from application layer to data layer). Think of how the customers are going to use the application, and then identify sub-domains within the application domain (called "bounded contexts"), and then proceed to identify the "aggregates" within this bounded context. Refer aggregates

Upvotes: 2

Related Questions