Design a Scalable News Feed System

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2018-07-26

Problem Statement

You are asked to design a News Feed System where users can:


•Post content (text, images, videos).
•Follow friends/people/companies.
•See a personalized feed of posts, ranked by relevance (recency, engagement, relationships, etc.).
•Scale to hundreds of millions of users and billions of posts daily.


✅ Functional Requirements


1.Post Creation
•Users can create posts that are visible to their followers.
2.Feed Generation
•Retrieve posts from people/pages a user follows.
•Support infinite scroll (pagination).
3.Ranking & Personalization
•Prioritize posts by freshness, user affinity, engagement signals, ML models.
4.Engagement Actions
•Likes, comments, shares, reshares → should reflect in near real-time.
5.Notifications
•Notify when friends post or engage with your content.


❌ Non-Functional Requirements


•Scalability: Must handle millions of concurrent users and billions of feed requests/day.
•Low Latency: News Feed API should respond in <200 ms.
•Freshness: Posts and engagement should appear within seconds.
•High Availability: 99.99% uptime, fault-tolerant across regions.
•Consistency: Eventual consistency is acceptable (slight delays in propagation).


🧱 Core System Components


1.Post Service
•Stores new posts in a distributed storage (Cassandra, DynamoDB, HBase).
2.Social Graph Service
•Stores relationships (followers/following).
•Optimized for quick graph lookups.
3.Feed Generation Approaches
•Pull Model (On-Demand): Fetch posts at read-time from followees.
•Push Model (Fan-out-on-write): Push new posts to followers’ feeds at write-time.
•Hybrid Model: Popular users → pull, normal users → push.
4.Ranking Service
•Scores posts based on:
•Recency.
•Engagement (likes/comments).
•User affinity (how often user interacts with poster).
•Content type.
•Supports ML-based ranking.
5.Caching Layer
•Redis/Memcached to serve hot feeds quickly.
6.Streaming Pipeline
•Kafka/Flink/Samza for event-driven feed updates (new posts, likes, comments).
7.Search & Indexing
•ElasticSearch/Lucene for retrieving trending posts or hashtags.
8.Feed API Gateway
•Handles pagination, filtering, and personalization per user.


🔄 Flow Example


1.User A posts → stored in Post DB → sent to Kafka.
2.Feed Service:
•If small fanout → push to followers’ feed caches.
•If large fanout (celebrity) → store in Post DB only, followers pull later.
3.User B opens app → Feed API fetches:
•Cached feed (Redis) OR generated candidates.
•Ranked posts via Ranking Service.
•Returned in <200 ms.


⚖️ Trade-offs


•Push vs Pull
•Push = fresh but expensive for celebs with millions of followers.
•Pull = cheaper for celebs but increases read-time latency.
•Freshness vs Performance
•Precomputed feeds = faster, but less fresh.
•On-demand = fresher, but heavier at scale.
•Storage
•Billions of posts/day → partitioning by userID + time.
•Ranking Complexity
•Heuristics scale easily, ML gives better relevance but costs compute.


🧪 Open Discussion Points


•How to scale feed generation for celebrities with 100M followers?
•How to handle spam filtering & abusive content moderation?
•How to support multi-region deployments while keeping feeds consistent?
•How to design A/B testing hooks for new ranking algorithms?


👉 This problem tests system design for social platforms, scalability, caching, event pipelines, ranking ML, and trade-off reasoning — a classic FAANG-style interview question.

Solution

Comments