Design post search like Facebook: the junior, senior and staff answer
"Design post search" asks you to build keyword search over a social network's posts without reaching for an off-the-shelf search engine. Users type a word or two, and get matching posts, newest first or most liked first.
It tests whether you understand what a search index actually is, and whether you notice that the hard part is the write rate, not the reads.
Requirements to pin down
- Users create posts and like them.
- Users search posts by keyword: one word or several.
- Results sort by recency or by like count.
Out of scope, by agreement: fuzzy matching, personalised ranking, and relevance scoring. Ask, and write it down.
Non-functional targets from the worked design: searches under 500 ms; a new post findable within a minute; every post findable, old ones included; search keeps working while indexing lags.
The numbers
- Posts: 1 billion a day. The worked design rounds a day to about 100,000 seconds for easy arithmetic, giving about 10,000 posts a second.
- Likes: ten per user a day for a billion users is 10 billion a day, about 100,000 a second. Ten times the post rate.
- Searches: about 10,000 a second, with bursts ten times that around big events.
- Postings: with about 50 indexable words per post, 10,000 posts a second become 500,000 index entries a second.
- Retention: 1 billion posts a day for 10 years is about 3.65 trillion posts; at 1 KB each, about 3.65 PB.
Rounding a day to 100,000 seconds is fine in an interview if you say so. It makes the per-second rates about 14% low (10,000 a second instead of 11,600), which is noise at this level of estimate.
The junior answer
- Why a scan cannot work. Finding a word inside billions of posts by scanning text is far too slow. An inverted index maps each word to the posts containing it.
- Two structures per word, so nothing is sorted at request time: a list of post ids in time order (time-ordered ids make that a push to the front), and a sorted set scored by like count.
- Ingest: posts and likes are written to a database; its change stream feeds an indexer that tokenises each post and updates the word structures in an in-memory store.
- Query: a one-word search reads one key. A two-word search walks the shorter word's list in order and keeps the posts that also contain the other word, stopping after a page of matches or a fixed number of checks.
- Writes safe to retry: a like is recorded once per user and post, so a double tap changes nothing, and index updates are idempotent.
- Then where it stops, with numbers: updating the post's score under each of its ~50 words for every like, about 5 million sorted-set writes a second, and keeping every word's lists in memory forever.
What a senior answer adds
- Writes are the problem. A durable log in front of the index, and the index sharded by word on purpose, with batching.
- Likes handled in stages. Rather than moving a post in the index on every like, the score is updated only when the like count crosses thresholds such as powers of two. Results are then re-ranked from exact counts for the top candidates. The candidate says exactly where the answer is approximate.
- Caching argued from the requirements. Results are not personalised and may be a minute stale, so popular queries cache well.
- Several words and phrases, and a strategy for common pairs.
- Ten years of data that does not live in memory: recent posts in the hot index, older segments in object storage, read more slowly.
What a staff answer adds
- The index is derived and versioned. It can be rebuilt from the source of truth, re-indexing and backfills are routine, and an audit from outside the system proves the index is fresh and complete.
- Partitioning trade-offs named, with a plan for very hot words.
- Privacy that fails closed. A post visible only to friends must never leak into someone else's results, even though the index is shared.
- Regions and failover, including counting likes without counting them twice.
- A shedding order. Under pressure, approximate work is dropped first; indexing new posts and honouring deletes never are.
Mistakes that sink this question
- Proposing a substring scan, or a database LIKE query.
- Sorting results by likes at request time.
- Updating the index synchronously on every like.
- Keeping all ten years of postings in memory.
- Forgetting deletes: a deleted post must disappear from results.
Practise it
Read the free worked design, Design Post Search like Facebook: start with the board for your level. Before you open it, estimate the like rate yourself and decide what it does to your index.
With Pro ($10/month) you can draw your answer on the practice canvas and get an AI review against the rubric.
Questions people ask
Why not just use a managed search engine?
In a real company you might. In this interview the point is to show you understand what such an engine does inside: tokenising, an inverted index, sorted postings and sharding. Ask the interviewer; if they allow one, explain how you would still handle the like rate and privacy.
How is a search for two words answered?
Walk the rarer word's list in the requested order and keep each post that also contains the other word, stopping at a page of results or after a fixed number of checks. Reading both full lists would move megabytes for common words, and very common pairs such as "happy birthday" are best indexed as phrases of their own.
What does "approximate" mean for sorting by likes?
If the index only moves a post when its like count crosses a threshold such as the next power of two, posts within the same band may be slightly out of order. Re-ranking the top candidates using exact counts fixes the order of what users actually see.