CAPACITY PLANNING
Users: 100M active users.
Tweets/day: ~500M → ~6K tweets/sec.
Followers graph: ~1B edges.
Home timeline reads: ~50K/sec.
Likes/retweets/replies: ~200K/sec.
POST /signup → register new user
POST /login → authenticate user
POST /tweet → post a tweet
GET /timeline/home → get home timeline
GET /timeline/profile/{user_id} → get profile timeline
POST /follow/{user_id} → follow a user
POST /unfollow/{user_id} → unfollow a user
POST /like/{tweet_id} → like a tweet
POST /retweet/{tweet_id} → retweet
POST /reply/{tweet_id} → reply to a tweet
GET /search?q=keyword → search tweets
GET /trending → get trending topics
scss
CopyEdit
user_id (PK), name, username, email, password_hash, bio, created_at
scss
CopyEdit
user_id, follower_id (composite PK)
scss
CopyEdit
tweet_id (PK), user_id (FK), content, media_url, created_at, like_count, retweet_count, reply_count
CopyEdit
user_id, tweet_id, created_at
scss
CopyEdit
user_id, tweet_id (composite PK)
scss
CopyEdit
user_id, tweet_id (composite PK)
bash
CopyEdit
notification_id, user_id, type, source_user_id, tweet_id, created_at, is_read
Main components:
Post Tweet
pgsql
CopyEdit
Client → API → Tweet Service → Save to Tweet DB → Timeline Service → Fan-out to followers → Update Timeline DB + Redis → Notify followers
View Home Timeline
vbnet
CopyEdit
Client → API → Timeline Service → Get from Redis (or fallback to Timeline DB) → Return tweets
Search Tweets
sql
CopyEdit
Client → API → Search Service → Query ElasticSearch → Return results
scss
CopyEdit
tweet_id (PK), user_id, content, media_url, created_at
Cassandra: for tweets High write throughput, horizontal scalability
Redis for timelines: Low-latency reads for home timeline
ElasticSearch for search: Full-text search, hashtag & keyword support
Kafka for fan-out: Decouples services, handles spikes in write load
CDN for media: Reduces latency, offloads media traffic from core
Timeline fan-out backlog: Use backpressure + prioritize active users
Redis cache miss or failure: Fallback to Timeline DB
Search index lagging behind Tweet DB: Async update, retry queues
Media storage failure: Multi-region S3 replication, CDN fallback
Celebrity tweets overload fan-out pipeline: Use fan-out on read for high-fanout users