Social Network

Social Media Platform

Design the core of a social network: follows and friendships, posts with visibility rules, a news feed that mixes push and pull delivery, likes, blocks and private messages.

Overview

A social network lets people connect, share posts and read a feed of what the people they follow have posted. In a low level design interview the scope is the object model inside one service: users, the social graph (who follows whom, who is friends with whom), posts with a visibility setting, likes and comments, a feed, blocks, and direct messages.

The problem tests three things. First, whether you keep the social graph separate from posts and profiles, so each part has one job. Second, whether you know the two ways to build a feed (copy each new post to followers when it is written, or gather posts when the feed is read) and when to mix them. Third, whether privacy rules hold on every read path, including posts that were delivered before a friendship ended or a block was added.

A strong answer scopes the problem quickly, draws the graph and the post as separate classes, names the fan-out choice with its cost, and writes a program whose tests show a friends-only post hidden from a follower who is not a friend.

Scoping the problem

Seven short questions settle the scope. Each one changes a class or a code path, so ask them before you draw anything.

  • Follow (one way), friend (two way), or both? This decides the graph's shape and who sees friends-only posts. The usual answer is both: a friendship implies a follow in each direction.
  • Which visibility levels does a post have? Every read must check it. The usual answer is public, friends and private.
  • Is the feed newest first or ranked? If it is ever more than time, ranking must be pluggable. Start newest first, with ranking as a follow-up.
  • How many followers can one account have? This decides push, pull or a mix for the feed. Most accounts have few; a few have very many.
  • What does a block do? It must cut follows, friendships, feeds and messages. The usual answer is that it removes the link both ways and hides everything.
  • Are messages one to one or group chats? Group chats add a conversation entity, so keep the core to one to one.
  • Do we need persistence and many servers? That changes the storage layer, not the classes, so it is a follow-up, not the core.

Functional requirements

  • follow(a, b) and unfollow(a, b); request_friend(a, b), accept_friend(a, b) and unfriend(a, b), where accepting also adds a follow in both directions
  • create_post(author, text, visibility, now) with visibility public, friends or private
  • like(user) and comment(user, text) on a post; a user likes a post at most once
  • feed(viewer, now, limit, before_id): posts from followed accounts and the viewer, visibility checked, ranked, paged with a cursor
  • block(a, b) removes follows, friendships and pending requests both ways and hides posts and messages
  • send_message(sender, recipient, text) for one-to-one messages, refused between blocked users

Non-functional requirements

  • Posting cost stays bounded for accounts with very many followers (pull for those above a threshold)
  • Reading a feed does not scan every post in the system: inbox ids plus a few large accounts
  • Visibility and blocks are enforced on every read, so a later block or unfriend takes effect at once
  • Safe under concurrency: the graph, the network and each post have their own lock; likes are never lost
  • New ranking rules are new Ranker classes; the feed code does not change

Interview questions

  • An account has ten million followers. What happens when it posts?
  • Ben was Ana's friend when her friends-only post was pushed to his inbox. Then they unfriend. Can he still see it?
  • How do you add a ranked feed without rewriting feed()?
  • Two thousand people like the same post in one second. How do you keep the count right?
  • How do you page through a feed so new posts do not cause repeats?
  • How would this look with a real database behind it?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio