-3

I development multi subdomain website with separated databases with sso, like as:

  • www.example.com (corporate web & shop) - www_db

  • blog.example.com - blog_db

  • forum.example.com - forum_db

  • account.example.com - login, register, account_db (users)

and I have one problem, on the forum, users must can to add posts, and then when render posts list, I have to take user data with each post: username, avatar, etc., but since users and posts are in different databases, how can I do this?

I can't understand how it's release, I think getting users by user_ids from posts via API, but it's too many requests

Cardinal
  • 1
  • 1
  • 1. read account_db directly from the forum project. 2. the http api for query is provided by the account project, and user_ids is used to query in the forum. 3. account project provide the client package using the protobuf. 2 and 3 should use internal networks to avoid unnecessary domain name resolution and bandwidth usage – Trock Aug 29 '23 at 06:23
  • @Trock I'm sorry, but reading db of another microservice is a terrible suggestion and an anti-pattern. You're essentially telling him to break rules of isolation and autonomy that the whole concept of microservices is built around. What he should do is use a message queue and cache replica of whatever data he fetches, so he can reduce network traffic at cost of eventual consistency (https://en.wikipedia.org/wiki/Eventual_consistency). Well, that or straight database replication. – JTinkers Aug 29 '23 at 13:35

1 Answers1

0

Okay, let's do this one by one:

On the forum, users can add posts

I'm assuming this is a question about Auth, in which case you can make a request via MessageQueue to the AccountService for permissions of this specific user. This can be heavy on networking if you have a lot of traffic - but this can be optimized by caching every user and his permissions in ForumService.

Additionally, you could implement a Gateway API that performs initial fetching of users upon first requqest and then propagades user data further into underlying services.

When you fetch and render a list of posts, user data is needed, such as: username, avatar, etc., but users are located in different services - so, how can I access them?

Same thing applies as with the auth. You can fetch these users one by one and cache them for however long you deem necessary. Sure, you'll temporarily lose some data consistency, but you'll achieve availability and lower network traffic (Eventual Consistency). You can also make AuthService emit UserChangedNotification and reduce inconsistencies to absolute minimum.

https://obeycode.com/articles/7/microservices-in-simple-terms---introduction

JTinkers
  • 1,631
  • 9
  • 18