I am planning to use the Microservices architecture for the implementation of our website. I wanted to know if it is right to share databases between services or if it is preferable to have a separate database for each service. In this regard, can I consider having one common database for all services or does it violate the very essence of Microservice architecture ?
2 Answers
Microservices offers decoupling. You must break down your application into independent domains. Each domain can have a DB. In case other MS needs to access data owned by some other microservices, they have to communicate over the network.
In case you feel that there are too many dependent services and the network calls would be too much, then you can define a domain, clustering the dependent services together.
For instance -- Suppose I have an online Test evaluation Service where a manager of a company can post tests and he can view results of all the employees in his department.
My Microservices for this scenario would be:
Initial Design
- User Service: For login and user information.
- Test Service: Service to evaluate tests.
- Employee: Handles employee details
- Company: Handles organization CRUD
- Department: Handles department CRUD
After breaking it down, seems like employee, Organization and Department service would be making too much network/API calls as they are tightly dependent on each other. So it's better to cluster them.
Updated design
- User Service : For login and user information.
- Test Service : Service to evaluate tests
- Organization : Handles Company, Employee and Department related operations.
Each service could have it's own DB and it's independently deployable. User and Test Service can use mongoDB or any NoSql DB and Organization service can use RDBMS.
Hope this helps.
If you share the same database then you loose two of the most important advantages of microservices: strong cohesion and loose coupling (page 25).
You can share the same database if you don't share the tables in it. For example, microservice1
uses table1_1
and table_1_2
and microservice2
uses table2_1
and table2_2
. When I say uses I mean read and write. One microservice don't read and don't write on the other's tables.
-
2Thanks for the answer. But in my application, there is a case where one of the services makes use of data from a couple other services. In such a case, am I not risking too many API calls between services and from client to different services ? Apr 15, 2017 at 15:03
-
7
-
2You may want to look at your design, find the bounded context so you have nicely carved out verticals, that way you should not need to cross services boundaries to do data updates (state changes), data reads are less of a concern... Apr 20, 2017 at 15:41
-
you could use a message broker for sharing state, and each microservice saves this state to its own (maybe service local) database (or a cache).– sschrassAug 25, 2018 at 15:19
-
@ConstantinGalbenu What if I need some data? For example for computing certain statistics for scheduled weekly emails– scarfaceOct 8, 2019 at 14:56