Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sync the database with the microservices (and the new one)?

I'm developing a website with the microservice architecture, and each of the service owns a database. The database stores the data which the microservice needs.


Post, Video services need the user information, so both of the services subscribed to the NEW_USER_EVENT.

The NEW_USER_EVENT will be triggered when there's a new user registered.

enter image description here

Once the services received the NEW_USER_EVENT, they put the incoming user information to each of their own database. So they can do things without asking the User service.

enter image description here

So far so good. But here comes the question:

  • What if I'm going to create a new service? How do I get the registered user informations and put them in the new service?

Maybe I can get the informations from the existing services. But the events are pushed by the messaging queue (NSQ).

If I'm going to copy the data from one of the microservices, how do I make sure which service has the latest user informations? (Because some services haven't received the latest event)

enter image description here


Read More:

The Hardest Part About Microservices: Your Data

Intro to Microservices, Part 4: Dependencies and Data Sharing

like image 725
Yami Odymel Avatar asked Jan 03 '17 14:01

Yami Odymel


People also ask

How do microservices interact with database?

Microservices database challenges 101 A monolithic application interacts with a single database. The data is shared between all application's components. By contrast, in a microservices app, data ownership is decentralized. Every service is autonomous and has its own private data store relevant to its functionality.

Does microservices use different database?

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

What if I'm going to create a new service? How do I get the registered user informations and put them in the new service?

You have to replay all events to which this new service is subscribed from the beginning of time (you should have an "event store" that keeps all events already happened in your application). Also, you can put a bit smarter logic when replaying events by starting from the most recent ones and going back in time. In this way, you will be able to restore most valuable data first. Just be careful to handle interdependent events correctly.

Data source: The events are pushed by the messaging queue(NSQ), If I'm going to copy the data from one of the microservices, how do I make sure the copy source has the latest user informations?

You are not talking about doing backups, right?

Aside from backups, in the event-driven systems people usually don't copy the data in a classical way, row by row. Instead, they just replaying events from event store from the beginning of time and feeding those events to the event handlers for the new service (or new instance). As a result, new service eventually becomes consistent with the other parts of the system.

like image 108
IlliakaillI Avatar answered Oct 21 '22 22:10

IlliakaillI