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
Materialized View system design diagram
- Orders DB (database).
- Inventory DB (database).
- Reviews DB (database).
- View builder (function): on change events.
- Top products view (database): one row per product.
- Shop front page (client).
- Step 1: Orders DB → View builder: sales changed.
- Step 2: Inventory DB → View builder: stock changed.
- Step 3: Reviews DB → View builder: new rating.
- Step 4: View builder → Top products view: upsert product row.
- Step 5: Shop front page → Top products view: read top 20.
- When sales change, the orders database emits an event to the view builder.
- Inventory changes are sent the same way.
- So are new ratings from the reviews database.
- The builder updates just the affected products in the precomputed view.
- 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).