2

I'm not sure how to best ask this, as it's the first time I have to tackle such stuff. It might be that it's a common problem and I'm just using the wrong keywords to search. Basically, imagine a microservice needs (as part of its data) the ID of some objects managed by another microservice. For example, Users and Orders: the Orders microservice needs the ID of the user who placed the order, as exposed by the Users microservice.

This leads to two questions:

  • Is it good practice to save the user id for a user in the orders microservice (that is, in the database that backs the microservice)? (or, in the general case, the ID of an object managed by a microservice in another one)
  • How does a consumer of either API finds out the user ID for a user (or the order ID) to use to start making actual REST API calls? Normally only some generic identifier comes from the application, like an email address or telephone number. I guess there must be some kind of mapping service which, given an email or a phone number, returns the user ID to use for the REST calls? Where should this translation service be exposed? In the Users microservice?

Thanks

2 Answers 2

2

Is it good practice to save the user id for a user in the orders microservice (that is, in the database that backs the microservice)? (or, in the general case, the ID of an object managed by a microservice in another one)

If it wasn't, then you'd be patently unable to ever link two resources that come from different microservices.

It's good that you're already focusing on the ID of the user, not the entire user's data, because that is precisely what the default approach for microservices is. You only store the reference to the resource in other microservices. The actual data stays in the "home" microservice, and other microservices have to request it (based on the stored reference).

That being said, there are cases where you do want to copy over some data, but this is highly contextual and comes with its own cost. As a baseline, you should try to avoid these situations and only store references (i.e. IDs) external to the "home" microservice.

I guess there must be some kind of mapping service which, given an email or a phone number, returns the user ID to use for the REST calls? Where should this translation service be exposed? In the Users microservice?

Since the Users microservice is the only service that stores the user's data, it is inherently the only one able to translate a data field to an ID. So yes, the Users microservice should expose an API endpoint to provide this functionality.

Whether this is a targeted endpoint (get user ID by email) or a broad one (get users, with many possible filters); depends on what makes the most sense in your scenario.

0

I guess there must be some kind of mapping service which, given an email or a phone number, returns the user ID to use for the REST calls? Where should this translation service be exposed? In the Users microservice?

In general, that is not a good idea. Microservices exist for a reason. That reason is usually independent scalability, independent operation, independent development, etc. Depending on a different service for data query is therefore a dangerous road to go down on. It destroys most of the reasons microservices usually exist for.

What's the alternative? Well, depends on the exact case. Normally user ids are contained in the token the user already has. So any service seeing a token (JWT for example) should already have the user id. There's no need to ask somebody else.

Things like order id can have multiple solutions. Easiest one is of course fulfilling the order from one service. No need to get other services involved, or other services may have their own id if additional steps are needed in the process.

If that doesn't work, the order process must carry the order id (if it's that important) all the way through where each callee (service) can see it.

In general, having "Users", "Orders" services do not sound right though. Services should handle use-cases in their entirety, not be glorified database tables. You get all sorts of problems if you do that.

3
  • In general I tend to agree, but here for example JWT is not involved at all, as the service supplies a list of users just like it could supply a list of other objects. The operator has an interface where they input an email address, and that needs converting to the ID necessary to use the Users microservice (or give an error if that email doesn't belong to any user, of course).
    – persson
    Aug 2, 2022 at 22:11
  • The Users and Orders was an example, but I think the need/idea to have IDs exposed by one service saved into another service applies at a more general level.
    – persson
    Aug 2, 2022 at 22:14
  • Even if the UI of the "user" service need certain functionality, like searching based on email address, that doesn't mean it should expose that to others. The point is to have services which are functionally closed as possible. It all depends on why you have microservices in the first place though. Aug 3, 2022 at 6:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.