I have one microservice named UserRegistration
for user creation, another microservice named Billing
where able to show the billing details for users.
After a new user is being created, it will send userId
from UserRegistration
to Billing
through message queue like Apache Kafka or RabbitMq. Since these 2 microservices has different database, Billing
microservice only has userId
, how Billing
able to retrieve the user details like name, age, address? Through REST API in UserRegistration
?
2 Answers 2
To avoid duplication of data (with the associated risk of working with stale data) using a UserRegistration
service makes most sense indeed. Since both services are apparently under your control you could define an endpoint in UserRegistration
that delivers exactly the data needed by Billing
, unless such an endpoint already exists, of course.
-
what is the endpoint you mean? (REST call?, messaging queue?) sorry i can't really imagine how's the flow for the endpoint in
UserRegistration
hades– hades2020年03月26日 14:55:31 +00:00Commented Mar 26, 2020 at 14:55 -
1REST call, sorry that my wording was vague.
Billing
will need name and address essentially only when bills are being sent, so getting these fields just when needed using a REST endpoint will ensure that only up-to-date data will be used.Hans-Martin Mosner– Hans-Martin Mosner2020年03月26日 15:28:26 +00:00Commented Mar 26, 2020 at 15:28
If the billing service needs other data than it actually holds itself, what is the value of using a microservice here? If billing cannot run when UserRegistration is down, you have a tightly coupled big ball of mud.
So, ask yourself: which business value does the billing service supply, under which circumstances, and when? After you have answered the question, you basically have the following possibilities:
- scratch the billing service alltogeher, as it is part of a more general UserService
- depend on the UserRegistration service and do live queries by whatever method strikes your fancy
- transfer all necessary data to the billing service, and deal with the risk of stale data
-
thanks, but how those companies like Amazon, or Google do microservices, i mean, i don't think it is possible to eliminate the dependencies between 2 services(even the slightest) regardless of how well you design it right?hades– hades2020年03月27日 07:43:58 +00:00Commented Mar 27, 2020 at 7:43
-
1@hades It is possible. The big question is: why do you use microservices? Microservices may well be used to utilize computing power dynamically (i.e. have a fat common database and dynamically add images which do CPU number crunching) OR to cut your application into vertical slices. For the second approach, a microservice should (ideally, seldom achieved) always be able to run standalone without the other serivces being present.mtj– mtj2020年03月27日 08:19:16 +00:00Commented Mar 27, 2020 at 8:19