Search code examples
architecturemicroservices

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?


Solution

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