Skip to content

Facebook News Feed

Quick Reference Guide for System Design Interviews


Problem Statement

Design Facebook's News Feed system that aggregates and ranks posts from friends, groups, pages, and ads, presenting a personalized feed to each user.


Requirements

Functional Requirements

  • Display personalized feed of posts from friends, pages, groups
  • Support various content types (text, images, videos, links)
  • Like, comment, share functionality
  • Real-time updates for new posts
  • Infinite scroll with pagination

Non-Functional Requirements

  • Latency: < 500ms feed load
  • Availability: 99.99%
  • Scale: 2B users, 500M DAU
  • Freshness: New posts appear within minutes

Back of Envelope Estimation

Capacity Estimation


High-Level Architecture

Facebook News Feed Architecture


Feed Generation Approaches

Facebook's Hybrid Approach

Facebook's Feed Architecture


Ranking System

Feed Ranking


Social Graph (TAO)

TAO Architecture


Feed Service Architecture

Feed Service Flow


Data Models

-- Posts table (sharded by author_id)
CREATE TABLE posts (
    post_id         BIGINT PRIMARY KEY,  -- Snowflake ID
    author_id       BIGINT NOT NULL,
    content_type    ENUM('text', 'photo', 'video', 'link'),
    content         TEXT,
    media_ids       JSON,
    privacy         ENUM('public', 'friends', 'only_me'),
    like_count      INT DEFAULT 0,
    comment_count   INT DEFAULT 0,
    share_count     INT DEFAULT 0,
    created_at      TIMESTAMP NOT NULL,

    INDEX idx_author_time (author_id, created_at DESC)
);

-- Feed actions (for ranking features)
CREATE TABLE feed_actions (
    user_id         BIGINT NOT NULL,
    post_id         BIGINT NOT NULL,
    action_type     ENUM('view', 'like', 'comment', 'share', 'hide'),
    dwell_time_ms   INT,
    created_at      TIMESTAMP NOT NULL,

    INDEX idx_user_time (user_id, created_at DESC)
);

-- User affinity scores (computed offline)
CREATE TABLE user_affinity (
    user_id         BIGINT NOT NULL,
    target_id       BIGINT NOT NULL,
    target_type     ENUM('user', 'page', 'group'),
    affinity_score  FLOAT,
    updated_at      TIMESTAMP,

    PRIMARY KEY (user_id, target_type, target_id)
);

Caching Strategy

Caching Layers


Real-Time Updates

Real-Time Updates


Ad Integration

Ad Integration


Interview Discussion Points

  1. Pull vs Push for News Feed?
  2. Facebook uses Pull with pre-aggregated candidates
  3. Unlike Twitter, ranking is critical
  4. Fresh posts can wait minutes

  5. How does ranking work?

  6. ML model predicts engagement
  7. Features: affinity, recency, content type
  8. Diversity enforced post-ranking

  9. How do you handle the social graph?

  10. TAO: Distributed graph database
  11. Cached aggressively (99% hit rate)
  12. Sharded by user ID

  13. How do you handle real-time updates?

  14. WebSocket for connected users
  15. Only high-affinity updates pushed
  16. "New posts" badge, user refreshes

  17. How do you scale feed generation?

  18. Pre-compute candidate pool
  19. Cache ranked feeds (short TTL)
  20. Parallel fetching from shards

  21. How do you mix ads with organic?

  22. Ad service parallel to feed
  23. Auction-based ranking
  24. Inserted at fixed positions

Quick Reference for System Design Interviews