DevPath · Learn to code ESPTEN

Project: social network

The feed and the N+1 problem

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:

  1. Find out whom the user follows (traverse the edges of the graph).
  2. Keep the posts whose author_id is in that set, and order them by date descending.

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.

Put this into practice

DevPath is a hands-on course: you read the theory here; in the app you put it into practice with exercises that really run, offline.

Start free in the app →
← The plan: users, followers and the social graphView the module →