Mike
Mike

Reputation: 663

Does data duplication in microservice architecture lead to code duplication?

For example, we have an application with microservice architecture. The services communicate with each other using an event bus as follows:

  1. TeamService subscribes to UserCreated event
  2. UserService publishes the UserCreated event
  3. TeamService stores the user from the UserCreated event to its own database

So, we duplicate data in some services. And that's okay in microservices architecture. But what about the code that processes this data? For example, the user might have isPublic field. If the isPublic field is false, the user's profile information is visible only to the user himself and to a user with administrator rights.

Thus, UserService and TeamService must have the same code to handle this logic. Or is there another way to handle the isPublic field without code duplication?

Upvotes: 1

Views: 2953

Answers (3)

Mehdi Charife
Mehdi Charife

Reputation: 1024

One way to handle this is to disallow other services from handling user-related logic directly. Instead, services that wish to enact such logic should call the user service to perform the desired logic for them. Depending on your requirements and the particular services in place, the calls can also be asynchronous to conform to an event-driven architecture.

Upvotes: 0

xs2tarunkukreja
xs2tarunkukreja

Reputation: 462

In microservice architecture, data may be replicate in multiple services for e.g. User Service and Team Service but only one service own and serve the data. For e.g., user profile data is owned by User service and it should be served by user service only.

So, if any UI or other service want to show profile service then it ask the same to User service and User service may reject (access denied) if the profile is not public.

Upvotes: 0

Cosmin Ioniță
Cosmin Ioniță

Reputation: 4045

You can build a library which contains the common code, and add it as a dependency in both UserService and TeamService

Upvotes: 1

Related Questions