Backend:
Main components:
## Read path
Frontend asynchrounously calls REST API while displaying skeleton -> Backend checks cache or queries database -> database retrieves and returns corresponding data -> Backend organizes query results and send back to frontend -> frontend update view with new data
## Write path (make a like)
Frontend instantly updates UI, play a like animation, then make async REST API call -> backend relays to database with a query "IN LIKE_TABLE INSERT commentId by userid" -> database negotiate with remote peers throgh raft protocol, retry until successful, then commit
Deep dive into 2-3 key components. Explain how they work, how they scale, discuss tradeoffs, capacity, and any relevant algorithms or data structures.
## sharded database.
All of the tables above should be sharded for scalability. Because usage patterns are highly uneven: some posts receive a flood of likes/comments while most remain dormant
## Feed update
During a feed update, the backend server does the heavy lifting. It needs to make multiple queries to gather posts from followees that the current user follows, and run a ranking algorithm to determine the order. This result also should be cached and only invalidate upon new posts from any of the followees. Or to make it more perfromant, we can even append new posts in the front of the list, and run the ranking algo even less often.
## Latency consideration
Use caching agressively on hot paths such as like counts or comment history. We should have a separate table to store like counts of posts, instead of gathering counts from the postId -> like table every time. The userId->interaction mapping can be updated more often, since it's sharded and with few data races. But the like counter is synced less often, and the lag is usually tolerable for users. It's extremely expensive to update a single global counter on a viral post.
The goal is:
## TopK ranking system
We should have a separate service that periodically rank the hottest new posts. There is less latency constraint on this functionality, and it's natural to decouple it from the REST API server. To rank the posts, we would need to make data queries to like counter and comment counter. We can even run some machine learning algorithm to analyze the velocity of like/comment increase to identify viral posts to recommend to the users.