Learn SQL
-- performance.replicas_read_scaling

Replicas, Caching & Retention in SQL

Lag tolerance decides what leaves the primary

--concept

Overloaded primaries shed lag-tolerant reads to replicas (dashboards yes, balances no), pre-aggregate (rollups, matviews, result caches with invalidation), shape queries (narrow columns, keyset pagination). Writes stay primary; replica lag bounds placement. Retention: time-partition + DROP PARTITION (instant, no bloat) beats giant DELETE; batched deletes with pauses are the fallback.

--how it works
1

Move lag-tolerant reads

Dashboards, reports → replicas. Balances, writes → primary.

2

Cache + precompute

Result caches with invalidation; rollups/matviews for hot shapes.

3

Shape the query

Narrow columns, keyset pagination, indexed paths — less per read.

4

Retain by partition

RANGE-partition by time; DROP old partitions instant. Batched DELETE fallback.

--visualization
nodework
primarywrites + fresh reads
replicadashboards (lag OK)
Stage 1 / 1
Retention gold: DROP PARTITION instant. Fallback: batched DELETE with pauses.
--example
query.sql
1-- batched retention fallback (repeat, advancing):
2DELETE FROM events WHERE created_at < '2023-01-01' LIMIT 10000;
Result
strategyverdict
partition-dropinstant, no bloat — gold
batched-deletereplica-friendly fallback

Dropping a 2022 partition deallocates pages with one catalog change. Batched deletes keep each txn small with pauses so replicas stay close — single giant DELETE would lock, bloat and lag.

--variants

What moves where

Lag tolerance is the router.

query.sql
1-- primary: balances, writes, read-your-write
2-- replica: dashboards, rollups, exports (lag OK)
Result
nodework
primarywrites + fresh
replicalag-tolerant scans
1Replica lag (seconds) is fine for daily revenue.
2Fatal for balance-after-transfer reads.
3Document per-endpoint tolerance.

Retention without downtime

Partition-drop gold, batched fallback.

query.sql
1ALTER TABLE events DROP PARTITION FOR ('2022-01-01');
2-- fallback:
3DELETE FROM events WHERE created_at < '2023-01-01' LIMIT 10000;
Result
strategyverdict
drop partitioninstant, no bloat
batched deletepauses, replica-kind
1Catalog change deallocates whole months.
2Batches keep txns small with pauses.
3Giant DELETE locks, bloats, lags — never.
--key facts
Lag tolerancedashboards move, balances stay
Partition-dropretention gold standard
Shapenarrow + paginated + indexed
--use cases
Primary offload: replica routing by tolerance
TTL pipelines: partitions + batched purges
--under the hood
Replica lag is WAL-replay distance — long primaries widen it; short chunked writes keep it tight.
Partition pruning skips whole children at plan time — constraint_exclusion on.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.