1

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?

Kiran
  • 499
  • 2
  • 6
  • 17

2 Answers2

1

You're correct thinking that microservices should ideally have their own data structure so they can be deployed independently. However there are several design patterns that help you, and that doesn't necessarily translates in "No FK". Please refer to:

The patterns listed above answer both your questions.

Community
  • 1
  • 1
AR1
  • 4,507
  • 4
  • 26
  • 42
  • @Kiran, if you open the urls in the answer, you'll see that adopting one or more of the proposed patterns makes the use of the FK not necessary. If you still think it is, then you might want to review your services design. – AR1 Nov 15 '17 at 14:33
  • I understood your answer incorrectly. I will go through your links. Thanks – Kiran Nov 15 '17 at 14:50
0

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.

Constantin Galbenu
  • 16,951
  • 3
  • 38
  • 54
  • Thanks for reply. For the first answer, all microservices use MySQL in our case. In that situation, if we use foreign keys, delete in main table triggers delete in all other tables referencing that as foreign key. But is it anti-pattern for microservices? Thanks for the links. I will go through and get back. – Kiran Nov 15 '17 at 12:37
  • @Kiran this is definetely an anti-pattern. Microservices do not share tables or databases. – Constantin Galbenu Nov 15 '17 at 12:41