Back🌐

Designing a Social Network Feed System

AWAKENING0 / 100
[░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░]0%

Designing a Social Network Feed System

High Level Design·advanced·~30 min read

  • system-design
  • twitter
  • feed
  • timeline
  • distributed-systems
  • hld

What you'll learn

  • Problem Statement

    Design a social media platform where users can post short messages, follow others, and view a personalized home timeline aggregating posts from all followed accounts. The system handles billions of daily post views with sub-200ms timelin…

  • Requirements

  • Functional Requirements

    1. Post short messages (280 characters + optional media) 2. Follow/Unfollow other users 3. Home Timeline — aggregated feed of posts from followed users 4. User Timeline — all posts by a specific user 5. Search posts by keyword/hashtag 6.…

  • Non-Functional Requirements

    1. High availability — service should never go down 2. Low latency — timeline generation What breaks next — the timeline JOIN itself does not scale by adding replicas. Each replica still has to do the same fan-in sort per request, and th…

  • Stage 3 — Bottleneck #1: Timeline generation at read time (the fan-in problem)

    At 325K timeline reads/sec, doing a JOIN across follows and posts on every request is architecturally impossible — no amount of read replicas fixes it. A user following 200 people needs the top 20 posts merged from 200 possibly-scattered…

  • Stage 5 — Bottleneck #3: Media, search, and cross-region reads

    Two smaller-but-real problems appear once fan-out is under control: 1. Media — post attachments (images/video) are 40× the size of post text. Serving 24 TB/day of media through the API tier is wasteful; that traffic never touches busines…

  • Cost vs Complexity — When to Pick What

    | Concern | MVP choice | Growth choice | Enterprise choice | |---------|------------|---------------|-------------------| | Compute | Single VM, monolith | K8s deployment, N API pods | Multi-region K8s, autoscaling | | Database | Managed…

  • Migration Path (MVP → Enterprise)

    The ordering matters. Each step should be shippable behind a feature flag, measurable, and reversible. 1. Introduce read replicas — no code change to writers; readers gradually shift via a config flag. Measure replication lag before maki…

  • Fault-Tolerant Key Generation

    - Each server independently generates sequences - No coordination/lock needed between them - If one dies: other continues (IDs have gaps, uniqueness preserved) - Both out of sync? Fine — uniqueness comes from even/odd split, not from syn…

  • Timeline Generation (The Core Challenge)

  • The Problem

    User X follows 200 people. Loading their timeline requires: 1. Get list of 200 followed accounts 2. For each, find their latest posts (scattered across shards) 3. Merge and rank 4,000+ posts 4. Return top 20 At 325K timeline reads/sec, t…

  • Feed Storage Data Structure

    The timeline cache for each user can be modeled as: A LinkedHashMap is chosen because it provides both random access (jump directly to any feed item by ID for cursor-based pagination) and sequential iteration (traverse items in insertion…

  • Solution: Pre-Generation (Fan-out)

  • Fan-out Approaches (Critical Trade-off)

    | Approach | Mechanism | Best For | Problem | |----------|-----------|----------|---------| | Fan-out on Write | Push to all followers on post creation | Users with 50% failure rate, stop routing to it ---

  • Extended Features

  • Retweets

  • Who to Follow Suggestions

    ---

  • Monitoring and Metrics

    | Metric | Alert Threshold | Why | |--------|----------------|-----| | Timeline latency p99 | > 200ms | SLA violation | | Fan-out lag | > 5 seconds | Followers see stale timelines | | Replication lag | > 5 seconds | Reads may be too stal…

  • Summary: Architecture Decisions

    | Decision | What it Solves | |----------|---------------| | PostID = epoch + sequence | Time-ordered primary key, no secondary index, even sharding | | Even/odd key-gen servers | Fault-tolerant ID generation without coordination | | Pos…

← back to High Level Design

Designing a Social Network Feed System — High Level Design | GoCrack | GoCrack