NoSQL · Data models beyond tables
When do you embed data and when do you reference it?
Short answer
Embed data that is read and written with its parent and has a bounded size. Reference data that is shared, changes independently, grows without bound, or is too large to duplicate.
Embedding keeps an aggregate together, so one read returns the whole object and one write is atomic within the document. Referencing stores an id and requires a second lookup or a join, but avoids duplication and unbounded growth.
{
"_id": "order_1042",
"customer": { "id": "cus_7", "name": "Asha" },
"shippingAddress": {
"line1": "12 MG Road",
"city": "Bengaluru",
"postalCode": "560001"
},
"items": [
{ "sku": "sku_1", "qty": 2 },
{ "sku": "sku_9", "qty": 1 }
]
}Rules of thumb
- One-to-one or one-to-few with bounded size: embed
- One-to-many with a known small maximum: embed if the parent is always read with the children
- One-to-many with unbounded growth: reference or use a child collection
- Many-to-many: reference, because duplication would explode
- Data shared across many parents and updated often: reference
Consider access frequency as well as cardinality. Embedding 50 line items is fine if the order page always reads them. Embedding 50 items that are only needed on a rare detail view wastes space on every list query.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1How would you model a blog post and its comments?
- 2What changes if comments can be edited by their author?
- 3How do you handle a many-to-many relationship?
- 4What is the risk of embedding a snapshot of a mutable field?
Say it in the interview
I embed when the child is read and written with the parent and has a bounded size, because one read and one atomic write is a strong advantage. I reference when the data is shared, changes independently, grows without bound, or is only needed on a rare screen. Cardinality alone is not enough; I also look at access frequency and document rewrite cost. For many-to-many relationships I reference, because embedding both sides would duplicate far too much.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live NoSQL mock interview and get scored feedback on depth, clarity and confidence.