liveBook · Manning

Denormalization

As explained in Microservices Patterns, Second Edition

Denormalized data is data stored with deliberate redundancy to make retrieval more efficient.

Denormalization stores related data together, sometimes with deliberate duplication, so queries can retrieve it without expensive joins or separate lookups. It is useful when high query volume or large data sets make a normalized schema too slow or costly. For small, infrequently queried data sets, a normalized relational schema with joins may be simpler and sufficient. section 13.1.2 section 13.3

In a CQRS view, grouping related entities into a record can make the stored shape match the query result. A primary-key query can then retrieve the result in one lookup. The choice must also account for updates: event handlers need to find and update affected records efficiently. section 13.3.2

Order and delivery views

An order record can include its ticket and delivery so an order lookup retrieves them together. A courier’s location can also be copied into each current delivery, avoiding a separate lookup when returning a delivery. The copied location then has to be maintained as it changes. section 13.3.2

Choose the grouping with the database and operational costs in mind:

  • Document and key-value stores can nest collections in a record. In a relational database, a related collection remains in its own table. section 13.3.2
  • Duplicated data consumes storage and makes updates part of the design. In an event-driven CQRS view, propagation can lag, and a faulty event handler can leave the view stale or incorrect. section 13.2.3 section 13.3.2
  • Check whether an existing view can support the query before designing a new one; weigh query efficiency against update efficiency and infrastructure costs. section 13.3 section 13.3.2 section 13.2.3
Figure 13.6 There are often multiple ways to group records, each with different tradeoffs. A customer’s orders can be stored in a customer document, or each order can be stored in its own document.
CQRSRecordGrouping

Compare grouping a customer’s orders inside one document with storing each order separately. section 13.3.2

How Microservices Patterns, Second Edition covers it

How deep it goes
Explains grouping and duplication in query views, including database constraints and the competing demands of queries and updates.
Examples
Illustrates an order grouped with its ticket and delivery, and a courier location copied into current deliveries.
What you can do after
Evaluate denormalization against normalized joins and choose a view grouping that balances query and update efficiency.
How it connects
Connects denormalization to CQRS, normalized schemas, indexing, and event-driven updates.

Also explained in

Compare how 8 books explain denormalization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Microservices Patterns, Second Edition. The section links point to the parts of the book it draws on.