Dragonfly Cloud announces new enterprise security features - learn more

Question: How does using references impact performance in MongoDB?

Answer

In MongoDB, data relationships can be represented in two main ways: embedding and referencing. When considering the impact of using references on performance, several factors come into play.

1. Read Performance

Using references generally means that your data is normalized across collections. When you fetch a document that contains references to other documents, you'll typically need to perform additional queries to retrieve those referenced documents. This results in multiple round trips to the database, which can significantly decrease read performance compared to having all the data embedded in a single document.

For example, consider a blog application with separate users and posts collections. If post documents include a reference to their author's user document, retrieving a post along with its author's information would require two queries:

// Find the post const post = await db.collection('posts').findOne({ _id: postId }); // Find the referenced user const user = await db.collection('users').findOne({ _id: post.userId });

2. Write Performance

When using references, updates that span multiple documents are more complex and might require additional write operations compared to embedded documents. However, since each document is independent, there's less risk of hitting the BSON document size limit (16MB as of my last knowledge update). For large, complex datasets, this could mean better performance and scalability in writes because smaller, more focused updates are faster and can reduce contention.

3. Data Consistency

Referenced data models can complicate maintaining consistency, as updates to related data might need to be performed across multiple documents and collections. MongoDB transactions (introduced in version 4.0 for replica sets and extended in 4.2 for sharded clusters) can mitigate this issue by allowing atomic operations across multiple documents. However, transactions can introduce overhead and potentially affect performance.

Best Practices

  • Use references when your data is naturally separate, doesn’t frequently access related data together, or when the data set is too large to embed.
  • Consider embedding when accessing related information together is common and critical for performance, and the related data does not exceed the document size limit.
  • Evaluate the use of MongoDB’s $lookup aggregation pipeline stage or DBRefs for populating referenced documents in queries, understanding that $lookup can also impact performance at scale.

In summary, the choice between embedding and referencing in MongoDB should be guided by your specific application’s data access patterns and requirements. While referencing provides a normalized data model that can be beneficial for data consistency and avoiding data duplication, it can also lead to increased complexity and potential performance drawbacks due to the need for multiple query operations to assemble related data.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Switch & save up to 80% 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost