Course contentsShow
← Databases & SQL
Lesson 1204 of 1,95311. NoSQL and Document ModelsFree lesson

The Subset Pattern

Keep working sets small by embedding only a limited number of items in the main document.

In this lesson, you will learn how to optimize document performance by embedding only the most relevant, frequently accessed data within your primary documents.

When working with document models, a common pitfall is the "unbounded array" problem. If you have a one-to-many relationship—like a User and their Comments—you might be tempted to embed every single comment inside the User document. While this keeps data localized, it eventually causes the document to grow beyond the storage engine's optimized size, leading to slow disk I/O and wasted memory as your application retrieves thousands of irrelevant comments just to fetch a user profile.

The Subset Pattern solves this by storing only the "working set" inside the main document.

The Strategy

Instead of embedding every related item, you embed only the most recent or most important ones. For the User example, you might embed only the 5 most recent comments directly in the User document. If the user needs to view their full history, the application performs a separate query to a dedicated Comments collection.

// The User document with a subset of comments
{
  "_id": "user123",
  "username": "jdoe",
  "recent_comments": [
    { "id": 105, "text": "Great article!", "date": "2023-10-01" },
    { "id": 104, "text": "Very helpful.", "date": "2023-09-28" }
  ]
}

Why it Works

By limiting the embedded array to a fixed, small number of items, you ensure that:

  1. Predictable Size: Your primary document size remains stable, preventing performance degradation as the relationship grows.
  2. Efficient Reads: You fetch the data you use 90% of the time in a single, fast read operation.
  3. Improved Locality: The working set stays small enough to fit efficiently in the database cache, significantly speeding up common queries.

The Subset Pattern is essentially a trade-off: you accept a slightly more complex application-level logic to handle the "full list" fetch in exchange for significantly faster performance during standard user interactions.

Key Takeaway: The Subset Pattern optimizes performance by embedding only the most frequently accessed data within a document, offloading the rest to a separate collection to maintain a lean, predictable working set.