When User Add a comment the request along with details will be first hit to the API gateway where it gets routed to the service and a specific instance of the service.
Once the comment details reach to the backend Service it gets store in the relational database table. And the parent connect count gets updated to +1.
While inserting the data into database table it also verify the depth of the comment if depth of the comment id greater than configured allowed depth then set the depth of the new comment as the highest one.
The Recently added comment will be added/updated to the Radis Cache and the message will be sent to the messaging queue for the notification.
Comment Read Service
When lookup for a comment for a specific post the request goes to the Backend Service through the API gateway where it routed to the appropriate instance of the read service.
The comments will be loaded in a pagination fashion where first it will load only the first 20 parent(whose parentid will be null) comments using a path field.
Once your click on see reply of option of the parent comment the next request will be generated and get all the first level comment of the parent comment using path column (path like "parentid/%")
This is how can load the comments of every hierarchy of the parent comment.
Notification Service
Notification service listen to the queue where consumer write service push the message after successfully insert into database.
And it will be push to the user if WebSocket is open or will be sent as a notification to the user.
Detailed Component Design
There are 3 main service in this system
Comment write Service
Comment Read Service
Notification Service
Post Comment Flow
Here user will add a comment or reply to the comment
Once user post the comment the request first hit to the API gateway.
API Gateway route the request to the appropriate instance of comment write service.
The request contains the user id, comment details and Unique UUID for a comment. This UUID will use to make the comment unique and can avoid the same comment to be persisted multiple times.
Once request reached to the instance of comment write service it will form the path of the comment as mentioned below
Parent1CommentUUID/Parent2CommentUUID/
Persist the data into the database once new comment added into the table update the reply count of the parent comment by one.
Once both the operation completed then invalidate Radis cache for the post.
And Sent the notification event to the Radis.
Notification Service
It consumes the notification sent after persisting the Post.
Once the notification consume first find the user id of the parent comment for which reply is given. And the notification will be form and publish to the user specific queue.
Also notification will be persisted into the database so that if user is offline then the notification can be retry once your back to online.
Read The post comments
Once user sent the request to get the comments of the specific post the request first goes to the API Gateway.
API gateway route the request to the appropriate read service instance.
Once it reached to the read service instance it first lookup for comment inside cache. if found the match then return the Comment JSON back to the user. if there is no match in the cache then as a fail over it lookup for a parent comments of a post. the retrieved comments then transform into the JSON Tree in the application and return to the user.
The Comments will be retrieve using the path column of the database table.