List functional requirements for the system (Ask the chat bot for hints if stuck.)...
List non-functional requirements for the system...
Define what APIs are expected from the system...
You should identify enough components that are needed to solve the actual problem from end to end. Also remember to draw a block diagram using the diagramming tool to augment your design. If you are unfamiliar with the tool, you can simply describe your design to the chat bot and ask it to generate a starter diagram for you to modify...
The system should include the following items:
Client (Publisher + Receiver): The client plays two different roles in the system: comment publishers and comment receivers.
Servers (Dispatcher + Gateway Server): The servers play two roles in the system: the dispatcher broadcasts the comments to appropriate servers (and subsequently receivers), and the Gateway Server forwards them to the receivers.
Storage (Endpoint Store + comments store + in-memory subscription store): The system has three storage locations—the comment store stores comments, user information, etc. The Endpoint Store contains information on which gateway servers are subscribed to what topics. The In-memory subscription store resides in the Gateway Server and contains information on which receivers want to receive comments on which topics.
Client: Let's call normal read path handler client - client
Read Server: Handles read path, will read comments from comments store and returns to client, also update endpoint store and gateway server as needed
Here's the way to handle Create, Update and Delete (Write Path)
Because these operations mutate state and require live updates to other users, they follow the entire flowchart.
POST, PATCH, or DELETE request as the Publisher.topic_idexists, calculates the new nested path, and inserts the record into the Comments Store.is_edited, and updates the Comments Store.thread_id: 123.COMMENT_ADDED or COMMENT_DELETED) to the appropriate Gateway Server.This architecture is heavily optimized for writes and real-time pushes. For standard Retrieve operations (e.g., a user loads the page for the first time), the flow is slightly different:
GET request to a Read API (which could sit alongside or within the Publisher block) that queries the Comments Store (or a Redis cache sitting in front of it) directly to fetch the paginated, nested comment tree.To handle pagination, we need to use cursor-based pagination. Specifically, the initial comments will return the first page and a cursor, and subsequently when user scroll or move to next page, cursor is used to fetch subsequent pages.
To handle lazy-loading of nested comments, the client can send a request to the GET /comments/{topic_id}/{comment_id}/replies API, and the server reads from the comments store and returns the list. Besides, the server also updates the Gateway Server and the Endpoint Store to subscribe to the newly loaded reply path.
Real-time-notification: Once a client read a specific thread, or a specific reply with replies, they subscribe to this topic via websocket or server-side events. Whenever a new reply is available, the dispatcher will read subscriptions from endpoint store, figure out what gateway server needs it, and then the gateway server will forward the update through server-side events to the interested receivers.
API-gateway: This gateway handles authentication of users. It verifies that the user is logged in and also have enough permission to read / post in certain threads. Once that's done, it forwards these information to dispather or read server to further handles the requests.
Dig deeper into 2-3 components and explain in detail how they work. For example, how well does each component scale? Any relevant algorithm or data structure you like to use for a component? Also you could draw a diagram using the diagramming tool to enhance your design...
Gateway Server: In order to achieve real-time comments refresh, each gateway server maintains SSE connections to all receiving clients. Each server also contains an in-memory subscription store, so that it does not fan out comments to all receivers unnecessarily.
Endpoint Store: This storage is stand-alone so that dispatchers does not need to maintain a state, therefore, the publishing load can be flexibly distributed across all dispatchers.
Defining the system data model early on will clarify how data will flow among different components of the system. Also you could draw an ER diagram using the diagramming tool to enhance your design...
Topics Table
Comments Table
Users Table
Upvotes Table
Downvotes Table