What is the feed?
A user's feed (or timeline) is the list of posts of all the people they follow, normally ordered from the most recent to the oldest. It does not include (in its simple version) your own posts nor those of whom you don't follow.
To build it you need two steps:
- Find out whom the user follows (traverse the edges of the graph).
- Keep the posts whose
author_idis in that set, and order them bydatedescending.
In SQL that is a JOIN between posts and followers.
The N+1 problem
Imagine you already have the feed posts and, for each one, you want to show the name of its author. A naive solution is:
for (const post of posts) {
post.author = await loadUser(post.author_id); // one query per post!
}
If the feed has 100 posts, that is 1 query for the feed + 100 queries for the authors = 101 queries. This is the N+1 problem: one initial query and then N additional queries, one for each row. With many rows and network latency, the page becomes extremely slow.
The solution: batch loading
Instead of requesting the authors one by one, you collect all the author_ids
you need and request them all at once:
const ids = [...new Set(posts.map((p) => p.author_id))];
const authors = await loadMany(ids); // 1 single query
Then you build an index (a Map or an object) of id → author to
look up each author in constant time. You go from N+1 queries to 2. This
technique is the basis of patterns like DataLoader and of batching in GraphQL.