List functional requirements for the system (Ask the chat bot for hints if stuck.)...
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...
User { create thread } > Load Balancer - L7 > API Gateway > ThreadService > Database > CDC: Stream > CDC Processor > Kafka > Flink {verify/filter/Top K adjustments} > Cache update > Notification Service > Notify user
User {Read threads} > Load Balancer - L7 > API Gateway > ListignService > Top 30 threads from cache
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...
When multiple users posts comment on a thread at the same time, these requests might hit different nodes of ThreadService/CommentService in Kunernetes, each of these contents are updated with an unique identifier (threadId or commentId) which is a twitter-snowflake id, this helps in arranging by time when the system receives the comment/thread. This avoid collisions in database, even if two comments has the same created/updated timestamp set by two different nodes and yet the unique identifier is sortable
When a thread goes viral, the reads/writes will spike for the thread this will hit the microservices in the Kubernetes pods, the auto-scaling of Kubernetes is utilised for upscaling the no of nodes and auto-balance the traffic. Each node knows how to generate unique identifiers by itself.
The retention period for a thread is 90 days, post 90 days the threads and its comments are moved to a hot S3 storage and written in Parquet format. This data can be queried for analytics or by user(search old thread), using Apache Trino + SQL. Further the data in S3 are moved to warm > archive with 1yr retention period
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?