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:
- 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)
Read More:
The Hardest Part About Microservices: Your Data
Intro to Microservices, Part 4: Dependencies and Data Sharing
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.
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.