System design interview guide
Facebook Post Search System Design
Picture opening a social network and typing beach wedding 2019 into the search box, because you half remember a friend's photos from that weekend. You would like to see the posts you are allowed to see: public posts, posts from friends, posts in groups you belong to, and your own. The part of the system that finds those posts is called post search. It has a harder job than a normal web search, because a post that matches your words can still be off limits to you, and a post your friend deleted a few seconds ago must not show up even though the search copy has not caught up yet.

Problem statement
Design the search box of a very large social network, the part that finds posts for the words a person types. The system should keep a searchable copy of post text and fields, search across huge lists of matching posts, rank useful results, and enforce privacy for the person searching on every response. The real interview trap is that a matching post is not automatically a valid result. It must pass visibility, friends-and-groups, block, delete, and moderation checks before the user sees it.
Introduction
The problem: a wedding you half remember
Picture someone sitting on a sofa, trying to find a friend's photos from a beach wedding a few years ago. They cannot remember who posted them or when, so scrolling back through their feed is hopeless. They open the search box of a social network and type "beach wedding 2019". Thousands of posts contain those words: public albums, memories shared only with friends, posts inside a group for people who attended the wedding, a link someone reposted, and old photos whose captions were generated automatically.
What the person wants is simple to say. They want the posts that match their words and that they are allowed to see, with the most useful ones first. Two things can go wrong. A post that belongs to a private group the person never joined might show up, which leaks something that was never meant for them. Or a post that a friend deleted a few seconds ago might still appear, because the copy of the data used for searching has not yet heard about the deletion. Either mistake costs more trust than a slow search ever would.
The part of the system that finds posts for words is called post search. It looks like a small feature, but it combines three jobs that pull in different directions: finding matches quickly among billions of posts, keeping the searchable copy up to date, and checking for every single result that this particular person may see it. This page walks through those jobs in the order you would think about them in an interview. First comes what we are solving and why, then what the system must do, then how big it is, and only after that the design itself.
Why teams bother with post search
You have probably used search on a social network to find something you could not get to any other way. A feed shows what the network chose to put in front of you right now, in an order it decided. It cannot answer "what did my sister post about the lake house last summer?". Behind that small search box there are several real reasons a company spends serious engineering effort on it.
Finding things again. People remember posts by a few words, not by a date. Without search, a post that is a month old is effectively lost.
Communities. Groups, neighbourhood pages and interest communities hold advice, event details and local news. People search them for answers, such as "plumber recommendation" or "marathon route", long after the thread has left the top of anyone's feed.
Trust and safety. Search is one more place where private content could leak, and one more place where removed content could linger. A network that is careless here gets reports, press coverage and sometimes legal attention, so safety is a reason for building search carefully, not an extra.
Reach for creators. Someone who writes a useful post wants it to be found by people who were not their friends that day, but only when they chose to make it public.
What problem are we solving?
Every time a person submits a search, the system gets three things: the words they typed, who they are (called the viewer, the person whose eyes will see the results), and optional filters such as an author or a date range. It must return one page of posts, best first, plus a way to ask for the next page.
Given these words and this viewer, which posts match, which of those may this viewer see right now, and in what order should they appear?
Success has three parts. The page must come back fast, so that typing and searching feel like one motion. A new post should show up soon after it is published. And nothing the viewer is not allowed to see, whether private, blocked or deleted, may appear, even for a moment.
Notice what we have not said yet. We have not said how to find matching posts quickly, where the copy used for searching lives, or how the privacy question is answered. Each of those is a real design choice, and each has a simple first idea that does not survive the requirements. We will get to them after we are clear about what the system must do and how well.
This page is the interview deep dive in the system design interview guides hub. If caches, queues and shared stores are new to you, skim Distributed Cache System Design first.
How to approach
In an interview you do not begin by drawing boxes. You begin by turning a short, vague prompt into a clear contract: which posts are searchable, what the system must do, how well it must do it, and what is out of scope. Only then do you size the problem, make the design decisions one at a time, and draw the result. The list below is the order this page follows.
How to design post search in an interview
- Clarify the problem: which posts and which audiences, how fresh results must be, whether privacy checks may call other services, how big the system is, and how exact the results must be.
- Write the functional requirements (what the system does) and the non-functional requirements (how fast, how big, how safe).
- Estimate the size: posts, searches per second, shards, and the time budget for one search.
- Make the design decisions in order: how words find posts, how a new post gets in, how the data is split, how privacy is enforced, how deletes disappear fast, how a common word is kept cheap, how pages are ordered, and what happens when a helper fails.
- Draw the picture those decisions produce, then walk one write and one search through it.
- Go deeper where the interviewer pushes: privacy at scale, delete safety, out-of-order events, thin result pages, pagination, hot words, and one production failure.
Clarifying questions (say these in the room)
In a real interview the prompt is only a few words: "Design Facebook post search." Before drawing anything, ask questions until the problem is clear. Each answer below changes the design, which is why it is worth asking.
You: "Are we searching posts only, or also comments, profiles, pages and marketplace listings?"
Interviewer: "Focus on posts: public, friends-only, group, and the viewer's own."
That answer fixes the thing we store. The main item is a post that carries a visibility rule, not a public web page that anyone may read. Comments, profiles and listings each have their own fields and permissions, so you say the same pattern could extend to them and leave them alone.
You: "How fresh must search be after someone creates, edits or deletes a post?"
Interviewer: "New posts can show up within seconds or a few minutes, but deletes and takedowns should disappear as fast as possible."
This gives you two different promises. For a normal new post, "soon" is good enough, which lets the system do the update in the background. For a delete or a takedown (a post removed by the company's own safety team), "soon" is not good enough, because a removed post that still appears is a trust incident. Keep these two promises separate from this point on.
You: "Is text relevance plus recency enough, or do we need a full personalized ranking platform?"
Interviewer: "Text match, recency and a few simple social signals. No full machine-learning platform."
Keeping ranking simple lets you spend the interview on privacy and deletes, which is where this problem is actually hard.
You: "When we check whether the viewer may see a result, may the search path call the privacy and friends service, as long as we stay inside a normal search time budget?"
Interviewer: "Yes, you can call it, but searches must still feel instant."
That answer is a warning in disguise. You are allowed to ask the privacy service, but you cannot ask it about every match, because a common word can match millions of posts. So the number of results you check per search must be capped, and you will say the cap out loud later.
You: "Roughly how many posts and how many searches a day, and may we return a slightly incomplete list if that keeps the answer fast?"
Interviewer: "Billions of posts and millions of searches a day. A slightly incomplete list of results is fine. Showing something forbidden is not."
Those two sentences set priorities for every later trade. Between "show everything that matches" and "never show what is forbidden," the second always wins. Thinking of results as "a good, safe sample" and not "all matches" is what makes a bounded design possible.
You: "I would like to leave reading text out of photos, autocomplete, ads and country-by-country legal workflows out of scope, so we can focus on the core path. Does that match?"
Interviewer: "Mention photo text as something added later. Leave the rest out."
If the interviewer later asks about index engine brands, how many shards, or how privacy is checked in detail, you do not need to answer yet. Park those until the design decisions, where each one has a natural place.
Functional requirements
Functional requirements describe what the system does. From the answers above, post search must:
- Make a new post searchable after it is saved. The person who posted should not wait for search, and a crash in search should never stop someone from posting.
- Keep the searchable copy in step with edits and visibility changes. If an author changes a post from public to friends-only, search must follow, and an older update must never overwrite a newer one.
- Remove deleted and taken-down posts. They must stop appearing quickly, even before the searchable copy has been fully cleaned up.
- Search by words and filters. A person types words and can narrow by author, group, post type or time range.
- Show only posts this viewer is allowed to see. Visibility depends on who the author is, who the viewer is, group membership, blocks and custom audiences, so it has to be checked for each viewer.
- Order the matches. Text match, recency and a few simple social signals decide which results come first.
- Return the next page. A person who scrolls should get new results without repeats or gaps, even if the order shifts a little in between.
Non-functional requirements
Non-functional requirements describe how well the system must do its job. They are the numbers and promises that shape the design.
- Speed. Most searches should finish within a few hundred milliseconds, because search is used in a typing-and-glancing rhythm. A slow search is simply not used.
- Two kinds of freshness. A normal new post may take seconds to a few minutes to appear. A deletion or takedown must be hidden faster than that, so the two promises need different mechanisms.
- Privacy correctness. The system must never show a post the viewer may not see, including its snippet, its author name or the fact that it exists. This is the requirement that outranks the others when they conflict.
- Scale. Billions of posts and millions of searches a day, with a heavy read when a very common word matches an enormous number of posts.
- Graceful degradation. When a helper such as the privacy service or the ranking step is slow, the system may return fewer, slower or less personalized results. It must not return unsafe ones.
- Visibility into the system. The team must be able to measure how far behind the searchable copy is, and how long deletes take to disappear, not only whether searches succeed.
Out of scope (and why you park these)
Write a short out-of-scope list on the board so the interviewer sees you can bound the problem.
Searching all of Facebook. Profiles, pages, marketplace, events and comments each have different fields and permissions. Say the same retrieval pattern could extend to them, and keep the interview on post documents.
Training a large ranking model. Ranking matters, but this interview tests architecture. Use simple text, recency, social and spam signals so your time goes to privacy and latency.
Complete media understanding. Reading text from photos, labelling images and transcribing video can enrich the index later through background workers. They must not be part of saving a post.
Ads and sponsored results. Ads change ranking, policy and billing. Park them unless the interviewer widens the scope.
Global legal compliance in every country. Say that deletes, data residency and legal holds exist, but a full legal workflow would turn this into a compliance interview.
That is the requirements stage. If you want to practice it before reading on, try the requirements stage on Practice System Design (about 5-10 minutes), then come back for the sizing and the decisions.
Minute pacing (about 40 minutes)
| Minutes | Focus |
|---|---|
| 0-5 | Clarify the problem; write functional and non-functional requirements; agree out of scope |
| 5-10 | Capacity: posts, searches, shards, hot words, time budget |
| 10-22 | Design decisions, then the high-level boxes with one write and one search |
| 22-33 | Deep dive: privacy at scale, deletes, out-of-order events, thin pages, pagination |
| 33-40 | Failures, observability, one production story and questions |
In the room (how you open): "I would start by agreeing what we are searching: posts with a visibility rule, not all of Facebook. I would ask how fresh new posts must be compared with deletes, whether the privacy service can be called while searching, how many posts and searches there are, and whether a slightly incomplete result list is acceptable if we never show something forbidden. I will write the requirements on the board, size the problem, and then make the design decisions one at a time, so every box I draw has a reason."
If you remember one thing: Ask first and write the requirements down. Then separate "a new post appears soon" from "a deleted post hides now" before you draw a single box.
Capacity estimation
Now that we know what the system must do, we need to know how big it is, because size is what rules out the easy ideas. None of the numbers below come from a real system. They are round, illustrative estimates for the whiteboard, and you would say so in the room.
| Axis | Illustrative estimate | What it means for design |
|---|---|---|
| Posts | About 5 billion searchable posts | Too many to scan per search; the data must be split across many machines |
| Index size | A few terabytes per full copy (assume roughly half a kilobyte to one kilobyte per post), several times that with copies for safety | Memory and disk are a real budget; index only what search needs |
| New posts | About 10 million a day, which is roughly 120 a second on average and about ten times that at peak | Writes are modest compared with reads; update the index in the background in batches |
| Searches | About 50 million a day, which is roughly 600 a second on average and a few thousand a second at peak | Each search fans out to many machines, so the machine-level load is a multiple of this |
| Data split | About 100 pieces of about 50 million posts each | One search touches roughly 100 pieces, so a few thousand searches a second becomes a few hundred thousand piece-level requests a second |
| Time budget | About 300 ms end to end | Split it per step (see below); a step with no budget has no ceiling |
| Candidates to check | Each piece returns about 50 candidates, so about 5,000 arrive; keep only about 500 to 1,000 for privacy checks | Privacy work per search is bounded by a number you choose, not by how popular a word is |
| Deleted-post list | A few million recent entries, each a small record | Small enough to copy into the memory of every machine that merges results |
A rough split of the 300 ms budget is useful to say aloud: a few milliseconds to understand the query, around a hundred for the slowest search piece, a few to merge, a few to check the deleted-post list, around fifty for one batched privacy check, and the rest for snippets and ranking, with a little slack.
What the numbers imply
- The searchable copy is a derived view, not the source of truth. The post service owns the real post. If the copy is ever wrong, it can be rebuilt from the post service or from the log of events.
- Every search needs a work budget. Decide how many pieces it may touch, how many candidates each returns, how many are checked for privacy, and how long ranking may take. Without those numbers, a popular word decides the cost instead of you.
- Reads fan out, writes stay local. A new post lands in one piece, but a search must ask all of them, so the machine that merges answers matters as much as the machines that hold the data.
- Safety cannot wait for the slowest background job. Index refresh and cleanup run on a schedule that is fine for new posts and far too slow for a takedown.
- Privacy per candidate is the hidden cost. If every match needed its own call to the privacy service, a common word would cost millions of calls, so the check must be batched and capped.
Checkpoint: Name your per-search budget in numbers, and say why you cannot check privacy for every match.
If you remember one thing: The cost of a search should be set by your caps, not by how popular the word is.
Design decisions
We now know what the system must do, how well, and roughly how big it is. Now we choose. Each decision below answers one question the requirements raised, and each one rules out an easier idea that would not survive those requirements. Read them in order, because every decision builds on the one before it.
Decision 1: How do the words lead to the posts?
The first requirement is to find posts for words fast. The simplest idea is to keep every post in a table and, for each search, look through all of them for the typed words, for example with a "text contains" condition in a database. For a few thousand posts that works. At billions of posts it means reading every post for every search, which would take many seconds or minutes and burn the machines, so it breaks the speed requirement immediately.
Two other ideas come up. The first is to turn on the full-text feature of the main post database. That helps at small scale, but it makes the database that serves posting also serve every search, so a busy search hour slows down posting, and one machine still cannot hold billions of posts. The second is to compute answers for common searches ahead of time. That only covers searches people have already made, and it fails for the long tail of unusual ones.
The idea that works is the one printed books use. At the back of a book there is an index: for each word, the pages where it appears. Search does the same with posts. For every word, it keeps a list of the posts that contain it, so a search for "beach" goes straight to the list for "beach" and a search for "beach wedding" combines the lists for the two words. This is called an inverted index (an index from words to the posts that contain them), and the list for one word is called a posting list. Products such as Elasticsearch, OpenSearch and Lucene-based engines provide this; the brand is optional, and the property you need is "words lead straight to posts." Each post also carries a few extra fields, such as author, time and visibility, which the later decisions need.
Decision 2: How does a new post get into the searchable copy?
The requirement is that a new post becomes searchable soon, without making the poster wait and without search problems stopping posting. The simplest idea is to have the post service write to the search index in the same request that saves the post. It is easy to follow, but it fails twice. Posting now waits on the search system, so a slow index makes publishing slow. And if the post is saved but the index write fails, or the other way round, the two copies disagree and nothing remembers to repair it.
Two alternatives were set aside. Writing to both places from the application ("dual write") has the same disagreement problem. A scheduled job that rescans recent posts every few minutes keeps the systems separate, but it is slow for new posts, costly for old ones, and cannot see deletes it never scanned.
The choice is to save the post first and tell the search system afterwards through a durable event. The post service saves the post, and in the same database transaction records an event in an "outbox" (a small table of things that must be announced). A background step publishes those events to a log, so none is lost even if something crashes in between. Workers read the log, split the text into words (this is called tokenizing), attach the fields, and write batches of documents into the index. Each event carries a version number for the post, so a slow worker cannot overwrite newer information with older information. The post becomes searchable after the index's next refresh, which in practice takes a few seconds.
Slow enrichments, such as reading the text inside a photo, follow the same pattern. The post is searchable by its own words first, and the extra words arrive later as another event. That keeps the poster's wait short.
Decision 3: How do we split the index across machines?
The index is far too big for one machine, so it has to be split into pieces called shards. The simplest idea is one shard, and it fails on size and on the number of searches per second. The real question is what to split by.
One option is to split by word: one shard owns the lists for certain words. A search for one word then talks to one shard. The cost is on the write side, because a post with fifty words must update fifty shards, and a popular word becomes a single overloaded machine. The other option is to split by post: each shard owns a range or hash of post ids and holds the lists for just its own posts. A new post goes to exactly one shard, which keeps writes simple, but every search must ask all shards, because matching posts could be anywhere.
In a social product, posts are created, edited, deleted and re-permissioned all day, so cheap writes and simple updates matter more than saving some search fan-out. We choose splitting by post, copy each shard onto a few replicas to spread the reading load, and accept that every search goes to many shards. That choice creates a job: a machine in front of the shards must send the question to all of them, collect their answers and combine them. We call it the aggregator, and many later decisions are really about keeping its work bounded.
Decision 4: How do we make sure the viewer only sees what they may see?
The requirement says never show forbidden posts. The simplest idea is to let the index return every match, rank them, and let the application hide the ones that fail a privacy check at the end. It has two problems. For a common word the list of matches can be huge, so checking each one wastes enormous time. And if snippets, titles or ranking features are fetched before the check, private text can end up in logs and caches even if the final response hides it.
Two alternatives were considered. A separate index for every person would make privacy trivial, but a large social network would need to store and update billions of private indexes whenever anyone posts or a friendship changes, which is far too costly. The opposite idea, putting the complete privacy rules inside the index, fails because "friends of the author" depends on the viewer, and friendships, blocks and group memberships change every minute, so the index would always be stale.
The choice is a two-stage check. First, store cheap, stable facts on every indexed post, such as whether it is public, its author, its group and its audience. Shards use them to throw out obvious misses early. Second, the aggregator takes a bounded set of the surviving candidates and asks the privacy and friends service about them in a single batch, using the viewer's current context (their friends, groups and blocks). Only candidates that pass get a snippet and a rank. Order matters: deleted-post check, then cheap field filter, then the batched privacy check, then snippets and ranking.
Decision 5: How do deletes and takedowns disappear fast?
The requirement says deleted and taken-down posts hide faster than normal updates. The simplest idea is to wait for the index to apply the delete like any other update. That fails the requirement directly: the update goes through the same slow background path, and old copies of the post can stay inside index files for a while after the logical delete.
The alternatives are expensive. Refreshing the index much more often for every change costs a lot of CPU and disk for a benefit that only deletes need. Asking the post service "does this post still exist?" for every result on every search turns a fast read into a stream of calls to the busiest system.
The choice is a small, fast deleted-post list, called a tombstone store (a tombstone is a marker that says "this post id is gone"). When a post is deleted or taken down, the post service writes its id there at once, besides sending the normal event. Every search checks that list before it fetches snippets or ranks anything. The slow index cleanup still happens in the background, but the user is protected immediately. Because the list only needs recent ids, it is small enough to keep a copy in the memory of every aggregator.
Decision 6: How do we keep a very common word cheap?
A word like "birthday" may match tens of millions of posts. The simplest idea is to collect all matches, sort them, and take the best twenty. The cost of a search then depends on how popular the word is, and the most popular words cost the most. That breaks the speed requirement exactly when many people are searching the same thing.
The choice is to put a ceiling on work at every step. Each shard returns only its best few dozen candidates (called its top-k, the k best), and it can stop reading its list early when it is clear nothing better will turn up. The aggregator then keeps only the best few hundred of the merged candidates. It gives each shard a time limit, and it accepts a slightly incomplete answer if one shard is slow. For popular public searches, a short-lived shared cache can hold the head of the results. None of this is a hack. It is the direct consequence of the earlier agreement that a slightly incomplete list is fine and a slow or unsafe one is not.
Decision 7: How do we order results and return the next page?
Ranking uses text match, recency, social closeness, group membership and spam signals. The decision that matters is when ranking happens and how pages are cut. The simplest idea for paging is "skip the first N results and take the next twenty" (called offset paging). It fails because the order shifts while someone scrolls: new posts arrive, scores change, privacy answers change, so the person sees repeats or misses posts.
The choice is an opaque cursor: a token that the server gives back with each page, containing the score and post id of the last result shown, a time fence for the search, and enough query context to continue. The next page asks for results that come after that point. The token is opaque (the client cannot read or build it) so the server can change its contents later and cannot be tricked into leaking anything. Ranking itself runs after the safety checks, on the posts that remain, which keeps private text out of the ranking step.
Decision 8: What happens when a helper is slow or down?
Search depends on several helpers: shards, the privacy service, the deleted-post list and the ranker. A decision made in advance is better than one made during an outage. The rule for all of them is that degraded search may return fewer, slower or less personalized results, but never unsafe ones.
So, if one shard is slow, search returns without it, and the results are a little thinner. If the ranker times out, search falls back to text match plus recency, applied after the privacy checks. If the privacy service times out, private candidates are dropped, and only candidates that are safe without a graph check may be kept, for example public posts that pass the deleted-post list and the viewer's own block list, depending on product policy. If the deleted-post list cannot be reached and the copy held by the aggregator has gone too stale, the aggregator must not guess, so it returns a temporary error and lets the client retry, because a quiet guess could turn a delete into a leak. Say each rule out loud, because the interviewer is looking for that reasoning, not for a single right answer.
Putting the decisions together
Taken together, the eight decisions are the design. A post is saved first, and a durable event carries it to workers that write a searchable copy with a version and a few visibility facts. The copy is a list of posts for every word, split by post across many shards. A search fans out to all shards, each returns a small top-k, and the aggregator merges, drops deleted posts, applies cheap filters, runs one batched privacy check with the viewer's context, and only then fetches snippets and ranks. A cursor carries the page boundary. Every helper has a pre-agreed failure rule that prefers fewer results to unsafe ones.
In the room (a fuller opening you can actually say): "A post is saved in the post service first, and that service writes an event in the same transaction, so the search system can catch up without anyone waiting. Workers turn the text into words, attach the author, time and visibility facts, and write a version-stamped document into an index that maps each word to the posts containing it. I would split that index by post id, so writes stay on one shard, and accept that every search asks every shard. For a search, each shard returns a few dozen candidates, and an aggregator merges them, drops anything on the deleted-post list, uses the stored visibility facts to prune, and makes one batched call to the privacy service for a capped set, so a common word cannot cost millions of checks. Only the survivors get snippets and a rank, and the page ends with an opaque cursor. Deletes and takedowns go to a small fast deny list that every search checks, so a removed post is hidden even while the index is behind. If a helper is slow I return fewer or less personalized results, never unsafe ones."
The next section draws these decisions as one picture and shows who owns what.
High-level architecture
The decisions above were made one at a time. This section puts them on one page as a picture: which boxes exist, who owns what, and how a write and a search travel through them. The deeper mechanics come later, so keep this at the level of a map.
The post write path and the search read path are different systems that meet at the index. The post service owns the truth. The indexing pipeline owns a searchable copy. The query service owns the answer the viewer sees.
[ Post service ] -- outbox event --> [ Event log ] --> [ Index workers ]
| |
| source of truth v
v [ Index shards ]
[ Post DB ] word lists + fields
|
+-- delete / takedown --> [ Tombstone store ]
[ Client search ]
|
v
[ Search API ] --> [ Query planner / aggregator ] --> shard top-k fanout
| |
| v
+--> [ Privacy / Graph batch ] <-- candidates
|
+--> [ Tombstone check ] --> [ Rank + cursor ]
The post service commits the post, edit, visibility change, delete or moderation decision first. It emits a durable event after the commit so index workers can retry without losing changes. Index workers tokenize text, normalize language, attach fields and bulk-write documents into index shards. Media enrichment workers can add words from a photo later through update events.
The search service receives a query with a viewer id. It parses the words and filters, picks the relevant shards, sends requests in parallel and asks each shard for local top candidates. The aggregator merges those candidates, drops anything on the deleted-post list, runs the viewer-aware privacy filter, fetches lightweight ranking features and returns a page with a cursor.
Who owns what:
| Box | Owns | Does not own |
|---|---|---|
| Post service | Source-of-truth content, author, visibility, delete state, moderation state | Search ranking |
| Event log | A durable, replayable stream so workers can retry and reindex | Deciding what is visible |
| Index workers | Tokenizing, enriching, version checks and bulk writes into shards | The truth about a post |
| Index shards | Word lists, stored fields for snippets, local top-k scoring | The viewer's friends, groups or blocks |
| Search aggregator | Fanout, merge, candidate budgets, timeouts, privacy calls, ranking, cursors | Storing posts |
| Privacy and graph service | Whether this viewer may see this author, group, audience and block relationship | Search results |
| Tombstone store | Blocking deleted or taken-down post ids while index segments catch up | Normal index updates |

Figure: Shards return candidates, then the deleted-post list and the privacy check remove anything unsafe before ranking.
In the room: Draw the write path and the read path separately. Say that the index helps find candidates, but the post service and the privacy systems still decide whether a candidate may be shown.
If you remember one thing: The index finds candidates. Privacy and the deleted-post list decide whether a candidate becomes a result.
Core design approaches
The decisions chose one way to split the index and one way to enforce privacy. This section sets those choices beside their alternatives, because interviewers often ask "why not the other one?". The main choices are not vendor choices. They are where you pay for coordination: on writes, on reads, or on privacy checks.
| Approach | Writes | Reads | Where it hurts | When to pick |
|---|---|---|---|---|
| Split by post (document shards) | One shard per post, cheap updates | Every search asks many shards | Needs a strong aggregator | Posts change visibility often (the usual social case) |
| Split by word (term shards) | One post updates many shards | Some searches touch few shards | Popular words become hot machines; multi-word searches coordinate across owners | Rarely edited, mostly read-only collections |
| Index facts plus batch privacy check | Small extra fields per post | One bounded batch call per search | Needs a cap and a timeout | Default for social search |
| Index per person | Every post updates many indexes | Trivial privacy | Huge storage and update cost | Small private surfaces only |
Document-sharded index. Each shard owns a range or hash of post ids. A new post goes to one primary shard and its replicas. A search fans out to many shards, each returns its local top candidates, and the aggregator merges them into a global page. This is usually the better default because posts are constantly created, edited, deleted and re-permissioned. Some systems also split by time, so a search for recent posts only touches the newest shards. That is a refinement of the same idea.
Term-sharded index. Each shard owns some words, so a query can hit fewer shards. The cost moves to writes, because a post with many words can update many shards. Hot words become hot shards, and multi-word queries need coordination across the owners. This can work for specialized cases, but it is harder to operate when the write stream is large and documents change visibility.
Privacy facts inside the index. The index stores fields that reject obvious misses early: visibility type, author id, group id, language, post type, created time and moderation state. For public posts that may be enough. For friends, groups, custom audiences and blocks the system still needs viewer-specific information, which is why the two-stage filter exists: cheap pruning first, then a bounded batch check.
Query-time safety layer. Deletes, takedowns and some visibility changes also write to the tombstone store, keyed by post id. The query service checks it before ranking. It does not replace normal index deletion. It protects the user while slower segment cleanup and compaction catch up.
Data model
The search path needs somewhere to keep what it knows, so the next question is what exactly is stored and which copy is the truth. The data model should make it clear which fields are source of truth and which are copied into the index for fast retrieval.
| Entity or store | Important fields | Why it exists |
|---|---|---|
posts | post_id, author_id, body, created_at, updated_at, visibility_type, group_id, audience_id, moderation_state, deleted_at | Source of truth for content and current state |
post_events | event_id, post_id, event_type, version, payload, created_at | Durable stream for create, edit, delete, visibility change and takedown |
index_document | doc_id, terms, author_id, created_at, visibility_type, group_id, language, spam_score, version | Searchable copy used by index shards |
forward_index | doc_id, title or snippet fields, safe preview metadata | Lets the query path build snippets without fetching the full post for every candidate |
viewer_context_cache | viewer_id, friend ids or compressed set, group memberships, block set version | Speeds privacy checks for the current viewer |
tombstones | doc_id, reason, expires_at, source_version | Blocks deleted or taken-down posts while index segments lag |
search_cursor | encrypted query plan, last score, last doc id, snapshot or time fence | Supports stable pagination without offset scans |
The index document should include enough metadata to filter early, but it must not become the privacy source of truth. If a friendship, group membership or block changes, the query path must use current viewer context, or a clearly bounded cache with a short lifetime. The doc_id is the same as the post id, is created once and is never reused, which keeps tombstones safe.
Detailed design
We now know what is stored and who owns it, so we can put the pieces in motion. This section follows three journeys: one post entering the index, one post leaving it, and one viewer searching. Then it shows the same thing from the caller's side: the request, the response and the internal calls an operator uses. Read the three journeys as a story first. The exact edge cases come in the deep dive right after.
When a post is created or edited
A user writes a post and the post service commits it to the post database. That commit is the moment the product has accepted the content. In the same database transaction, the service records an outbox event so the indexing system can retry later. This avoids a dangerous split where the user sees "posted" but the index event is lost forever.
Index workers consume the event and build a search document. They tokenize the text (cut it into words), normalize the language, remove or downweight filler words, store author and time fields and attach visibility facts such as public, friends, group id or custom audience id. They also keep a document version so a slow worker cannot overwrite a newer visibility change with stale facts.
The worker bulk-writes to the right index shard. The document becomes searchable after the index refreshes, which may happen every few seconds depending on cost and freshness needs. Background merges later compact many small index files, called segments, into larger ones. That merge is an operational detail, but it explains why normal search freshness is near-real-time and not perfectly instant.
If photo text is in scope, text reading and caption extraction run in a separate pipeline. A post can be searchable by its words first and later gain extra fields. That keeps post creation fast and prevents a slow image model from blocking the user's write.
When a post is deleted, hidden or taken down
A delete or takedown starts in the source-of-truth post service. The service marks the post deleted or moderated, emits a normal index event and, when the removal is safety-sensitive, also writes the post id into the tombstone store. The normal index update eventually removes or hides the document in search segments. The tombstone protects the query path immediately.
A visibility change, such as turning a public post into a friends-only one, also carries a version. The new version must win even if older create events arrive later. A simple rule helps: indexers only apply an event if its version is newer than the version stored for that document.
When a viewer searches
The client calls the search API with the query text, filters, a page size and a cursor if this is not the first page. The API authenticates the viewer and builds a small viewer context: user id, friend set or compressed membership, groups, block relationships and any policy flags the privacy check needs.
The query planner parses the query, applies required filters such as a time range or post type, and sends the planned query to index shards in parallel. Each shard uses its word lists to find matching documents and returns only its local top candidates, not every match. The local score can include text match, recency, language, spam score and simple fields.
The aggregator merges shard candidates and keeps a hard cap, such as the best few hundred or thousand, before privacy checks. It checks the tombstone store first, because there is no reason to rank a deleted post. It then applies cheap visibility filters and sends the remaining candidates through the privacy service in one batch, so the query does not become one network call per candidate.
Only after that does ranking finalize the page. Ranking can boost recent posts, friends, groups the viewer visits often and exact phrase matches, and it should downrank spam and unsafe content. The response returns safe results plus an opaque cursor that records the last score, the last post id and enough query context to continue without offset paging.
- Authenticate the viewer and build the viewer context.
- Plan the query and send it to all relevant shards with a time limit.
- Each shard returns its best few dozen candidates.
- Merge and cap the candidate pool.
- Drop anything on the deleted-post list.
- Prune with stored visibility facts, then one batched privacy check.
- Fetch snippets and rank only the survivors.
- Return the page and an opaque cursor.
API design: what viewers send and what operators change
Search is usually a read API in an interview, so the API design is mostly about three things: the call the viewer makes, what comes back, and the internal calls operators use to repair or protect the index. The public API should make the cost of a query visible through required bounds and cursor paging.
| Endpoint | Role |
|---|---|
GET /v1/search/posts | Search visible posts for the authenticated viewer |
POST /internal/index/posts:reindex | Internal admin trigger for backfill or repair jobs |
POST /internal/index/posts:tombstone | Internal safety path to block a post id quickly |
The search call. The viewer's identity comes from the login session, never from a query parameter, so a client cannot pretend to be someone else.
| Param | Role |
|---|---|
q | Query text |
limit | Page size with a server cap |
cursor | Opaque pagination token |
author_id | Optional author filter |
group_id | Optional group filter, still checked against the viewer's membership |
since, until | Optional time window that helps bound work |
type | Text, link, photo or video |
The hot read path:
GET /v1/search/posts?q=beach+wedding
|
v
[ Search API ] -> viewer context -> query planner
|
v
[ Index shards ] -> local top-k -> aggregator
|
v
tombstone check -> privacy batch -> rank -> cursor response
What the client receives:
{
"results": [
{
"post_id": "p_123",
"author_id": "u_42",
"snippet": "Beach wedding photos from 2019...",
"created_at": "2019-07-20T10:00:00Z",
"reason": "friend_post"
}
],
"next_cursor": "opaque-token"
}
Which status code means what. A 400 means the query is malformed. A 401 means the viewer is not logged in, if the product requires login. A 429 means the viewer or app is sending too many searches. A 503 means the system chose not to serve a partial answer. A filtered-out private post must look as though it never existed, so no error message may reveal that a private post exists.
How operators change things. The reindex endpoint rebuilds documents from the post service or the event log after a bug or a schema change. The tombstone endpoint lets the safety team block a post id right away, without waiting for any pipeline. Both are internal, authenticated and audited.
In the room: "A post is saved first and announced by an event, and workers write a versioned, searchable copy. A removal also goes into a fast deny list. A search fans out to shards, merges a bounded candidate set, checks the deny list and the privacy service in a batch, ranks only the safe posts and returns a cursor. Operators can reindex or tombstone through internal endpoints."
Deep dive: the questions an interviewer asks next
You have drawn the design and walked a write and a search through it. This is where a good interviewer starts digging, and the questions usually come in a predictable order, because each answer raises the next one. Read this section as that conversation. Each question is followed by an answer you can give, plus a hint of where the interviewer is likely to go next. If the next answer is already clear from what you just said, offer it before they have to ask.
“A common word matches ten million posts. Do you check privacy on all of them?”
No, and the reason is the heart of the design. Look at the weak version first. It asks the index for all matching posts, ranks them, and lets the application hide the ones that fail privacy at the end. For a common word the candidate set can be enormous, so the application spends its time filtering. And if a snippet or a title is fetched before the check, private text can reach logs and caches even if the final answer hides the post.
The better shape uses the index for what it is good at: finding by word and cheap field filtering. Each shard returns only its best few dozen, the aggregator keeps a global cap, and privacy runs on that capped set in batches.
Weak: 10,000,000 matches -> rank -> hide forbidden -> page
Better: 100 shards x top 50 = 5,000 -> deleted-post list -> cheap filter
-> ~500 to privacy (one batch) -> snippets -> rank -> page
The important phrase is bounded candidate set. You choose the number, say it out loud (for example "a few hundred to a thousand") and size the privacy service for it. The cost of a search then no longer depends on how popular the word is.
Where it goes next: "What exactly does the index know about privacy, and is that enough?"
“What privacy information lives in the index, and why isn't that enough?”
The index stores facts about the post that do not depend on who is looking: whether it is public, its author, its group id, its audience id, its language, its type and its time. Shards use them to reject obvious misses, for example a group post when the search is for the viewer's friends.
It is not enough on its own, because "friends" depends on the viewer. Whether a post may be seen depends on the author, the viewer, whether they are friends, whether the viewer blocked the author, whether the viewer is in the group, who is in a custom audience and sometimes the moderation policy. All of these change every minute. If the index tried to hold them, it would always be a little stale.
So the aggregator builds a compact viewer context at the start of the search (friend set or a compressed version of it, groups, a small block set, all with a version) and uses it for the batch check. The viewer context can be cached briefly, but blocks and safety actions should invalidate or bypass the cache, because a stale block is the kind of leak that users notice at once. The point to land is that the check is about the viewer and not only about the document.
Where it goes next: "A post was deleted, but the index is forty seconds behind. What does the viewer see?"
“A post was deleted but the index is forty seconds behind. What does the viewer see?”
Nothing, if the fast deny list is working. The searchable copy updates in the background, so an old copy of a deleted post can still sit in a search file for a while after the delete. That is why answering a search needs its own, faster check: a small list of removed post ids, often called a tombstone store, that every search consults before showing results. Here is the timeline:
00:00.0 Moderator removes post p_9 in the post service
00:00.0 Post service writes p_9 to the tombstone store + emits delete event
00:00.5 Tombstone copied into every aggregator's memory
00:00.5 A search that matches p_9 on a shard -> aggregator drops it
00:05 Index applies the delete event; document hidden in its segment
hours Segment merge removes it physically; tombstone can later expire
Search checks the store before snippets and before ranking. Tombstones need to live long enough to cover index cleanup and event replay. Doc ids are created once and never reused, which means a tombstone can never accidentally hide a new post. If ids could be reused, the tombstone would need a version to tell the old document from the new one.
Where it goes next: "What if two events for the same post arrive in the wrong order?"
“What if two events for the same post arrive in the wrong order?”
It happens, because events are processed by many workers and retried. Say a post is created as public (version 1), then the author makes it friends-only (version 2). If version 2 is processed first and version 1 arrives late, a naive worker would write "public" over "friends-only", and the post would be visible to everyone, which is a privacy bug.
The rule is simple: every document carries the version of the last event applied, and a worker applies an event only if its version is newer. The index write is conditional on that version, and processing the same event twice is harmless. This is called making updates idempotent (doing it twice has the same effect as once). A delete is the newest version of all and is never overwritten by an older create.
Where it goes next: "If you only keep the best few hundred and privacy removes most of them, is the page empty?"
“If privacy removes most of the candidates, is the page empty?”
It can be, and this is a case that candidate caps create. Suppose the viewer searches for a word where most matching posts are private to other people. The shards return their best candidates by text score, but the best-scoring ones may be exactly the ones the viewer cannot see. After filtering, three results remain, and the viewer has asked for twenty.
The fix is to ask again, within a budget. The aggregator can request the next slice of candidates from the shards (the cursor already carries the position), filter that slice, and repeat for one or two more rounds. It stops when it has enough safe results or when it spends its time budget. If it still has fewer than a full page, it returns what it has with a cursor, and the client can ask for more. The cost is a little more latency for searches with a low pass rate, and it is worth watching, which is why the privacy-filter drop rate is one of the metrics in Observability. You can also reduce the problem by using visibility facts in the shard query itself, so a shard rarely returns posts that obviously cannot qualify.
Where it goes next: "How does paging work when the ranking keeps changing?"
“How does paging work when ranking keeps changing?”
Offset paging, which means "skip 20 and take 20", repeats or skips results when scores change between requests. A new post arrives, a privacy answer changes, or a score moves, and the person sees the same post twice or never sees another.
Instead the server gives back a cursor with each page. It records the last score and post id the person saw, a time fence for the search, and enough of the query to continue. The next request asks for results that come after that score and id. A tie in the score is broken by post id, so the position is exact. The cursor is encrypted or signed, so a client cannot edit it to leak something, and the server stays free to change its contents later. Be honest with the interviewer: even with cursors, a very fresh index can move a post between pages, because ranking is not frozen. A time fence limits how much.
Where it goes next: "What breaks first when everyone searches the same word?"
“What breaks first when a hashtag goes viral?”
Everyone searches the same hashtag after a live event. The median search is fine, because most searches are ordinary, but the slowest searches are for the hot word. Its posting list is enormous on every shard, the aggregator waits for the slowest shard, and the privacy service is asked for a full batch every time.
More application servers do not help, because the cost is inside the shards and the privacy batch. What helps is the budget from the decisions: each shard returns a small top-k and stops early when it can, the aggregator caps the global pool, every shard has a time limit, and a short-lived cache holds the public head of popular searches. Identical public queries can be combined so that one execution answers many people. Personalized re-ranking stays bounded, and it runs on the cached safe head and not on the raw list. The cost to say out loud is that the cached head can be a few seconds old, which is fine for ordinary posts because deletes still go through the tombstone check.
Where it goes next: "What if the privacy service itself is slow?"
“What if the privacy service is slow?”
Then the choice is between fewer results and unsafe results, and the rule from the decisions is that it must always be fewer. The aggregator gives the batch call a strict time limit. If it expires, private candidates are dropped, and only candidates that are safe without a graph check may be kept, such as public posts that already passed the deleted-post list and the viewer's block set. Depending on the product, the response may be public-only and somewhat thin. It is never padded with unchecked private posts. The alert for this state is a rise in privacy-service latency together with a rise in the privacy-filter drop rate.
Where it goes next: the interviewer may now step back and ask you to sum it up.
“Can you sum it up in one sentence?”
Land on this sentence: "The searchable copy finds candidates; privacy and the fast deny list decide whether a candidate can become a result." It tells the interviewer you understand the boundary between search relevance and social trust.
Say it in the interview: "I bound the candidate set, apply cheap facts in the index and a batched check with the viewer's current context after it, put deletes on a fast deny list the query always checks, and make updates version-safe. The cost is some extra latency when few candidates pass, and that is why I watch the drop rate."
If you remember one thing: Bounded candidates plus batched checks plus a fast deny list are what turn word search into social search.
Key challenges
The deep dive showed that the core of search is simple once the work is capped. What makes the real system hard is everything around it, and five hard parts show up again and again. Name them early in the interview, because each is a place the interviewer is likely to push.
Privacy depends on who is looking. A post can be visible to one person and forbidden to another. The searchable copy can store facts about the post, but it cannot replace the viewer's current friends, groups and blocks.
"Fresh" means two different things. A new post appearing after thirty seconds may be fine. A deleted private post still appearing after thirty seconds is not. Treat the hide-now promise as its own lane.
Hot words create huge match lists. Words like "birthday", celebrity names or viral hashtags can match millions of posts. Caps on how many matches you keep, and stopping early inside a list, matter more than adding another application server.
Ranking must happen after safety checks. If ranking fetches snippets or features for private posts before filtering, logs and caches can leak information even if the final response hides the post.
Paging is hard when the order keeps shifting. "Skip the first twenty and take the next twenty" repeats or skips results when scores change. An opaque page token with a stable score and post-id boundary fixes most of it, and a very fresh searchable copy can still move items between pages.
If you remember one thing: Privacy and delete safety are design requirements, not polish added in the last week.
Scaling the system
Those hard parts get worse as traffic grows, so the next question is what happens when posts and searches multiply. You do not have to wait for the interviewer to ask. Right after you finish the design, say the scaling story yourself, then be ready for the follow-ups.
What you can say upfront: "Reads scale by adding copies of each search piece, and storage and writes scale by adding more pieces, though more pieces mean every search asks more machines, so I keep that count inside the time budget. Indexing scales with more workers that write in batches and replay from the event log. Hot public searches use a short-lived shared cache, but I would not cache final personalized pages. Celebrity authors and huge groups get special routing and their own safety queue. Across regions I would either keep a regional searchable copy and combine results, or route users to one home region, and I would state how fresh each region is instead of claiming one copy is perfectly fresh everywhere."
That one paragraph answers most of what follows. Here are the follow-ups, in the order they usually come.
Interviewer: "Searches double next quarter. What do you change?"
You: "Replicas first, because they are the easy half. A replica holds a copy of a shard, so adding replicas spreads the reading load without changing how data is split. I would also check the aggregator, because each search costs it a fixed amount of merging. Only if storage or write load is the problem would I add shards."
Interviewer: "Why not just add more shards?"
You: "Because every search asks every shard. Doubling the shards doubles the requests one search makes and makes the slowest shard more likely to be slow, so tail latency gets worse. More shards help when the data or the write rate no longer fits, and they cost fan-out. I would add them only for that."
Interviewer: "How do you handle a burst of new posts, say during a live event?"
You: "The post service keeps working, because it only writes the post and an event. The event log absorbs the burst, and workers write to the index in batches with backpressure from the index cluster. If workers fall behind, new posts appear later, and that is the acceptable kind of lag. Deletes use the separate tombstone lane, so they do not wait behind the burst. I would alert on index lag so we know the gap before users do."
Interviewer: "Can you cache search results?"
You: "Carefully. Public head queries can use a short-lived shared cache. Personalized results need the viewer in the cache key, which lowers the hit rate and raises the leak risk if permissions change. I cache pieces, such as parsed query plans or the public top candidates of a shard, and I am cautious about caching a final personalized page. Anything I cache still passes the tombstone check."
Interviewer: "What about one celebrity whose posts everyone searches?"
You: "A single celebrity post can create many searches and many privacy checks. I would give very large authors and groups dedicated shards or routing, and a separate safety queue, so their traffic does not slow everyone else, and so a takedown on their content is fast."
Interviewer: "And more than one region?"
You: "Either keep regional indexes and combine their answers, or send users to a home region so privacy is judged in one place. Strict global freshness is expensive, so I would state a policy such as local search with cross-region backfill, and say that deletes are broadcast to all regions' tombstone stores."
If you remember one thing: Replicas scale reads, shards scale data, and the aggregator's budget keeps fan-out honest.
Failure handling
Scaling answers "what if there is more traffic". This section answers "what if something breaks". Again, say it first. Failures here come from slow shards, a slow privacy service, falling-behind indexers and stale safety data, and what impresses an interviewer is that the answer was decided before the failure happened.
What you can say upfront: "Every failure has a decision made in advance. Degraded search may return fewer, slower or less personalized results, but it must never return private or deleted ones. A slow search piece is skipped after a time limit. A slow privacy service means private candidates are dropped. If index workers fall behind, only ordinary freshness suffers, and deletes use the separate fast deny list. If that deny list cannot be reached, the merge machines keep using their own copy, and if that is too stale, search fails safe instead of guessing. Events carry version numbers, so applying the same event twice does no harm."
The follow-ups are about the details of those decisions.
Interviewer: "One shard is slow. What does the viewer get?"
You: "Each shard has a time limit. After it, the aggregator answers without that shard, so the results are a bit thinner, and the response may carry a flag that it is partial. The viewer sees fewer results, not an error. Repeated slowness trips a circuit breaker for that shard so searches stop waiting on it, and an alert fires on shard p99 and timeout rate. Fewer results is degraded service, not an outage."
Interviewer: "The privacy service is down. Do you show results anyway?"
You: "Only the ones that are safe without it. Private candidates are dropped. Public posts that passed the deleted-post list and the viewer's block set may be kept, depending on product policy. Anything uncertain is hidden. Showing nothing is better than showing a private post, and I would alert on a jump in the privacy-filter drop rate."
Interviewer: "The tombstone store fails. What now?"
You: "This is safety-critical, so I treat it carefully. Every aggregator keeps an in-memory copy fed by the event stream, so a short outage of the store changes nothing. If the copy goes stale beyond a limit I choose, say a minute, I fail safe: return a temporary error and let the client retry. I do not quietly continue, because that would turn a delete into a leak."
Interviewer: "Index workers fall hours behind. What happens?"
You: "New posts and edits appear late, which is the acceptable kind of lag, and I alert on the gap between post commit time and searchable time. Because workers read from a replayable log, they catch up without losing anything. Deletes do not wait, because they also go into the tombstone store. Visibility changes are the one thing I watch closely, because an old 'public' document lingering is a privacy risk, so I can also write those into the fast lane."
Interviewer: "The same event arrives twice, or out of order."
You: "Updates are idempotent and version-checked. A worker writes a document only if the event's version is newer than the stored one, so a replay does nothing and a late older event cannot overwrite a newer one."
Interviewer: "The ranking service times out."
You: "Results become less personalized. I fall back to text score plus recency, applied after the privacy checks, so the page is still safe."
| Failure | What users or teams see | What to build |
|---|---|---|
| One index shard is slow | Search p99 rises or results look thin | Per-shard timeout, partial results policy, shard latency alerts, circuit breaker |
| Privacy service is slow | Searches hang for logged-in users | Batch checks, strict timeout, drop private candidates |
| Index workers lag | New posts missing, old versions still searchable | Index lag metrics, replayable events, separate tombstone lane |
| Tombstone store fails | Deleted content could leak if unguarded | In-memory copy on aggregators fed by the event stream; return a temporary error if it goes too stale |
| Ranking service times out | Results are less personalized | Fall back to text score plus recency after privacy checks |
| Event replay duplicates events | Version conflicts or stale writes | Idempotent updates using post_id and increasing version |
If you remember one thing: Degraded search may return fewer, slower or less personalized results. It must not return private or deleted ones.
Observability (what you measure)
Once the system is live, you need to tell whether it is healthy, and one dashboard that says "search is up" is not enough. Observe the write path, the read path and the safety path separately, because a green dashboard can hide an unsafe delete lag.
| Signal | Why it matters |
|---|---|
| Index lag from post commit to searchable, by create, edit, visibility change, delete, takedown | Shows whether new posts and safety changes are keeping up |
| Tombstone propagation time | The safety lane has its own promise |
| Event replay lag and index worker failure rate | Tells you whether search is falling behind the post service |
| p50, p95, p99 by phase: parse, shard fan-out, shard wait, merge, tombstone check, privacy batch, ranking, snippets | Finds which step is eating the time budget |
| Per-shard latency and timeout rate | Finds one slow shard before users do |
| Candidates before and after privacy, and empty-result rate | Shows thin-page problems and abusive queries |
| Privacy-filter drop rate, deleted-post hits caught by tombstones | Shows how often the index returns unsafe candidates that later guards save |
| Top queries by CPU, top users by query rate, hot word list sizes, cache hit rate | Abuse and cost control |
Alert ideas: delete-to-hidden time above its promise; index lag above budget; one shard's p99 far above the others; a sudden rise in privacy-filter drops (something upstream is returning too many unsafe candidates); empty-result rate rising together with traffic.
Security and abuse
A search box is also a way to probe for things people did not mean to share, so this section covers how to keep it honest. Privacy is the main security requirement. Every search request must run under an authenticated viewer context unless the product explicitly supports public search, and the query path should never trust a viewer id or group id supplied by the client.
Avoid leaking existence through snippets, logs, caches and errors. If a private post is filtered out, the response should look as though the post never existed. Query logs should not store raw private snippets or full viewer context, and internal debugging tools need role-based access and audit trails.
Protect the system from query abuse. Rate limit by user, IP, device and app, as in the Rate Limiter System Design guide. Reject or rewrite queries that expand into huge work, such as leading wildcards or very large OR expressions. Apply spam and safety scores during indexing so abusive posts are not rewarded by search.
Treat privacy caches carefully. Short lifetimes and version stamps help, but a stale friend list can still leak. For sensitive cases such as blocks, takedowns or private groups, prefer fail-closed behavior over showing a result from a stale cache.
Cost awareness
Every choice so far also has a price, and interviewers like to hear that you noticed. Search costs money in three places: storing index replicas, refreshing and merging segments, and running query fan-out plus ranking. A design that indexes every field and refreshes every second may be fresh, but it can make the cluster too expensive to run.
Choose fields deliberately. Index words needed for search and filters, and store some fields only for display or snippets. Avoid indexing fields with many distinct values and little value, which users rarely filter on. Media enrichment should be opt-in per field, because text read from photos, labels and embeddings can multiply storage.
Use lifecycle policies. Recent posts and hot segments live on fast storage. Older documents can move to cheaper storage or fewer replicas if the product accepts slower search for old content. Tombstones can expire after index cleanup is proven complete, while audit records stay according to policy.
Cache only where it is safe. Shared public query caches save money. Personalized result caches can leak or go stale when permissions change, so keep their lifetime short and include the viewer in the key.
Green Dashboard. Angry Users. The Production Mindset System Design Interviews Expect.
Everything so far describes how search should work on a good day. This section is about a bad day, told as short stories from the people who carry the pager (the on-call engineers). Textbooks teach boxes and arrows. On-call teaches something else: a search system can look healthy while it shows something it should not, and it can look busy while users see thin results.
A calm dashboard can mean "all is well" or "the delete lane has quietly stopped". A low empty-result rate can mean "people find things" or "we are padding pages with whatever passed". Interviewers like this gap because it separates a diagram from someone who has run the system.
Three nightmares that look fine on a dashboard:
| What the dashboard shows | What is actually happening |
|---|---|
| Index lag is 40 seconds and search latency is normal | A removed post still appears for any exact phrase, because only the normal lane is being measured |
| The typical search is fine, but the slowest ones take seconds | One hot word is overloading search pieces and the privacy batch |
| Privacy service is healthy | Viewer context was cached too long, so a just-blocked account still shows up |
Scene 1: A takedown is complete in the feed but still appears in search
The moderation team removes a harmful post from the main feed. Support confirms the feed is clean. Then someone searches for the exact phrase and sees the old snippet. The dashboard says indexing lag is only forty seconds, but forty seconds is a long time when the result is a policy violation.
The trap is treating "new posts appear within a few seconds" as enough for safety. Cleaning up search files is not instant. The searchable copy is derived from the real post, and it may keep returning an old post id until the delete event has been applied everywhere.
What to build is a separate fast hide path. Moderation writes the post id into a small deny store immediately, and search checks it before snippets and ranking. Cleaning the searchable copy still happens, but the user-visible guard does not wait for the slowest cleanup. Alert on delete-to-hidden time as its own number, and count how often the deny list catches a removed post so you can see the lane working.
Scene 2: A viral hashtag makes the slowest searches explode
Everyone searches the same hashtag after a live event. The typical search still looks fine because most searches are ordinary, but the slowest ones (often called p99, the worst one percent) take several seconds for the hot word. The merge step pulls too many candidates from too many search pieces, and then privacy checks become the new bottleneck.
The trap is assuming word lists make every word cheap. Common words create huge lists, and social search adds privacy work after retrieval. More application servers do not fix one overloaded search piece or a privacy service handling unbounded batches.
What to build is a hard work budget. Each search piece returns only its best few dozen matches and stops early when possible. The merge step caps the global pool, combines identical public queries and applies strict timeouts. For trending queries, cache the safe public head with a short lifetime, and keep personalized reordering bounded.
Hot word: "finalmatch"
shards x 50 candidates -> aggregator pool (capped) -> privacy batch (capped)
p99 per shard rises queue grows timeouts -> thin pages
Dashboard: median fine, p99 seconds, privacy-drop rate climbing
Scene 3: A block relationship changes while cached viewer context is stale
A user blocks an account that has been bothering them, and immediately searches for an old topic. If the search service uses a cached block set for too long, it may still show a post from the person they just blocked. The post service might enforce the block correctly, but search feels broken because it used older context.
The trap is treating viewer context as static. Social relationships are data too, and privacy depends on their current version. A long-lived cache can turn a fast search path into a trust bug.
What to build is a short-lived viewer context that carries a version, plus stronger handling for block events. Blocks and safety actions invalidate or bypass the cache. If the privacy service cannot confirm a relationship, the search path hides uncertain private candidates and does not show them.
In the interview: Pick one scene and tell it like a short story: what people felt, which metric lied, and what you would build next. You do not need all three. You need to sound like someone who knows that green does not mean done.
Bottlenecks and tradeoffs
Those stories all trace back to a small number of choices where you cannot have both things you want. Interviewers listen for whether you can name the tension, say what breaks, and pick a side on purpose. Here are the four that matter most for post search.
Privacy exactness vs query latency
The tension - Exact privacy checks may need the viewer's current friends, groups, blocks and custom audiences, and those checks add network calls.
What breaks - Either privacy or the slowest searches. Cache too aggressively and you risk showing forbidden posts. Check every candidate one by one and hot queries time out.
What teams do - Store simple facts in the searchable copy, then run one capped batch check. Cheap filters first, then the privacy service for a limited set.
Say in the interview - Say the cap out loud, for example "a few hundred candidates per search".
Freshness vs indexing cost
The tension - Refreshing the searchable copy more often makes new posts findable sooner but costs more CPU, disk and cleanup work.
What breaks - The bill, if every change is treated as urgent, or user trust, if takedowns wait for the next refresh.
What teams do - Two lanes: background updates for creates and edits, and a faster deny path for deletes, blocks and moderation.
Say in the interview - "New posts may take seconds; removals must not."
Personalization vs cache hit rate
The tension - Public search results are easy to reuse across people. Results that depend on the viewer are not.
What breaks - Either cost (no cache) or privacy (a shared cache serving something personal).
What teams do - Cache public pieces, such as planned queries and the public head of a search piece, and be careful with final personalized pages because permissions can change.
Say in the interview - "I cache what is the same for everyone, and I recompute what depends on the viewer."
Split by post vs split by word
The tension - Splitting by post keeps writes on one machine but makes every search ask many machines. Splitting by word can reduce how many machines a search asks, but writes touch many places and popular words become hot machines.
What breaks - The slowest searches, when you split by post. Write cost and hot machines, when you split by word.
What teams do - For a high-write social product, splitting by post is the cleaner default. Then invest in the merge step, candidate caps and per-piece time budgets.
Say in the interview - Name which cost you chose to pay, and where you pay it.
If you remember one thing: Every tradeoff here is between speed and safety, and safety wins when they conflict.
Interview tips
You now have a full design and the tradeoffs behind it, so the last thing to practice is defending it when the interviewer pushes back. These five are the pushes that come up most after you finish the design:
Do not make privacy a footnote
In the room
You might say: "We find matching posts in the index and then filter them by privacy."
Interview Push: "A common word returns ten million candidates. Are you going to check all of them? Also, what if a private snippet is fetched before filtering?"
Land here: Privacy has to shape the search path, not just dress the response. Store simple visibility facts so the searchable copy can throw out obvious misses early. Keep only a capped set of survivors and ask the privacy service about them in one batch. Fetch snippets and rank only after the deleted-post list and the privacy check have passed.
Treat deletes differently from normal freshness
In the room
You might say: "The searchable copy catches up eventually, so deletes disappear after the next refresh."
Interview Push: "A moderated post is still visible in search for one minute. Is that acceptable?"
Land here: Treat "a new post appears soon" and "a deleted post hides now" as different promises. New posts can wait a few seconds for the background update, but deletes and takedowns write to a small fast deny list that every search checks immediately. Cleaning the searchable copy still runs in the background. The user-visible rule is that removed post ids are blocked before ranking.
Pick a sharding story and name its cost
In the room
You might say: "We split the searchable copy across machines."
Interview Push: "By what, and who pays when a post has hundreds of words or a query touches every piece?"
Land here: Split by post so a write lands on one owner, then own the cost that every search asks many owners. Each piece returns its local best matches under a time limit, and a merge step combines and caps the candidates. If you discuss splitting by word, say that writes now touch many owners and hot words become hot machines.
Rank only safe candidates
In the room
You might say: "We rank results with text score, recency and social signals."
Interview Push: "Do you rank before or after privacy? Could a private post be logged or cached while you gather ranking features?"
Land here: Order the page only after the safety checks. Search pieces can compute rough local text scores, but the merge step should run the deleted-post and privacy checks before snippets and personalized reordering. Logs and caches should not contain forbidden snippets.
Close with one production failure
In the room
You might say: "We monitor search latency and errors."
Interview Push: "What incident would this system actually have?"
Land here: Pick one concrete scene: a removed post that still appears in search, a hot hashtag that makes the slowest searches crawl, or a block that is missing from a stale cache. Name the metric and the fix. For example, counting how often the deny list catches a removed post proves the safety layer is doing real work.
Red flags to avoid without correcting yourself: "filter privacy after ranking everything", "deletes wait for the next refresh", "check privacy for every match", "offset paging", "the index is the source of truth".
After each push, close with one real part of the design you would build, such as a bounded batch check, a deny list, or a version-checked update, and not "we will scale it later".
What should stick
After working through the requirements, the decisions and the depth sections, you should be able to explain:
- The searchable copy finds candidates; privacy creates results. A matching post is not safe to show until viewer permissions, blocks, group membership and the deleted-post list have passed.
- New posts can update in the background, but safety needs a faster lane. Ordinary post freshness can lag by seconds. Deletes and takedowns need a hide check while the search is answered.
- A capped candidate set protects speed. Never check privacy for an unbounded list for a hot word, and never let popularity set the cost of a search.
- How you split moves the cost. Splitting by post simplifies writes and makes every search ask many machines. Splitting by word does the opposite.
- Ranking comes after safety. Snippets, caches and logs can leak if they touch private posts too early.
Tell it in the room: "A post is saved first and announced by a durable event. Workers write a version-stamped, searchable copy with visibility facts. A search fans out to shards, merges a bounded candidate set, checks the deny list and the privacy service in a batch, then ranks the safe results and returns a cursor. Hot words need caps, and deletes need a fast deny layer because the index is a derived copy."
Quick check before a mock: (1) Why not check privacy for every match? (2) Why must a delete not wait for the index? (3) Why a cursor and not an offset? → (1) A common word matches millions of posts, so the check must be capped and batched (2) The index lags, so a fast deny list must cover removals (3) The order shifts while someone scrolls.
Practice next: Open Facebook Post Search on Practice System Design and run a timed attempt, using clarifying questions, one write and one search, without peeking until you have a draft on the board.
Key Takeaways
- A search result is a post that matches the words and that this viewer may see right now; matching alone is never enough.
- Save the post first and update the searchable copy afterwards through a durable, version-stamped event, so posting never waits on search.
- Split the searchable copy by post for cheap writes, accept that every search asks every piece, and cap the candidates at every step.
- Throw out obvious misses with stable facts in the searchable copy, then run one batched privacy check against the viewer's current context for a capped set.
- Put deletes and takedowns on a fast deny list that every search checks, because the searchable copy lags.
- Rank only after the safety checks, and page with an opaque token, not skip-and-take.
- Watch searchable-copy lag, delete-to-hidden time, the slowest search-piece latency, and the privacy-filter drop rate, not only whether searches succeed.
Related Topics
- System design interview guides - Hub of whiteboard walkthroughs
- Facebook Feed System Design - Feed delivery and ranking beside post search.
- Instagram Feed System Design - Media enrichment and background processing for searchable captions.
- Distributed Cache System Design - Viewer context caching, short lifetimes, hot keys and invalidation.
- Rate Limiter System Design - Query abuse controls and bounded work on hot paths.
- WhatsApp System Design - Privacy boundaries when servers should not expose message content.
Quick revision notes
The problem in your own words. Post search takes a few words and a viewer and returns the posts that match and that this viewer may see right now. The hard part is that matching is easy and permission is not: friends, groups, blocks and deletions all change, and the searchable copy is always a little behind.
The design in one breath. The post service saves the post and announces it through a durable event. Workers write a version-stamped searchable copy split by post across many machines. A search asks every piece, each returns its best few dozen matches, and a merge step combines a capped pool, drops deleted posts, filters with stored facts, runs one batched privacy check with the viewer's current context, ranks the survivors and returns a page token.
The choices you must be able to defend. Split by post, because writes and permission changes are frequent. Two stages of privacy, cheap facts first and a capped batch second. A separate fast deny list for deletes. An opaque page token instead of skip-and-take. Fewer results instead of unsafe ones when a helper fails.
The numbers worth remembering. Billions of posts, roughly a hundred pieces, a few hundred to a thousand candidates sent to privacy per search, a budget of a few hundred milliseconds. All of them are estimates you would confirm with a load test.
The mistakes that cost points. Filtering privacy after ranking everything, checking privacy for every match, letting deletes wait for the searchable copy, using skip-and-take paging, letting an old event overwrite a newer one, and ranking or fetching snippets before the safety checks.
Frequently asked follow-ups
- How do you enforce privacy during search?
- How does a new post become searchable?
- How do deletes and takedowns leave the searchable copy?
- How do you split the word-to-post lists across machines?
- How do you keep a common-word search fast?
Deep-dive questions and strong answer outlines
How do you enforce privacy without checking millions of matches one by one?
Store simple visibility facts with each searchable post, such as public, author-only, friends, group id, or custom audience id. Use those fields to throw out obvious misses early, then check a capped set of survivors against the friends, groups, and blocks service in one batch. The important phrase is capped set: never pull ten million matching post ids and ask the privacy service about each one alone.
How does a post enter the searchable copy?
The post service saves the post as the source of truth and records an event that must be announced. Background workers split the text into words, attach fields such as author, time, visibility, and group id, and write batches into the pieces of the searchable copy. The post becomes findable after the next refresh, often a few seconds later. Later cleanup packs small files into larger ones, so "soon" is the promise, not "the same instant."
What happens when a post is deleted or taken down?
The post service that owns the real post emits a delete or takedown event. Workers eventually remove or hide the post in the searchable copy, but every search also checks a small fast deny list of removed post ids. That means a removed post can be blocked at once even when the older search files have not been cleaned up yet.
How do you split the searchable copy across machines?
A common choice is to split by post: each machine owns a range or hash of post ids, so a new post lands on one owner, and every search asks the relevant owners. Splitting by word can make some searches touch fewer machines, but every new post then updates many owners. In a social product with heavy writes and many updates, splitting by post plus a fast merge step is usually easier to run.
Where does ranking happen?
Each piece returns its best local matches using text score and simple fields. A merge step combines them, applies privacy and deny-list checks, loads a few ranking signals, and reorders only the safe set. Ranking should not promote a post before the viewer is allowed to see it.
AI feedback on your design
After a practice session, InterviewCrafted summarizes strengths, gaps, and interviewer-style expectations—similar to a written debrief. See a static example report, then practice this problem to get feedback on your own answer.
FAQs
Q: Is Elasticsearch or OpenSearch enough?
A: It can store and query the searchable copy, but it is not the whole design. The interview is about the path around that copy: events, visibility facts, privacy checks, delete safety, asking many machines, ranking, and the limits you operate under.
Q: Should search results be perfectly fresh?
A: Ordinary new posts can show up within seconds or a few minutes. Safety deletes, account blocks, and takedowns need a faster guard, usually a small deny list checked while answering the search, because leftover private content is a trust incident.
Q: Do we build a per-user index?
A: A full private searchable copy for every person is usually too expensive for a large social network. Use shared pieces with visibility facts and viewer-aware filters. Special private surfaces may add per-user or per-group caches, but they do not replace the shared searchable copy.
Q: How do photos and text read from images fit?
A: Reading words from photos is a later enrichment job. The original post can be searchable by its own text first, then image text, alt text, labels, or captions add more searchable fields later. Do not make saving a post wait on slow image analysis.
Practice interactively
Open the practice session to use the canvas and stages, then review AI feedback.