Practice/Snapchat/Design Twitter
Design Twitter
System DesignMust
Problem Statement
Design a social media platform where users post short text updates, follow other accounts, and consume a personalized home timeline that blends followed content with product recommendations and in-stream shopping. Think of Twitter's real-time feed mechanics combined with an integrated commerce experience -- users scroll through tweets, see promoted products, tap to view details, add items to a cart, and check out without leaving the app.
This problem tests two traditionally separate domains in one design. The social feed side requires low-latency fanout to millions of followers, real-time delivery of new posts, and a ranking layer that balances recency with relevance. The commerce side introduces transactional workflows -- inventory reservation, payment authorization, order fulfillment -- that demand strong consistency guarantees typically at odds with the eventual consistency acceptable for social metrics like like counts and follower numbers. Interviewers want to see you draw clear service boundaries between these domains and articulate where consistency models differ.
At Snapchat-scale, the system must handle hundreds of millions of daily active users, many of whom follow high-profile accounts with tens of millions of followers. Posting a single tweet from such an account triggers an enormous write amplification problem if you fan out eagerly. Meanwhile, flash sales promoted in popular tweets create thundering-herd purchase attempts that can oversell inventory in seconds. Navigating these simultaneous pressures is what makes this design compelling.
Key Requirements
Functional
- Tweet creation -- users can post short text updates with optional media attachments and product tags that link to purchasable items
- Home timeline -- users see a personalized feed of tweets from followed accounts and algorithmically recommended content, updating in near real-time
- Product discovery and purchase -- users can browse tagged products, view details, add items to a cart, and complete checkout with payment processing
- Search and trending -- users can search tweets and products by keyword, and the platform surfaces trending topics and popular products
- Notifications -- users receive alerts for mentions, likes, retweets, order confirmations, and shipping updates
Non-Functional
- Scalability -- support 500 million daily active users with peak home timeline reads of 1 million requests per second
- Latency -- home timeline loads in under 200 milliseconds at the 95th percentile; product detail pages render in under 150 milliseconds
- Reliability -- zero lost tweets or orders; payment and inventory operations guarantee exactly-once semantics through idempotency
- Availability -- the social feed path maintains 99.99 percent uptime; the commerce path tolerates brief degradation with queued order processing
What Interviewers Focus On
Based on real interview experiences, these are the areas interviewers probe most deeply:
1. Feed Fanout Strategy
When a user with 30 million followers posts a tweet, how does the system deliver it to every follower's timeline without introducing minutes of delay or overwhelming the database? This is the defining challenge of any Twitter-like system.
Hints to consider:
- Use a hybrid fanout approach: for accounts with fewer than a threshold of followers (say 10,000), fan out on write by pushing the tweet ID into each follower's timeline cache at post time
- For high-follower accounts, fan out on read by merging their latest tweets into the timeline at query time, avoiding massive write amplification
- Store precomputed timelines in Redis sorted sets keyed by user ID, with tweet IDs scored by timestamp for efficient range queries
- Discuss how you detect which users are "active" and skip fanout for dormant accounts to reduce unnecessary writes
2. Commerce Transaction Integrity
When a promoted product goes viral and thousands of users attempt to purchase simultaneously, the system must prevent overselling while keeping the checkout experience responsive. Interviewers want to see saga patterns and idempotency in action.
Hints to consider:
- Reserve inventory atomically when a user adds to cart using a compare-and-swap operation on the inventory count, with a TTL-based hold that auto-releases if checkout is abandoned
- Implement the checkout flow as a saga: reserve inventory, authorize payment, create order, confirm inventory deduction -- with compensating actions at each step for rollback
- Use idempotency keys on every payment API call so that network retries never produce double charges
- Queue purchase attempts through a rate-limited pipeline during flash sales, providing users with position feedback rather than failing fast
3. Real-Time Delivery and Notifications
Users expect new tweets and engagement notifications to appear within seconds. Polling at scale is expensive, so interviewers look for push-based architectures.
Hints to consider:
- Maintain persistent WebSocket connections from clients to a notification gateway service for push delivery of new tweets, likes, and order status updates
- Use Redis pub/sub or a lightweight message broker behind the gateway to route events to the correct user connections
- Implement exponential backoff polling as a fallback for clients that cannot maintain WebSocket connections
- Separate notification priority tiers: order confirmations and payment alerts are high priority (push immediately), while social engagement (likes, retweets) can be batched
4. Search and Recommendation Infrastructure
Discovery drives engagement on both the social and commerce sides. Interviewers explore how you index tweets and products for fast search and how you rank the home timeline beyond pure chronology.
Hints to consider:
- Index tweets and products in Elasticsearch with fields for text content, hashtags, product category, and engagement signals, updated via change data capture from the primary store
- Rank the home timeline using a lightweight ML model that scores candidate tweets by predicted engagement (likes, replies, purchases), blending followed content with recommended posts
- Surface trending topics by running a streaming top-K computation over recent tweet volumes, similar to an ad click aggregation pipeline
- Cache search results for popular queries with short TTLs to reduce Elasticsearch load during trending events