Embedding vs Referencing in NoSQL Databases
NoSQL schemas choose between storing related data together or linking separate records based on access patterns and update behavior. Embedding favors read locality and atomic updates within one document, while referencing favors independent lifecycle, reuse, or large and frequently changing related data.
Embedding stores related information directly inside the parent document. A customer document, for example, might contain an array of shipping addresses rather than storing addresses in a separate collection. This can make common read operations simple because the application retrieves the needed data in one document lookup.
Referencing stores related entities separately and keeps an identifier, such as a customer ID, in the related record or parent document. The application then performs additional queries to assemble the complete view. References are useful when related data has its own lifecycle, is shared by many records, or can grow too large to embed safely.
The choice should start with access patterns rather than attempting to reproduce a relational normalization strategy mechanically. If data is normally read together and has a bounded size, embedding can reduce query overhead. If data is independently updated, shared, unbounded, or commonly accessed on its own, referencing can avoid duplication and oversized documents.
Embedding also affects update behavior. Duplicated embedded values may require multiple documents to be changed when the same fact changes, whereas a referenced value can have one source of truth. Conversely, references can introduce extra reads and consistency considerations because the application may need multiple operations to construct or update related data.
In a Java backend, the schema decision should be evaluated alongside transaction boundaries, query patterns, document-size limits, indexing, write frequency, and expected growth. Hybrid designs are common: embed small, tightly coupled attributes while referencing independently managed entities.
Key points
- Embed data that is usually read with its parent and has bounded growth.
- Reference data that has an independent lifecycle, is shared, or can grow without a practical bound.
- Use access patterns and update behavior as primary schema-design inputs.
- Embedding can duplicate data; referencing can require additional reads.
- Hybrid schemas can combine embedded and referenced relationships.
Interview questions
When would you choose embedding over referencing?
I would embed when related data is normally retrieved with the parent and has a bounded size. Embedding can make reads simpler because one document contains the required information. I would avoid it when the nested data is independently managed, shared broadly, or can grow without a useful upper bound.
What are the main trade-offs of referencing?
Referencing avoids duplicating independently managed data and can provide a single place to update that entity. The trade-off is that reconstructing related information may require additional queries or application-side joins. Those extra reads can affect latency and throughput, so the decision should be based on actual access patterns.
How does update frequency affect embedding versus referencing?
If a value changes frequently and is duplicated across many embedded documents, updates can become expensive and consistency becomes harder to maintain. A reference can centralize that mutable information in one record. However, embedding a value can still be appropriate when the application needs a historical snapshot rather than the current value.
How would you model a one-to-many relationship where the number of children can grow indefinitely?
I would generally avoid embedding an unbounded child collection in the parent. Instead, I would store the children separately and include a parent identifier or other reference. This allows the child records to grow independently and lets the application query only the subset it needs. I would still consider embedding small, bounded subsets when the access pattern strongly favors it.
Can a NoSQL schema use both embedding and referencing?
Yes. A document can embed tightly coupled data while referencing entities that have independent lifecycles. For example, an order could embed line-item snapshots while referencing the customer record. This hybrid approach lets each relationship follow the access and consistency requirements that fit it.