Lets assume that we have a Spring Boot application that uses microservices architecture. Each of the services has its own domain models, but each service must reference a User domain object. What would be the best approach on how to solve this issue? Would it be better for each service to just have a userId and then, when needed, ask user service for user details, or would it be better to have a shared domain library for all microservices?
Microservices – Shared Domain Models in Microservice Architecture
microservicesspring
Best Answer
If you did go for microservices to benefit from scalability, loose coupling, and easy independent modification of each service, the you should stick to it to the maximum possible extent.
Overall architecture
I think that the best approach would be:
Additional reading:
Code sharing
Now if you agree on the solution above, we have a user microservice (encapsulating domain model for user) and all other services are consumers of the same microservice. The question is then to know if you want:
I will not take a clear cut position on that, as there are opinatered opinion wars on this code sharing topic, and I don't think that I'm in a position to take an objective position. Here is already some additional reading:
My own opinion on that is that you SHOULD NOT SHARE code between the user-provider and the user-consumers, in order to avoid a tight coupling. However you COULD SHARE the user-consumption code between consumers if you have a strong version management in place. This approach would have some advantages: