System Design Playbook
10 · Data & databases7 min read

Precomputing values: what it is and when to do it

Instead of recalculating an expensive result every time someone asks for it, calculate it when the data changes and store it. The average GPA of a million students, follower counts, leaderboards and dashboards, and the trade-offs that come with it.

In one sentence

Precomputing means doing the expensive work at write time (or on a schedule) and storing the result, so every read is a cheap lookup instead of a fresh calculation.

The example: the average GPA of 1M students

A dashboard shows the average GPA of all students. The naive way runs SELECT AVG(gpa) FROM students on every page view. With 1 million rows, every request reads a million rows, and if 50 people open the dashboard at once, that's 50 million rows read for the same number.

The precomputed way keeps a tiny gpa_stats row with count and sum. When a grade is written, it updates those two numbers. Reading the average is sum / count: one row.

Average GPA: compute on read vs precompute on write
Students
Compute on every readSELECT AVG(gpa) FROM students;students · 1,000,000✎ INSERT the gradeRows read: 1,000,000 per request · ~500 msPrecompute on writeSELECT avg FROM gpa_stats;students · 1,000,000gpa_statscount 1,000,000sum 3,120,000avg 3.1200✎ INSERT the grade + UPDATE count & sumRows read: 1 per request · instant
Requests
0
Rows read · Compute on every read
0
Rows read · Precompute on write
0
Average GPA
3.120
Both sides always show the same average. The left one recalculates it from every row on each request; the right one keeps a running count and sum, updated when a grade is written, so reading is a single row.
Both strategies always show the same average. Compare how many rows each one has read after a few requests.
-- on every new grade, in the same transaction
INSERT INTO grades (student_id, gpa) VALUES ($1, $2);
UPDATE gpa_stats SET count = count + 1, sum = sum + $2;

-- reading is constant time
SELECT sum / count AS avg_gpa FROM gpa_stats;

Where you see it everywhere

FeaturePrecomputed as
Followers / likes / views countA counter column updated on each event (or in batches)
Your social feedFan-out on write: a new post is pushed into followers' feed lists
LeaderboardsA Redis sorted set updated when scores change
Dashboards and reportsMaterialized views or summary tables refreshed every few minutes
"Because you watched…" rows on NetflixRecommendations computed offline in batch jobs
SearchAn inverted index built when documents change

Three ways to precompute

  • On write, synchronously: update the aggregate in the same transaction. Always exact; makes writes a bit slower.
  • On write, asynchronously: publish an event and let a worker update the aggregate. Fast writes; the value lags slightly.
  • On a schedule (batch): recompute every N minutes or nightly (materialized view refresh, cron, Spark job). Simple for heavy aggregates; data is as old as the last run.

When it's worth it

Precompute when

  • The result is read much more often than the data changes
  • Calculating it is expensive (big scans, joins, ML)
  • A slightly old value is acceptable, or you can update it exactly on write

Don't when

  • The data changes far more than it's read
  • Each user needs a different, unpredictable calculation
  • The query is already fast with the right index

Precomputing moves cost from reads to writes and adds data you must keep consistent. In a read-heavy system that's usually a great deal.

Key takeaways

  • 01Precomputing turns an expensive read into a cheap lookup by doing the work when data changes.
  • 02Keep aggregates like count and sum, and adjust them on insert, update and delete.
  • 03Choose synchronous, asynchronous or batch depending on how fresh the value must be.
  • 04It pays off when reads vastly outnumber writes.

Check your understanding

1 of 3

What does precomputing mean?

Related concepts