Search code examples
mysqlmicroservices

Microservice Architecture design


I have few doubts on Microservice architecture.

Lets say there are microservices A, B and C. A maintains the context of a job apart from other things it does and B,C work to fulfill that job by doing respective tasks for that job.

Here I have questions.

1. DB design

I am talking about SQL here. Usage of foreign keys simplifies lot of things. But as I understand microservice architecture, every microservice maintaines its own data and data has to be queried from that service if required.

Does it mean no foreign keys referring to tables in another microservices?

2. Data Flow

As I see here are two ways.

All the queries are done using jobId maintained uniquely in all microservices for a job.

  • Client requests go directly to individual service for a task. To get summary of the job, client queries individual microservices collects the data and passes to user.
  • Do everything through coordinating microservice. Client requests go to service A and in tern service A will gather info from all other microservices for that jobId and passed that to user.

Which of the above two has to be followed and why?


Solution

  • Does it mean no foreign keys referring to tables in another microservices?

    Not in the database sense. One microservice may hold IDs of remote entities but should not assume anything about the remote microservice persistence (i.e. the database type, it could be anything from SQL to NoSQL).

    Which of the above two has to be followed and why?

    This really depends. There are two types of architectures: choreography and orchestration. Both of them are good. Which one to use? Only you can decide. Here are a few blog posts about them:

    Also, the solution to this SO question might be useful.