Search code examples
spring-datamicroservices

Microservices communication JDBC SQL VS REST


which is the best way between these two approches that allow two microservices to exchange data

1- Via Rest call.

2- Each microsevice expose its related data as a database's view ,so that it can be reached by other microservices using Spring JDBC template or JPA.

Notice that each microservice has its own (private) tables in the same database schema.

Thanks,


Solution

  • I would say that from a domain driven design perspective (and microservices could be considered as domains), other domains should not know anything about how your data is stored/structured (Bounded Context). Therefore I would vote for REST. Another point would be, what if your table/view structure changes? this would cause breaking changes in other microservices. With REST you can change the underlying code of your routes without bothering your consumer. Direct Database queries would be needed if you have to use stored procedures (or other database related performance tweaks) for better performance.