Command Palette

Search for a command to run...

Hectal
Phase 12Intermediate13 of 18 in Database Design

Caching, Read Models and Polyglot Persistence

Cache-aside and friends with invalidation and stampede control, materialized views and summary tables, CQRS read models, polyglot persistence with one source of truth, and schema patterns: soft delete, history, temporal and audit tables.

Most large systems serve reads from copies of the data: caches, materialized views, search indexes, read models. The design question is always the same: who owns the truth, how does each copy get updated, and how stale may it be?

0/4 · 0%
4 topics ~34 min 5 code blocks & diagrams
Start with the first topic
1
12.1

Caching in Front of the Database

Caches (in-process and distributed) cut latency and database load for hot reads. Cache-aside is the default: read the cache, load from the database on a miss, and delete the key after writes commit. Correctness problems come from invalidation races, stampedes on hot keys, penetration by nonexistent keys and synchronized expiry. Each has a standard defence.

9 min 1 code practice

2
12.2

Materialized Views, Summary Tables and CQRS Read Models

A materialized view stores a query's result so expensive aggregations become cheap reads. PostgreSQL refreshes them fully (REFRESH MATERIALIZED VIEW CONCURRENTLY keeps them readable); incremental maintenance uses summary tables updated by triggers, jobs or event consumers. CQRS generalises this: writes go to a normalized model, reads come from projections built for each screen.

8 min 1 diagram 1 code practice

3
12.3

Polyglot Persistence: One Truth, Many Copies

Real systems combine PostgreSQL, Redis, Kafka, Elasticsearch, object storage and sometimes Cassandra or MongoDB. For each piece of data, name the source of truth and the synchronisation mechanism (same transaction, outbox/CDC, cache-aside, batch) for every copy, with its consistency, durability and staleness budget.

8 min 1 code practice

4
12.4

Schema Patterns: Soft Delete, History, Temporal, Audit and Event Sourcing

Recurring schema patterns: soft delete (mark instead of remove), history and audit tables (who changed what, when), temporal tables with validity ranges (what was true at time T), snapshots, event sourcing (store events and derive state), plus the repository and unit-of-work patterns for application access. Each solves a real need and adds cost.

9 min 1 code practice