Search code examples
node.jsmicroservicesbounded-contexts

Microservices: how to handle scenario when data is needed between bounded contexts


I am beginning to move a node.js project to a microservices approach and am not getting how data should be shared. Here is an example, suppose I have the following bounded contexts:

  • account (used for managing user accounts, permission, login/logout, profiles, etc.)
  • image (used for managing file uploading such as profile pics, image gallery, etc. associated with a given account)
  • video (used for managing video uploading and transcoding to various formats associated with a given user)

In this example, the image and video seem to have a natural dependency on the accounts bounded context or microservice. I assume that I could copy the accounts table over to the image and video databases and only store the accounts data that is needed but this seems like a nightmare in terms of maintaining consistency, etc. as account information is updated frequently, not to mention if other microservices need this same dependency. I could have all three microservices to use the same database, but that would violate the recommendation that each microservices being tied to its own database.

What is the recommended approach for handling this scenario? I am still in the planning phase and want to make sure I did this the right way.


Solution

  • You don't need to copy the account data into the video / image services. When you store a video / picture, just give the accountID with it.

    When you for example need to get a user and it's images, you have different options to get the data:

    If you work with an API Gateway, you could make 2 async parallel calls from it to the services, and combine both data in a DTO and give that back.

    If that isn't possible because you first need specific data from the user, just make a call to the account service, which on his turn will make a call to the image service to retrieve it's data, and send the whole package back.

    It's up to you to see if that is possible or not.

    Long story short, just store account ID with each video / image entity.