Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microservices: database and microservice instances

Lets say we have a microservice A and a B. B has its own database. However B has to be horizontally scaled, thus we end up having 3 instances of B. What happens to the database? Does it scale accordingly, does it stays the same (centralized) database for the 3 B instances, does it become a distributed database, what happens?

like image 517
nobitta Avatar asked Nov 29 '16 02:11

nobitta


People also ask

Can 2 microservices have same database?

In the shared-database-per-service pattern, the same database is shared by several microservices. You need to carefully assess the application architecture before adopting this pattern, and make sure that you avoid hot tables (single tables that are shared among multiple microservices).

Does each microservice need its own database?

As you described it very well above, each microservice needs to own it's DATA, which could be held within a dedicated database, within a dedicated schema (within a database), or even a set of dedicated tables (within a schema within a database).


3 Answers

The answer is based on the which kind of data should be shared from 3 B instances. Some occasions:

  1. The B is just read data without write anything, the DB can use replicate methodology, and three B instance just read data from different DB instance, and DB was replicated.

  2. The B instance can read/write data without interrupt other B instance, that mean every B instance can have designated data, and no data sharing between instances, the database was changed to three databases with same schema but totally different data;

  3. The B instances should share the most of data, and every instance can occasion write the data back to the DB. So B instance should use one DB and some DB lock to avoid conflict between the instances.

In other some different situation, there will be many other approaches to solve the issue such as using memory DB like redis, queue service like rabbitMQ for B instance.

like image 160
Justin Avatar answered Oct 17 '22 20:10

Justin


using one database by mutliple service instances is ok when you are using data partitioning.

like image 44
giorgi dvalishvili Avatar answered Oct 17 '22 21:10

giorgi dvalishvili


As explained by Chris Richardson in pattern database per service,

Instances of the same service should share the same database

like image 2
jmhostalet Avatar answered Oct 17 '22 22:10

jmhostalet