Data Modeling medium
In a Document Database (like MongoDB), when should you prefer 'Embedding' data (nested array) over 'Referencing' (separate collection with ID link)?
- A When the data is logically owned by the parent entity, rarely changes independently, and is almost always accessed as part of the parent's read lifecycle.
- B When the relationship between entities is strictly Many-to-Many and the child data must be frequently queried and updated across multiple disparate parents.
- C When the embedded dataset is unbounded and expected to grow indefinitely, potentially exceeding the database engine's hard limit on document size.
- D When the application requires the ability to perform complex, multi-level JOIN operations that are natively optimized by the underlying document engine.
Answer: A. When the data is logically owned by the parent entity, rarely changes independently, and is almost always accessed as part of the parent's read lifecycle.
Embedding (denormalization) provides 'Pre-Joined' performance, allowing the application to retrieve an entity and all its related details (e.g., Line Items in an Order) in a single disk I/O operation. However, this pattern introduces a risk of data bloat; if the embedded array is unbounded (e.g., thousands of user comments), referencing separate documents is preferred to stay within document size limits (like MongoDB's 16MB limit) and prevent excessive memory usage during updates.
One system design question a day, with spaced repetition so the ones you miss come back.
Get the appRelated questions
- When modeling a Distributed Transaction using the Saga Pattern (Choreography or Orchestration), what type of consistency do you achieve?
- Which practice prevents SQL Injection attacks?
- What is 'Bi-Temporal' data modeling (e.g., in SQL:2011 standard)?
- Adding an Index to a database column generally improves Read performance. What is the negative trade-off?
- Log Systems Merge (LSM) Trees (used in RocksDB, Cassandra) optimize for fast writes. What is their primary downside regarding Disk I/O?
- What is the primary objective of normalizing a relational database schema (e.g., to 3NF)?