Search code examples
microservices

How to manage store "created by" in micro-service?


I am building the inventory service, all tables keep track the owner of each record in column createdBy which store the user id.

The problem is this service does not hold the user info, so it cannot map the id to username which is required for FE to display data.

Calling user service to map the username and userid for each request does not make sense in term of decouple and performance. Because 1 request can ask for maximum 100 records. If I store the username instead of ID, there will be problem when user change their username.

Is there any better way or pattern to solve this problem?


Solution

  • I'd extend the info with the data needed with from the user service. User name is a slow changing dimension so for most of the time the data is correct (i.e. "safe to cache")

    Now we get to what to do when user info changes - this is, of course, a business decision. In some places it makes sense to keep the original info (for example what happens when the user is deleted - do we still want to keep the original user name (and whatever other info) that created the item). If this is not the case, you can use several strategies - you can have a daily (or whatever period) job to go and refresh the users info from the user service for all users used in the inventory, you can publish a daily summary of changes from the user service and have the inventory subscribe to that, you can publish changes as they happen and subscribe to that etc. - depending on the requirement for freshness. The technology to use depends on the strategy..