Search code examples
restmicroserviceshateoas

Sharing entity ID between microservices


Let's say I have a Users microservice. Its data is consumed via REST API following HATEOAS "pattern", so a common request/response would be something like this:

GET /users

{
  results: 5,
  data :[
    {
      name: "John Doe",
      email: "whatever",
      ...,
      links : [
        {
          rel: "self",
          href: "/users/1"
        }
      ]
    },
    ...
  ]
}

As HATEOAS says, the users' ID is not returned, but a link to "self".

So far, so good. Now, I want another microservice to manage users' pictures. In that new microservice there is a relationship between one user an her pics, so I will need a user identifier.

Should I use "/users/1" ("self" link) as user ID in the pics microservice?

If not, how can I approach this?


Solution

  • You cannot assume the structure of the url but you can return an entity id.

    Should I use "/users/1" ("self" link) as user ID in the pics microservice?

    If you do this you are assuming that the second microservice is using the same url scheme which is bad. You couple the two microservices.

    Also, the urls included in the response of a request have a meaning only to that (micro)services, you cannot just assume that a corresponding resource in another system has the same id