Designing a social media feed like Twitter or Instagram is an advanced LLD problem that tests fan-out strategies, notification systems, and feed ranking. It's asked in senior SDE interviews at Meta, Twitter, and Indian product companies.
Core Entities
- User — profile, follower/following counts
- Post — content, media, author, timestamp, hashtags
- Follow — follower → following relationship
- Feed — ordered list of posts for a user
- Like / Comment — engagement on a post
- Hashtag — maps to trending topics
- Notification — like, comment, follow events
Fan-Out Strategy — The Core Design Decision
Two approaches, and you should discuss both in an interview:
// Fan-out on WRITE: when user posts, push to all followers' feed caches
class FeedService {
onNewPost(post: Post) {
const followers = this.followRepo.getFollowers(post.authorId);
followers.forEach(followerId => {
this.feedCache.prepend(followerId, post.id); // O(followers) on write
});
}
getFeed(userId: string): Post[] {
return this.feedCache.get(userId, 100); // O(1) on read
}
}
// Fan-out on READ: compute feed when requested (better for celebrities)
class LazyFeedService {
getFeed(userId: string): Post[] {
const following = this.followRepo.getFollowing(userId);
return this.postRepo.getRecentFrom(following, limit: 100);
}
}Interview answer: Hybrid — fan-out on write for normal users; fan-out on read for accounts with millions of followers.
Notification System with Observer
class Post {
private observers: PostObserver[] = [];
addObserver(o: PostObserver) { this.observers.push(o); }
like(user: User) {
this.likes.push(user);
this.observers.forEach(o => o.onLike(this, user));
}
}
class NotificationObserver implements PostObserver {
onLike(post: Post, liker: User) {
if (post.author.id !== liker.id)
this.notifService.send(post.author, `${liker.name} liked your post`);
}
}Feed Ranking Strategy
interface FeedRankingStrategy { rank(posts: Post[], user: User): Post[]; }
class ChronologicalRanking implements FeedRankingStrategy {
rank(posts, user) { return posts.sort((a,b) => b.createdAt - a.createdAt); }
}
class EngagementRanking implements FeedRankingStrategy {
rank(posts, user) {
return posts.sort((a,b) => (b.likes + b.comments*2) - (a.likes + a.comments*2));
}
}