Design Instagram

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2019-11-13

Problem Statement

Design the core backend system of Instagram, focusing on user management, posts, likes, comments, follow system, and feed generation. Ensure scalability, extensibility, and efficient operations for millions of users.


Functional Requirements (FR)


User Management


•Users can register/login, manage profiles (username, bio, profile picture).
•Follow/unfollow other users.


Post Management


•Users can create posts with images/videos, captions, and tags.
•Posts have unique IDs and timestamps.
•Edit or delete posts.


Feed


•Users see posts from followed users in reverse chronological or algorithmic order.
•Support pagination and infinite scroll.


Interactions


•Users can like/unlike posts.
•Users can comment on posts.
•Support notifications for likes, comments, and follows.


Optional Features


•Stories, reels, search, explore page.
•Hashtags and trending topics.
•Media processing (compression, thumbnails).


Non-Functional Requirements (NFR)


•Performance → Low latency for feed generation and interactions.
•Scalability → Handle millions of users and posts.
•Consistency → Ensure likes, comments, and follows are consistent.
•Availability → System should be highly available.


Core Entities & Attributes


User


•userId, username, email, password, profilePicture, bio, followers[], following[]


Post


•postId, userId, mediaUrl, caption, timestamp, likes[], comments[]


Comment


•commentId, postId, userId, text, timestamp


Like


•userId, postId, timestamp


Feed


•userId, list of postIds (ordered by timestamp or relevance)


Core Components


UserService

•Manages users, authentication, follows/unfollows.

PostService

•Handles post creation, deletion, editing, and media storage references.

InteractionService

•Manages likes, comments, and notifications.

FeedService

•Generates and caches user feeds.
•Supports pagination and personalized ranking.

MediaService

•Handles image/video storage, compression, and thumbnails.

NotificationService

•Sends notifications for interactions like likes, comments, follows.


APIs / Operations


User APIs


•createUser(name, email, password)
•followUser(followerId, followeeId)
•unfollowUser(followerId, followeeId)
•getUserProfile(userId)


Post APIs


•createPost(userId, mediaUrl, caption)
•deletePost(postId)
•getPost(postId)
•getUserPosts(userId, page, limit)


Interaction APIs


•likePost(userId, postId)
•commentPost(userId, postId, text)
•getPostComments(postId)


Feed APIs


•getFeed(userId, page, limit)


LLD Design Goals


•Class Diagram → User, Post, Comment, Like, Feed, Services.


Design Patterns:


•Observer → Notify users for likes, comments, and follows.
•Factory → For creating posts and comments.
•Singleton (Optional) → For caching or feed generation services.
•Trade-offs →1. Real-time feed vs. precomputed feed.

2.Database normalization vs. denormalization for performance.

5.Concurrency Handling →

•Thread-safe like/comment counters.
•Atomic operations for follow/unfollow.


Solution

Comments