0

I am learning micro-services in java and after going through several reference on its design and patterns still not clear what is the right way to check foreign key relationship.

I have two micro-services userService and groupService.

userService has: user_id, user_name, group_id
groupService has: group_id, group_name

When adding new user through Rest POST I have to make sure group_id exist in groupService.

I went with following approaches after going through several reference:

First, userService Post calls group service with provided groupid to check if group exist and then add user but not happy as this seems to tightly coupled user service and also I have to get Group domain object in my userService to parse it.

Second way using Aggregator Pattern. I have created another service user_department_composite_service and brought common domain model in this service so on POST to composite service, call GET group and on success call POST user. This seems to separate concerns from user service but not happy as to POST user I am calling POST composite_service.

Third approach seems event sourcing (not done yet).

Could anyone guide me or provide a reference for better way to do this as this seems a common problem with micro-service. Also if anyone knows good book for microservice design pattern please let me know.

Thanks

ahmadl
  • 1
  • 1
  • These relationships are better left to a single service. These should be one service, not two. You don't want that latency - keep them in a single service with GET requests for user and group. – duffymo Feb 21 '17 at 11:12
  • I get your point but above services I am using are as example. Let say we have order_service and customer_service and assume a scenario where order need to verify customer address to know if it can be shipped before creating order then at what level to perform that check? – ahmadl Feb 21 '17 at 11:46
  • I'd have an address service to do scrubbing and validation. That does not have any JOIN characteristics. To be clear, neither the customer nor order service would be calling the address service. An application would orchestrate those calls. Micro services should be self-contained and focused. – duffymo Feb 21 '17 at 12:35
  • @duffymo, Thanks for explaining further. I assume with every order you keep customer id so in case if I have to validate customer before order then where is the right place to do? I am refering aggregator Microservice Design Pattern for my imlementation but not sure doing correctrly http://blog.arungupta.me/microservice-design-patterns/ – ahmadl Feb 21 '17 at 12:55
  • Validate customer? The contract should be that the customer exists as a pre-condition. The order service will persist the order, which includes the customer id, but it will be passed in. Micro services need to be idempotent. – duffymo Feb 21 '17 at 12:57
  • May be I have taken wrong example. Another example let say we have HR and employee service. If HR need address of employee to printout letters to post. Should HR GET employee address directly or any composite/aggregator service? Also HR will need Address model to get street, town etc. which will be duplicate from employee service, do you think that will be right thing to do? If you can refer me some example for my understanding that will be great. – ahmadl Feb 21 '17 at 13:07
  • Ask the Customer for their Address. There isn't a cookbook "right" answer. This is design. Do your best to think through what your system needs to do. Implement, measure, and refactor as needed. – duffymo Feb 21 '17 at 14:25

0 Answers0