Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep DB in sync when using microservices architecture?

Im about to learn how microservices architecture work. So far i unserstood that each microservice need its own database, which make sense.

So lets say we have a customer microservice which is responsible for creating a customer and returning a list of customers. The service will ofcource have it own customer DB.

Lets say we have very high load on this ervice, so we chooce to scale out 20x.

Så we have 20 microservices and each have its own DB, and all the services is behind a load balancer.

Now a client wants to create a customer, load balancer sends client request to service 9/20, and the customer is created.

On the next request the same client wants to be sure that customer is created and want to view the list of the customers, on the request LB sends him to service 11/20.

Now how do i make sure that service 9/20 synced the newly created customer to the db of service 11/20?

In MSSQL there are functionality to keep DB in sync by before alowing the initial commit, to save the data in all the other databases first, but this approach will give problems in the long run, because the more services there are the longer time it will take to make a commit?

like image 220
Timsen Avatar asked Jun 20 '16 05:06

Timsen


People also ask

How do microservices communicate with databases?

In microservices, instead of using traditional distributed transactions (XA/2PC-based), you have to use the sequence of local transactions (aka Saga). Here's how it works: one local transaction updates the database and then triggers the next transaction through messaging.

Can microservices share 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).

Should each microservice have its own DB?

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).

Can a microservice have two databases?

It means that we can use different database technologies for different microservices. So one service may use an SQL database and another one a NoSQL database. That's feature allows using the most efficient database depending on the service requirements and functionality.


1 Answers

each microservice need its own database

A separate DB per microservice is not a prerequisite (nor a requirement, really).

You can have as many microservices as you want working on top of the same database, but use different schemas for example.

The bounded context of a microservice should be the boundary.

Lets say we have very high load on this service, so we choose to scale out 20x.

Scaling to (X) instances of the same microservice does not mean necessarily having a separate database per each instance of that same service.

Most databases are designed with concurrent connections, users, transactions in mind. a single database instance (with some optimistic concurrency) can handle hundreds (if not thousands) of concurrent connections gracefully.

If you explicitly chose to have a separate DB per instance of the same service, then you will have to sync those databases up. and, most likely, data consistency will suffer for it.

Here are some suggestions:

  • use a single database per microservice (not per instance) no matter how many instances are using it. And only consider a DB per instance when you're sure a single DB cannot handle the load.

  • Use a shared cache layer on top of the DB (maybe redis cache)

  • Use a database cluster to deal with high load/availability of databases.

like image 56
Bishoy Avatar answered Sep 25 '22 16:09

Bishoy