Real architecture · Senior
Instagram Explore
Real production architecture. Key points: two towers, early/late stage ranking, ig2vec, and integrity filters.
Short answer
Real production architecture. Key points: two towers, early/late stage ranking, ig2vec, and integrity filters. Explore is the Instagram surface for discovering photos, videos, and Stories relevant to each person's interests. Meta says that more than half of the Instagram community visits it every month and, in another text, that hundreds of millions of people visit it daily.
01 · CONTEXT AND SCALE
What Explore is and why it is hard
Explore is the Instagram surface for discovering photos, videos, and Stories relevant to each person's interests. Meta says that more than half of the Instagram community visits it every month and, in another text, that hundreds of millions of people visit it daily.
The core challenge is to recommend the most relevant content among billions of options, in real time and at scale, which brings several challenges of ML system design. The Meta AI post describes the result as a 3-part ranking funnel that extracts 65 billion features and makes 90 million model predictions every second.
This case uses two Meta sources: the Meta AI post about Explore and an engineering post on how the system was scaled. The second sums up the idea that runs through the text: smart use of caching and precomputation across the different ranking stages, which allows heavier models to be used at each stage.
02 · CANDIDATE GENERATION
From seed accounts to an eligible inventory
The Meta AI post divides Explore into two main stages: candidate generation (also called sourcing) and ranking.
Generation starts from accounts the person has already interacted with, for example by liking or saving media from an account, to find other accounts of interest; they are called seed accounts. Instead of looking at media by media, the retrieval pipeline focuses on account-level information.
The account embeddings come from ig2vec, a framework similar to word2vec that treats the account IDs the person interacts with as a sequence of words in a sentence. A KNN search then finds accounts topically similar to any account in the embedding, and the version described covers millions of accounts.
The sources are written in IGQL, a domain-specific language optimized for retrieving candidates. Execution is optimized in C++ to reduce latency and compute usage, and each source type is a different IGQL subquery.
The person opens Explore and the service triggers the candidate sources, each one an IGQL subquery.
The sources start from the seed accounts: accounts the person has already interacted with.
The index returns accounts similar to the seed accounts, using nearest-neighbor search over account embeddings.
Before the eligible inventory is formed, filters remove whatever cannot be recommended.
On each ranking request, 500 candidates sampled from the eligible inventory move on to ranking.
Before forming each person's eligible inventory, Meta filters out content it identifies as ineligible; besides blocking content that likely violates policies and misinformation, it uses ML systems to detect and filter content such as spam. On each ranking request, thousands of eligible media items are identified for an average person, of which 500 candidates are sampled and sent to the ranking stage.
03 · RANKING IN PASSES
Distillation, lightweight network and deep network
With 500 candidates available, the Meta AI post uses a three-stage ranking infrastructure to balance ranking relevance and computational efficiency.
The first pass is the distillation model, with minimal features, which keeps 150 of the 500 candidates.
The second pass is a lightweight neural network with the full set of dense features and picks 50 candidates.
The final pass is a deep neural network with dense and sparse features and picks 25 candidates for the first page.
| Criterion | First pass | Second pass | Final pass |
|---|---|---|---|
| Model | Distillation that imitates the other passes | Lightweight neural network | Deep neural network |
| Atributos | Minimal | Full set of dense features | Dense and sparse features |
| Candidates that move on | 150 of 500 | 50 | 25 |
The first pass is a super lightweight model that learns to approximate the main ranking models as closely as possible. The inputs and outputs of the more complicated models are logged, and the NDCG ranking loss over the main model's output is the training objective. The described gain is evaluating a larger set of media per request while keeping computational resources under control.
A multi-task, multi-label (MTML) neural network predicts the actions on each media item, which include positive actions such as like and save and also negative actions. The predictions are combined by an arithmetic formula called the value model; if saving matters more than liking, the weight for saving should be higher.
The value model also gets a simple diversity rule: posts from the same author or the same seed account are demoted by a penalty factor, which grows as you go down the ranked batch and encounter more posts from the same author.
04 · RETRIEVAL WITH TWO TOWERS
Offline embeddings and approximate search
The engineering post, from August 2023, describes a ranking approach with several well-defined stages, each with its own objectives and algorithms.
In retrieval, the role of a source is to pick hundreds of relevant items out of billions. Sources can be heuristics, such as trending posts, or more sophisticated ML approaches, real-time or pre-generated. Candidates from multiple sources are combined and passed to the ranking models, and those from pre-generated sources can be generated offline during off-peak hours, which contributes to scalability.
The Two Tower model has two separate neural networks, one for the user and one for the item. Each network consumes only features of its own entity and produces an embedding, and the goal is to predict engagement events (such as liking a post) as a measure of similarity between the user and item embeddings.
| Criterion | ig2vec | Two Tower |
|---|---|---|
| Training input | IDs of accounts the person interacts with, treated as words in a sentence | Arbitrary user and media or author features |
| Learning signal | Predict accounts the person is likely to interact with in a session | Predict engagement events as similarity between embeddings |
Two Towers extends the Word2Vec algorithm: it accepts arbitrary features and multiple tasks at the same time, for multi-objective retrieval, and keeps the maintainability and real-time nature of Word2Vec.
Outside the online path, the item tower generates item embeddings in an offline pipeline, daily.
The generated embeddings go to an approximate nearest-neighbor search service, which avoids scanning all items.
At request time, the user tower fetches the freshest features and generates the person's embedding on the fly.
That embedding queries the approximate search service, which returns the most similar items as candidates.
There is a cost: the model cannot consume user-item interaction features, because it would lose the ability to provide user and item embeddings that can be cached. The main advantage is that these embeddings can be stored in cache, which makes Two Tower inference extremely efficient.
It is also possible to use item embeddings to look up items similar to those in the person's interaction history, which gives more control over the trade-off between types of engagement. In that case, a rule-based approach filters low-quality items out of that history.
05 · RANKING AND RERANKING
Two stages, value model and final rules
Explore uses two ranking stages. The first is a lightweight model, less accurate and less costly, which can retrieve thousands of candidates. The second is a heavy model, more accurate and compute-intensive, which operates on the top 100 candidates from the first. With two stages, more candidates are ranked while keeping the quality of the final recommendations.
In the first stage, Two Tower is used again, because of cacheability. The architecture can be similar to the one in retrieval, but the learning objective differs quite a bit: the first-stage ranker is trained to predict the output of the second, a form of distilling knowledge from a larger model into a smaller one.
The first stage uses a lightweight two-tower model, which reuses cached embeddings.
The second stage receives only the top 100 candidates from the first.
The heavy model consumes the most powerful user-item interaction features.
To ease the peak, part of the recommendations is precomputed off-peak and stored.
In the final reranking, integrity and diversity rules adjust the ordered list.
The second stage predicts the probability of engagement events with an MTML model, much heavier than Two Towers, but able to consume the most powerful user-item interaction features. Applying such a heavy model at peak time can be complicated, and so the recommendations for some users are precomputed off-peak, which helps ensure availability for every Explore user.
The predicted probabilities are combined with weights in a formula called the value model (VM), the approximation of the value each media item brings to the user. Tuning the VM weights explores trade-offs between online engagement metrics.
In the final reranking, returning items ordered only by the VM score may not be a good idea: items may be filtered or demoted based on integrity scores, and business rules may reorder to increase diversity, for example to avoid showing items from the same author in sequence. These rules give much more control over the final recommendations.
The models can be retrained (fine-tuned) every hour as soon as there is new data.
06 · TRADE-OFFS AND TUNING
What changes between versions and how to tune
| Criterion | Meta AI post | Engineering post (2023) |
|---|---|---|
| Ranking stages | Three passes: distillation, lightweight network and deep network | Two stages, with final rule-based reranking |
| First stage | Distillation model, with minimal features | Two-tower model trained to predict the output of the second stage |
| Last prediction stage | Deep neural network with dense and sparse features | MTML model that consumes user-item interaction features |
There are hundreds of tunable parameters that control the system's behavior. The engineering post cites two popular tuning approaches: Bayesian optimization and offline tuning.
| Criterion | Online Bayesian optimization | Offline tuning |
|---|---|---|
| Vantagem | Only requires the set of parameters, the objective metric and the regression limits on other metrics | Sets up the experiment in hours instead of weeks |
| Desvantagem | Often takes a long time to converge, sometimes more than a month | Requires a strong correlation between offline and online metrics |
In the engineering write-up, Meta notes that the system's growing complexity brings maintainability and feedback-loop challenges, and that it is investigating consolidating the retrieval strategies into fewer, highly configurable ML algorithms.
Frequently asked questions
What is Instagram Explore?
Real production architecture. Key points: two towers, early/late stage ranking, ig2vec, and integrity filters.
What does the Instagram Explore design cover?
It is organized in these sections: What Explore is and why it is hard, From seed accounts to an eligible inventory, Distillation, lightweight network and deep network, Offline embeddings and approximate search, Two stages, value model and final rules, and What changes between versions and how to tune.
How long is Instagram Explore and what level is it?
It takes about 17 minutes to read and is rated Senior level.
Now design it yourself
Run this case as an interview in the System Design school: you make the decisions, the tutor challenges them and you see how your design scores.