Design the database for Twitter
PremiumDatabase designHard
- Timeline storage
- ID generation
- Follow graph
Quick answer
This database design interview question asks you to design the database for Twitter. Aim for a design that stores data correctly and serves its main queries well, then explain the schema, keys, indexes and trade-offs. Interviewers want to see how you model entities, choose keys and design for the real access patterns.
The problem
Design the database for Twitter, a microblogging service. Users post tweets of up to 280 characters, reply, retweet, quote and follow others, and read a home timeline from the people they follow. The hard parts are 25,000 tweets per second at peak, accounts with 150 million followers and exact counts.
Users reply to tweets and chain their own replies into threads, and a quote tweet carries its own text. Each tweet shows reply, retweet, quote and like counts. Users can also search recent tweets by keyword.
What must the database for Twitter do?
- Post a tweet: Store the tweet and make it visible to followers within 5 seconds; a reply points to its parent, a retweet points to the original, and a quote tweet carries its own text and points to the quoted tweet.
- Home timeline: Newest 50 tweets from followed accounts, paged backwards, under 50 ms from storage.
How well must it do it?
- Latency: Home timeline under 50 ms from storage; a new tweet visible to followers within 5 seconds.
- ID generation: Tweet IDs must be unique, roughly time-ordered, and generated without a single point of failure.
What scale should you design for?
Use these numbers in your estimates.
The scale numbers unlock with this questionUnlock
Key concepts
- Timeline storage
- ID generation
- Follow graph
- Partitioning
- Write heavy
- Tweet counters
How you’ll practise
- Access patterns
- Entities
- Schema
- Indexes and queries
- Scale and consistency
- Wrap-up
Lala, personalized to you, gives a hint when you ask and reviews your design when you are ready.
What do interviewers look for?
In any database design round.
- Lists the entities and how they relate before writing tables
- Designs the schema around the main access patterns
- Chooses primary keys, foreign keys and indexes with reasons
- Explains where to normalise and where to denormalise
- Considers integrity, transactions and concurrent writes
- Plans for growth: partitioning, archiving and migrations
Questions people ask about this interview question
- Which design type is this question?
- It is a database design question. You design the database for Twitter and explain your choices the way you would in an interview.
- Is this question asked at entry level?
- It is marked Hard. It suits engineers preparing for senior interviews, where you are expected to drive the discussion and go deep on trade-offs.
- What should I cover first?
- Start with the entities and the main queries the database must answer. Agree on scope with the interviewer before you design anything.
- Can I practise it for free?
- It is a premium question. Sign up and unlock it once for 10 credits; it then stays unlocked. When you finish, Lala, your personalized AI mentor, can review your design for 30 credits.
Related questions
Updated · Written by the SevyDevy team