Newsfeed system design

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2025-04-02

Problem Statement

Design a scalable and real-time newsfeed system that displays personalized content (posts, images, videos, ads) to users based on their interests, social connections, and interactions.


Focus on system components, data flow, caching, and scalability.


Functional Requirements (FR)


User Management


•Users can register/login, follow/unfollow other users.
•Maintain user profile, preferences, and activity history.


Post Management


•Users can create, edit, and delete posts (text, images, videos).
•Support likes, comments, and shares.
•Maintain timestamps for posts.


Feed Generation


•Generate personalized feed based on:
•Users followed
•User interests and interactions
•Trending or popular content
•Display posts in reverse chronological or algorithmically ranked order.


Notifications


•Notify users about likes, comments, shares, or new posts from followed users.


Search & Discovery


•Search posts, users, or hashtags.
•Recommend trending content or users to follow.


Non-Functional Requirements (NFR)


•Scalability – Handle millions of users, posts, and interactions.
•Low Latency – Fast feed retrieval (<200ms for 90% of requests).
•High Availability – System should be available 24/7.
•Consistency – Ensure feed reflects recent user actions (likes, posts).
•Extensibility – Support new media types, ads, and feed ranking algorithms.


Core Components

Component Responsibilities

User Service Manage user profiles, connections (followers/following).

Post Service Create, edit, delete, and store posts.

Feed Service Generate and serve personalized feeds.

Ranking Engine Algorithmically rank posts based on relevance, recency, and engagement.

Cache Layer Cache feeds for fast retrieval (e.g., Redis, Memcached).

Notification Service Notify users about interactions on posts.

Search Service Search posts, hashtags, and users.

Analytics Service Track user engagement and trending content.

Database Layer Store users, posts, connections, likes, comments, and metadata.


Data Flow Overview


1.User A posts new content → Post Service stores the post → updates Feed Service.
2.Feed Service fetches posts from followed users → applies Ranking Engine → stores in Cache.
3.User B requests feed → Feed Service retrieves from cache (or generates dynamically if needed) → returns personalized feed.
4.User B likes a post → Post Service updates metrics → Notification Service notifies post creator.


Design Considerations


1.Feed Generation Strategies
•Pull Model → Generate feed on-demand (good for low-read users).
•Push Model → Precompute feeds for users (good for high-read users).
•Hybrid approach often used in production.
2.Caching & Performance
•Cache recent feeds or hot posts to reduce database load.
•Use CDN for media content (images/videos).
3.Database Design
•Separate storage for posts, user connections, and engagement metrics.
•Use relational DB for structured data; NoSQL for high-throughput feed storage.
4.Scalability
•Shard users and posts by ID to distribute load.
•Asynchronous processing for likes, comments, and notifications via message queues.
5.Ranking & Personalization
•Incorporate engagement, recency, and relevance for feed ranking.
•Use machine learning models for better personalization (optional).


✅ This HLD problem tests:


•Ability to design scalable social feed systems.
•Considerations for caching, feed generation, personalization, and notifications.
•Handling high concurrency, low latency, and large-scale data.


Solution

Comments