Design Instagram Feed – DB Schema, APIs, Optimizations, Celebrity Problem, Analytics Features

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2024-10-17

Problem Statement

Designing the Instagram Feed system, focusing on data modeling, APIs, scalability, and optimizations to support billions of daily users. The feed should display a mix of posts from people a user follows and suggested content, ordered by relevance and recency.


✅ Core Requirements


Feed Generation & Consumption


•Users can create posts (images/videos/captions).
•Followers should see posts in their feed with relevance-based ranking (recency, engagement, relationships).
•Infinite scrolling support.


APIs


•POST /createPost(userId, media, caption, metadata)
•GET /feed(userId, cursor, limit) → returns paginated feed.
•POST /like(postId, userId)
•POST /comment(postId, userId, text)
•GET /analytics(postId) → impressions, reach, likes, comments.

DB Schema Considerations


•Users Table – user profiles, follower/following lists.
•Posts Table – postId, userId, media URL, timestamp, metadata.
•Feed Table (optional denormalization) – precomputed feeds per user.
•Engagement Table – likes, comments, shares.
•Analytics Table – impressions, reach, engagement metrics.


⚡ Optimizations


•Celebrity Problem:
•Some accounts (e.g., celebrities with 50M+ followers) can generate massive fan-out.
•Optimize using pull-based feed generation for such accounts (fetch posts at read time instead of precomputing for all followers).
•Ranking & Relevance:
•Machine Learning-based ranking (engagement, user interactions, freshness).
•Hybrid of time decay + ML scoring.
•Caching & Latency:
•Cache hot posts in Redis.
•Store feed in sharded DBs or distributed key-value store for fast reads.
•CDN for serving media files.
•Pagination:
•Cursor-based pagination for smooth infinite scroll.


📊 Analytics Features


•Track post reach, impressions, unique viewers, engagement rate.
•Collect user interaction logs (likes, scroll depth, dwell time).
•Support real-time dashboards for creators & business accounts.


🚀 Constraints


•System should support hundreds of millions of DAUs.
•Low-latency feed retrieval (<200ms).
•Must handle high write throughput (millions of posts per minute).
•Consistency vs latency trade-offs: eventual consistency is acceptable for feed ordering.


Solution

Comments