Social feed (Instagram-style)

Users, follows, posts and likes: a feed model with the Observer pattern for notifications and a strategy for ordering.

On the roadmap:Software Design · step 6: Low-level design: games and apps

Lesson 15 of 16 · 10 min read · quiz · exercise

The problem

Users follow each other, publish photo posts, like and comment, and see a home feed of posts from the accounts they follow. This lesson models the classes and rules. Scaling the feed (fan-out strategies) is covered in the news-feed lesson of the classic designs course.

Entities

Figure SDA-LLD-15-1 · Social feed classes · © forcsguy.com

Publishing and the feed

Figure SDA-LLD-15-2 · Publishing a post · © forcsguy.com

interface FeedStrategy { build(userId: string, limit: number): Post[] }

const newestFirst: FeedStrategy = {
  build(userId, limit) {
    const followees = follows.acceptedFolloweesOf(userId);
    return posts.byAuthors(followees).filter(p => canView(userId, p))
      .sort((a, b) => b.createdAt - a.createdAt).slice(0, limit);
  },
};

function canView(viewer: string, p: Post): boolean {          // one place for visibility rules
  const author = users.get(p.authorId);
  return !author.isPrivate || author.id === viewer || follows.isAccepted(viewer, author.id);
}

Putting visibility in one function means the feed, profile and search all agree about who may see what.

Likes and comments

  • A like is unique per (user, post); enforce it with a uniqueness rule, not just a check, so double taps do not create two likes.
  • Keep a like count on the post for fast display and update it with each like or unlike, accepting that the count may briefly lag.
  • Comments are ordered by time and can have a parent id for replies.

Design notes

  • Pagination uses a cursor (the time or id of the last item) so new posts do not shift pages.
  • Deleting a post must remove it from feeds and counts; a soft delete flag filtered at read time is the usual first step.
  • Blocking and muting are visibility rules too.
  • Extensions: stories with expiry, hashtags, tagging, search, and a ranking strategy that mixes recency with interest.

Real-world reading

Check your understanding

Question 1 of 3

0 correct so far

How should the home feed be ordered in a first version?

Go deeper

  • Design the news feed (classic system designs course) by This site: for fan-out on write versus read at scale

© 2026 forcsguy.com. Original lesson text and diagrams. Books and sites linked above belong to their authors.