← Lessons

quiz vs the machine

Platinum1840

System Design

Precomputation and Materialization

Doing expensive work ahead of time so reads are fast and cheap.

5 min read · advanced · beat Platinum to climb

Shift work off the read path

Some answers are costly to compute on demand, such as an aggregate over millions of rows. Precomputation does that work ahead of time and stores the result so a read is a simple lookup.

Forms it takes

  • Materialized views store the result of a query and refresh on a schedule or on change.
  • Denormalized fields keep a precomputed count or total next to the record.
  • Rollups summarize raw events into hourly or daily buckets.
  • Caches are a lightweight materialization of recent results.

The freshness tradeoff

Precomputed data can be stale until it is refreshed. You choose how to update it.

  • On write keeps it fresh but adds cost to every write.
  • On schedule is cheap but lags reality between refreshes.
  • Incremental updates only the changed part, balancing cost and freshness.

When it pays

Precompute when reads vastly outnumber writes and the computation is heavy. If data changes faster than it is read, the refresh cost can outweigh the savings.

Key idea

Precompute and materialize heavy results so reads are cheap lookups, accepting some staleness chosen by how you refresh.

Check yourself

Answer to earn rating on the learn ladder.

1. What does precomputation move off the read path?

2. When is precomputation least likely to pay off?