Search code examples
microservices

Helper data and functions should be in a microservice?


I am creating an application using microservice architecture. I have broken down the domains into one microservice. I have two questions regarding handling additional data and functions.

  1. I have created an MS named ms-notification to handle all the users' notifications such as SMS, Email, Push notifications,...
    When I need to send a notification to a user, I publish an event and consume that in ms-notification. Is it a good approach to do this?
  1. I have a list of Courses that contains about 2000 Courses(Id, name, ...) and CourseId is being used approximately across all the microservices. Should I store these 2000 Courses in each microservice's database? (I am using a single database per microservice) Or should I create a new microservice and expose the 2000 list with an API controller?

Thank you in advance!


Solution

    1. Yes, it is a good approach to have it Event-Driven, however you can also have separate microservices for each type of notification, because they are independent, i.e.,
      1. SMS Microservice
      2. Email Microservice
      3. Push Notifications Microservice
                     ┌─────────────┐
                     │    Other    │
                     │Microservices│
                     └──────┬──────┘
                            │
                            │
                         Publish
                           SMS
                          Email
                      Notif Events
                            │
                            ▼
                     ┌──────────────┐
                     │ Event Queue  │
                     └──────────────┘
                            ▲
                            │
                            │ Subscribe
                            │
           ┌────────────────┼────────────────┐
           │                │                │
           │                │                │
    ┌──────┴──────┐ ┌───────┴───────┐ ┌──────┴───────┐
    │     SMS     │ │  Push Notif.  │ │    Email     │
    │ Microservice│ │ Microservice  │ │ Microservice │
    └─────────────┘ └───────────────┘ └──────────────┘
    

    This will result in a cleaner architecture.

    1. You should create a new microservice. Each microservice should be the owner of its database, i.e., if you want to consume data from another microservice you must do so by invoking a public API.