Materialized View

Precompute the data a screen or report needs into a ready-made table, and keep it updated, instead of joining and aggregating on every request.

Description

Some views are expensive to build: "top products this week with stock and ratings" joins orders, inventory and reviews, maybe across several services. Computing that on every page load is slow and loads every source.

A materialized view stores the finished result in a table or document shaped exactly for the screen. It's refreshed when the sources change, through events or on a schedule, so reading it is one cheap lookup. The view is disposable: if it's wrong or the shape changes, rebuild it from the sources.

System design

sales changed1stock changed2new rating3upsert product row4read top 205Orders DBInventory DBReviews DBView builderon change eventsTop products viewone row per productShop front page

Materialized View system design diagram

  1. Orders DB (database).
  2. Inventory DB (database).
  3. Reviews DB (database).
  4. View builder (function): on change events.
  5. Top products view (database): one row per product.
  6. Shop front page (client).
  7. Step 1: Orders DB → View builder: sales changed.
  8. Step 2: Inventory DB → View builder: stock changed.
  9. Step 3: Reviews DB → View builder: new rating.
  10. Step 4: View builder → Top products view: upsert product row.
  11. Step 5: Shop front page → Top products view: read top 20.
  1. When sales change, the orders database emits an event to the view builder.
  2. Inventory changes are sent the same way.
  3. So are new ratings from the reviews database.
  4. The builder updates just the affected products in the precomputed view.
  5. The front page reads the top 20 rows in one query: no joins, no aggregation, no load on the source databases.
AWS
DynamoDB Streams or EventBridge with Lambda writing to DynamoDB; Redshift materialized views.
Azure
Cosmos DB change feed with Functions; Azure SQL indexed views; Synapse materialized views.
Google Cloud
BigQuery materialized views; Pub/Sub with Cloud Run writing to Firestore.

When to use it

Use it when

  • A screen or API needs data joined or aggregated from several tables, services or databases.
  • The same expensive query runs again and again with the same answer.
  • The source databases are not designed or allowed for this query load (legacy systems, other teams' services).

Avoid it when

  • The query is already fast with proper indexes.
  • The data must be perfectly current on every read; views lag behind their sources.
  • The view changes on almost every write, so you'd recompute it constantly.

Pitfalls

  • Staleness surprises. Show when the data was last updated, or keep refresh latency within what users accept.
  • Views that can't be rebuilt from scratch. Always keep a full rebuild path for after bugs or schema changes.
  • Many views each recomputed from full scans. Update incrementally from change events where you can.

Alternatives

Cache-Aside

Choose it when you just want a faster copy of individual items, not a new shape of data.

CQRS

Choose it when you're splitting the whole read side from the write side; materialized views are its building blocks.

Database materialized views

Choose it when everything lives in one database that supports them (PostgreSQL, BigQuery, Snowflake).