In this lesson, you will learn how to maintain high performance in your document database when a small percentage of your data grows far beyond the typical size constraints of a document.
The Problem: When "Typical" Fails
In document models, we often use the Embedding Choice Criteria to store related data within a single document for speed. However, this strategy assumes a predictable, bounded growth. What happens if a "typical" user has 50 entries, but 0.1% of your users have 50,000?
If you embed these massive arrays directly, you will hit document size limits (like MongoDB’s 16MB cap), experience massive latency during write operations, and suffer from poor cache utilization. This is the Outlier Problem.
The Solution: The Outlier Pattern
The Outlier Pattern is a hybrid strategy. You continue to use the Embedding Choice Criteria for your "normal" documents, but you provide an escape hatch for the outliers.
- Flag the document: Add a boolean field (e.g.,
is_overflow: true) to the document. - Handle the overflow: For the majority of documents, store the data in an embedded array. For the outliers, store that data in a separate, referenced collection.
Consider a Product document where you embed the most recent reviews.
// Typical Product document
{
"_id": 1,
"name": "Widget",
"recent_reviews": [...], // The last 50 reviews
"is_overflow": false
}
// Outlier Product document
{
"_id": 2,
"name": "Massive Seller",
"recent_reviews": [],
"is_overflow": true // Directs the app logic to look elsewhere
}
When your application fetches a product, it checks the is_overflow flag. If it's false, it renders the embedded array. If it's true, the application executes a separate query to fetch the reviews from a dedicated reviews collection using the Referencing Join Pattern.
By sacrificing a tiny bit of application logic complexity, you ensure that your common-case read operations remain lightning-fast and your database avoids the memory overhead of ballooning documents. You keep your "common case" optimized for speed while keeping your "edge cases" from breaking the system.
Key Takeaway: Use the Outlier Pattern to maintain the performance benefits of embedding for the majority of your data while seamlessly offloading extreme cases to a secondary collection.