Search code examples
architecturemicroservicesdecoupling

Microservices, How to decouple services if data on one service depends on other?


Lets say that I have two microservices, which looks like this:

Services

I have one service that stores all questions and their answer options. The second service creates modules that will use questions from first service.

So we have Question Service and Module Service.

When we create modules:

  1. Client will query questions
  2. Client will send question_id with module data

When user will try to complete this module:

  1. Client will query module
  2. By question_id it will get question and its options to show as test.

Now there is a main problem. How Module Service will know that user's answer is correct or not?

Now I think two types of solving this problem:

  1. Client will ask Question Service if this answer is correct, and then send the result to Module Service. However, this method is unreliable, because requests from Client can be faked and Module Service will store incorrect results

  2. Client will send answer checking request to Module Service, which will then send request to Question Service by doing direct HTTP call. This is also bad solution, because this makes Module Service tightly coupled to Question Service.

Are there any solutions to decouple Module Service from Question Service?


Solution

  • The best way to do this and to not couple the two services is using a message queue, it could be rabbitmq, kafka or any other tool that can be an intermediate to this two services.

    When the user tries to answer a question, he sends his answer to question service because it is the one who knows it. Then the question service checks if the answer is correct and sends a message through a message queue to the module service indicating the user,the moduleid, the question that is trying to answer and the result. Then, the module service will recieve the message and update this user module properly