Chapter 1. Application Design Tips
Tip #1: Duplicate data for speed, reference data for integrity
Data used by multiple documents can either be embedded (denormalized) or referenced (normalized). Denormalization isn’t better than normalization and visa versa: each have their own trade-offs and you should choose to do whatever will work best with your application.
Denormalization can lead to inconsistent data: suppose you want to
change the apple to a pear in Figure 1-1. If you change
the value in one document but the application crashes before you can
update the other documents, your database will have two different values
for fruit floating around.

Figure 1-1. A normalized schema. The fruit field is stored in the food collection and referenced by the documents in the meals collection.
Inconsistency isn’t great, but the level of “not-greatness” depends on what you’re storing. For many applications, brief periods of inconsistency are OK: if someone changes his username, it might not matter that old posts show up with his old username for a few hours. If it’s not OK to have inconsistent values even briefly, you should go with normalization.
However, if you normalize, your application must do an extra query
every time it wants to find out what fruit is (Figure 1-2). If your application cannot afford this performance hit and it will be OK to reconcile inconsistencies later, you should denormalize. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access