List functional requirements for the system (Ask the chat bot for hints if stuck.)...
System Behaviour
List non-functional requirements for the system...
CAP
System prioritises availability more than consistency. The comments may appear a bit later but system is available for reads
Estimate the scale of the system you are going to design...
Define what APIs are expected from the system...
GET /1/threads?threadId={id}
Response : 200 OK
Response Body : [one or more Thread instances]
Get a paginated list of threads with parameters,
GET /1/threads?cursor={timestamp}&limit=50
Response : 200 OK
Response Body : {
threads:[],
nextCursor: <timestamp>
}
POST /1/threads
Request Header: {
Authorization: Bearer <JWT token>
}
Request body: {
A thread instance
}
Response: 201 CREATED
POST /1/threads?threadId={id}/comments
Request Header: {
Authorization: Bearer <JWT token>
}
Request Body: { a Comment instance }
Response: 201 Created
Response Body: {the created comment instance}
get top-level comments of a thread, if cursor is specified then system returns comments after the specified timestamp and steps through 100 comments at a time. If cursor is not specified then first 100 comments with its sub comments
GET /1/threads/{threadId}/comments/cursor={timestamp}&limit=100
Response : 200 OK
Response Body: {one or more Comment instance, each comment instance might contain sub comments in its children attribute}
Get nested comments of a comment
GET /1/comments/{commentId}/replies?cursor={timestamp}&limit=20
Response : 200 OK
Response Body: {one or more Comment instance, each comment instance might contain sub comments in its children attribute}
PUT /1/comments/{commentid}
Request Header: {
Authorization: Bearer <JWT token>
}
Request Body: {content of the comment}
Response : 200 OK
Response body: {updated Comment instance}
PUT /1/comments/{commentid}
Request Header: {
Authorization: Bearer <JWT token>
}
Response : 204 NO CONTENT
Response body: {updated Comment instance}
The cursor query parameter may be ISO timestamp or UUID of a 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...
System deployed multiple database nodes with replication factor = 3, meaning every write is copied to 2 more nodes, making it 3 copies. When a node goes down, the one or the replica can serve the data. Writes are eventual consistent and synchronised in the background. Helps in,
Threads are sharded by threadId making it distributed to a database node. "Write Node = hash(threadId) % N nodes" . this evenly distributes data to N nodes
In a node, the data is partitioned by threadId to enable parallel read/writes. Threads and related comments resides in same partition optimising access patterns.
When a thread/comment is written it's replicated to two more database nodes for redundancy, disaster recovery and high availability .
The reads are relatively higher than writes, hence the hot threads are cached. When a thread goes viral, the thread and its comments are cached into in-memory cache memcache distributed to multiple nodes. The data is sharded with threadId. The cache uses LRU eviction strategy and cache is updated asynchronously via a message bus.
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...
Explain how the request flows from end to end in your high level design. Also you could draw a sequence diagram using the diagramming tool to enhance your explanation...
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...
Explain any trade offs you have made and why you made certain tech choices...
Try to discuss as many failure scenarios/bottlenecks as possible.
What are some future improvements you would make? How would you mitigate the failure scenario(s) you described above?