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.
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.
So far so good. But here comes the question:
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)
Read More:
The Hardest Part About Microservices: Your Data
Intro to Microservices, Part 4: Dependencies and Data Sharing
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With