Skip to content
Prompt Words
History Member

Read model

Also called: CQRS read side, projection, query model.

A separate copy of your data, shaped for one screen or one question, apart from the tables you write to. Usually a projector takes each change from the write side and updates it (some are rebuilt on a schedule instead), so a page reads ready-made rows with no joins. It is fast to read, but it can be a moment behind the latest write.

Open the dashboard reads order_summaries, a table made for this screen. The old way joins 3 tables on the write side. Placing an order shows the read model's lag (1 s here, slowed down).

Rows read: 0 · Query time: none yet · Projector: up to date

The dashboard shows the 3 biggest orders this month, out of 2,000 orders.

    No reads yet

    Say it in a prompt

    Add an order_summaries read model for the orders dashboard: one row per order with month, customer_name, item_count and total, indexed on (month, total). A projector consumes OrderPlaced and OrderChanged events and upserts the row by order_id, so a repeated event does no harm. The dashboard reads only order_summaries, with no joins on the write tables. Alert if the projector falls more than 5 seconds behind.

    Vague vs precise prompt

    Vague prompt

    the orders dashboard is slow, make it faster

    Typical resultAdds indexes and caches the slow query for a minute. The dashboard still joins orders, customers and order_items on the database that takes every checkout, and the cache shows old totals with no rule for when they change.

    Precise prompt

    Add an order_summaries read model (order_id, month, customer_name, item_count, total), filled by a projector that consumes OrderPlaced events and upserts by order_id. The dashboard reads only order_summaries. Alert if the projector lags more than 5 s.

    Typical resultThe dashboard reads 3 ready rows in a few milliseconds and puts no load on the write tables. A new order shows up as soon as the projector applies its event, and the lag is measured and alerted on.

    Seen on

    • Martin Fowler: CQRS means using one model to update information and a different model to read it. Having separate models makes eventual consistency more likely.
    • Azure Architecture Center: The CQRS pattern: a common way to keep the read side in sync is for the write model to publish events that the read model uses to refresh its store. That store can hold a materialized view that avoids complex joins, and it may not show the newest changes at once.
    • Azure Architecture Center: The Materialized View pattern: build a read model ahead of time in the shape a query needs, like a sales summary a dashboard reads instead of combining orders, order items and customers on every request.

    You might describe it as

    • a table made just for the dashboard
    • work out the totals ahead so the page has nothing to join
    • the list page reads its own copy, shaped for it

    Not to be confused with

    • Read replica

      A read model is a new table you design for one screen and fill from events with your own code; a read replica is the whole database copied as it is, so the same joins still run there.

    • Eventual consistency

      A read model is a table built so one screen reads fast; it is usually eventually consistent, so right after a write it can miss the change for a moment.