Search code examples
design-patternsarchitecturemicroservicessoftware-design

Zone of responsibility of microservices?


Inspired by microservices I try to design a small server application to learn how to build them and how to connect them between themselves.

I know for sure at least two services I need: OrderService that does business logic within users' orders and NotificationService that sends messages to users via some channels.

Assume the user orders comes to OrderService. OrderService handles this inquiry and provides a list of users who should get notification messages after delegating this to NotificationService.

I wonder, should NotificationService know about users? Whats kind of data I should send to the NotificationService: users entities, users id, or message type?

Should NotificationService make requests to the database for receiving data?

I guess, no. NotificationService should know only the ready body message and type of channel through which message must be sent.

Could you share your mind about this how to find boundaries of each service and restrict theirs by responsibility?


Solution

  • My take on this is that usually, we tend to focus too much on the micro of the microservices word. It does not mean you should always have so focused services, that only do a very tiny thing. Why? Because this leads to the questions that you are now facing. Microservices must not share a database so the data should flow from one to the other, but then the question is, which data?

    My first suggestion is for you to really think if two separate services are what you really need or if you are overengineering it. Running multiple microservices is a complicated task, for multiple reasons. So we really need to think if we really need this additional burden.

    Assuming you end up deciding that you really need the two separate microservices then I would say the following:

    I wonder, should NotificationService know about users?

    No, otherwise you will couple your services even further.

    Whats kind of data I should send to the NotificationService: users entities, users id, or message type?

    Only the necessary to really send the notification: the recipients of the notification (its email for example), the content of the notification and maybe notification type if it helps your logic on the NotificationService side.

    Should NotificationService make requests to the database for receiving data?

    No, never, otherwise you are breaking the "rules" of microservices (databases must not be shared and each microservice should have its own database).