List functional requirements for the system (Ask the chat bot for hints if stuck.)...
List non-functional requirements for the system...
Estimate the scale of the system you are going to design...
Number of estimated users - 100M
Read :write ratio - 1:100
Number of comments per day - 1M
Size per comment - 512 bytes.
Per day size - 512MB.
3 year projections - 512 * 1000 - 512 GB.
TPS during morning - 1M tps
TPS after work - 10 M TPS
Define what APIs are expected from the system...
list_replies(post_id, page_size, marker) :
Makes a GET http call to /threads/{threadId}
Thread can either be associated with a post or a comment itself. Paginated API sorted by relevance.
post_comment(post_id, user, message, post_anonymously) :
Makes a Post http call to /threads/{parentPostId}
For posting replies on any thread including setting to post anonymously.
post_update(post_id, user, engagementType) :
Makes a Post http call to /threads/{postId}
Users can like the post or any comment. This api can also be used to update previously posted comment.
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...
We'll track data in 2 separate tables.
Depth field in Thread table is going to help us limit nesting of comments.
Design consists of 6 components
Overall there are 4 flows.
Real time notifications
There are two ways to send notifications, either we can have all clients poll for new updates frequently which can resource consuming especially on mobile devices since our users can be on different platforms. Instead we choose to implement a 2 way communication where users/ devices register to receive notification and then updates are delivered asynchronously efficiently.
Supporting nesting of comments
Hierarchical structure of comments is maintained by storing both parent post reference and the root post reference along with the depth of the current post in relation to the root post. Comments will be stored/retrieved in the order seen by the comments service. Customers only see comments from one level lower along with metadata to see if replies exists on any of the comments giving users a chance to view them as well.
Partitioning / Sharding
Data in both DB and cache will be partitioned by posts Id which allows horizontal scaling of capacity since all relevant data will be stored within single node.
Database
We're using NoSQL data base like Cassandra / DynamoDB here to support horizontal growth of data.
Read replicas
Each shard will be replicated to support higher availability.
Real time notifications
Using WebSockets to maintain persistent 2 way communication instead of long polling.
Pagination
Avoids loading too much data and also avoids overloading systems at once.