2

I would like to know about the database design of microservice. The scenario is given below

Microservice A is using mysql database. If I am creating multiple instance of microservice A:

  1. Each microservice A should point to the same database (common database for all instances) or

  2. Each microservice A points to its own private database

If each microservice A instance points to its own private database how to sync the data of each instance? Can you please explain the best way.

scott_lotus
  • 3,171
  • 22
  • 51
  • 69

1 Answers1

4

I am creating multiple instance of microservice A:

I assume by this you mean that if you are deploying multiple instances of your micro-service A to one or multiple servers as separate instances of that micro-service?

Short answer:

In general every micro-service should have one database. This means that your micro-service A has a database (lets call it) micro-service-A_db. Regardless of the fact that you micro-service-A is deployed to multiple servers(horizontal scaling) or on 1 server the deployment instances of your micro-service-A are accessing and using your one database micro-service-A_db.
All your instances of your micro-service-A should access the same database. This is the standard and most common use case. There are exceptions to this.

Long answer:

Each microservice A should point to the same database (common database for all instances) or

In short yes.

Each microservice A points to its own private database

In short no. But...

As assumed above if you mean multiple instances of micro-service-A should point each to private database then NO. You should not do that.

In general your application Domain/Business logic is split into micro-services based on some criteria. As soon as you split your system to multiple micro-service per Domain(you can read about it here) each micro-service is responsible only for that part of Domain means all the logic related to it and its storage. Let me give an example. Lets say you have an online shop application and you have 3 micro-services:

  • products-inventory-micro-service
  • payment-micro-service
  • orders-micro-service

Each of these micro-service has its own database:

  • products-inventory-micro-service has inventory-micro-service-db
  • payment-micro-service has payment-micro-service-db
  • orders-micro-service has orders-micro-service-db

Those databases can only be used by their owner micro-service. If the micro-service is deployed on one or multiple servers as separate process/instance it does not matter. All of them access the same database. On the other hand you can for sure use a lot of different types of deployment of your micro-service databases like having multiple Read-only replicas, use Sharding to partition your data and so on. There are a lot of ways to scale your database. The point is that a micro-service can only use its own database.

Additionally to this I can say that there are exceptions to this rule: 1 database - 1 micro-service. Sometimes people use 2 databases per micro-service Domain. These are examples where the second db is some kind of read optimized db for particular read/query operations. This is often used together with CQRS pattern. If you want to know more about it you can read a lot about CQRS here on Stack Overflow and in general on the web.

xargs
  • 2,741
  • 2
  • 21
  • 33