Database Pipelines — Part 4/5

Parts 1–3 covered plans, storage, and transactions. Runtime still hinges on whether the next page or object is already warm. This article separates database page caches from application object caches, then covers cache-aside, invalidation, and stampede control. Examples use PostgreSQL where noted; other engines differ—call that out rather than universalizing.

This is Part 4 of 5 in the Database Pipelines series.

Tiers from app to disk

Cache tiers: app, statements/pool, shared buffers, OS page cache, disk

For the Fairfax join, data might be satisfied from:

  1. Application cache (process memory, Redis, Memcached)—requires an explicit consistency story;
  2. Pool / prepared statement reuse—amortizes connect and parse costs (Part 5);
  3. Database shared buffers (PostgreSQL) / buffer pool (other engines);
  4. OS page cache;
  5. Disk or network block storage.

A failover that cold-starts shared buffers, or an app cache serving stale status, both look like “the database is wrong” if you do not name the tier.

Engine caution: MySQL’s buffer pool, SQL Server’s buffer pool, and PostgreSQL’s shared buffers share the idea of a page cache, but sizing advice, double-caching with the OS, and whether a product also offers a transparent query-result cache are product-specific. PostgreSQL core does not provide a general transparent result cache for arbitrary SELECTs.

Shared buffers: page cache inside the engine

Buffer hit and miss paths into a pinned page

In PostgreSQL:

  • Hit: pin a resident page for the executor;
  • Miss: choose a victim, write it out if dirty and required, read through the OS into the pool, pin.

EXPLAIN (ANALYZE, BUFFERS) shows shared hits versus reads for that execution—useful diagnostics, not a portable benchmark number to paste across environments.

Dirty pages leave via background writers and checkpoints (Part 2). Checkpoint frequency trades recovery time against write spikes.

Sizing shared_buffers interacts with the OS page cache. Leaving RAM for the kernel is normal on PostgreSQL; copying MySQL “assign most of RAM to the buffer pool” folklore blindly is an unsafe generalization.

OS page cache

The kernel may retain file blocks after PostgreSQL evicts them from shared buffers. A database “read” can be an OS-cache hit. State whether benchmarks were warm. Container memory caps change eviction. This tier is host behavior, not SQL syntax.

Application caching: cache-aside, invalidation, consistency

Cache-aside read path with singleflight and write-path invalidation

Cache-aside (lazy loading)

  1. App looks up a key (for example, order:1001).
  2. Hit: return cached value.
  3. Miss: load from the database, populate the cache with a TTL and/or version, return.

This is common and honest: the database remains source of truth; the cache is an optimization.

Write path consistency choices

When UPDATE orders SET status = 'paid':

  • Invalidate the key (delete order:1001) so the next read reloads—simple, brief stampede risk on hot keys;
  • Update the cache with the new value in the same app transaction after DB commit—faster reads, harder to get right if multiple writers;
  • Read-through / write-through libraries that wrap both—still need failure handling when cache and DB diverge.

Never claim the cache is correct if you skip invalidation on writes. Multi-key assemblies (customer + orders) cached separately can recombine into a join that never existed at one transaction snapshot (Part 3).

Stampede (thundering herd) controls

When a hot key expires, many requests miss together and stampede the database:

  • Singleflight / request coalescing: one miss loads; others wait for that fill;
  • Soft TTL + background refresh: serve slightly stale while one worker refreshes;
  • Probabilistic early expiration: jitter so not all keys expire on the same second;
  • Lock around miss (with care to avoid deadlocks and lock pileups).

Pair these with statement timeouts and pool admission (Part 5) so a stampede cannot open unbounded backends.

What belongs in an app cache

Safer Riskier as “truth”
Immutable copy / catalog text Mutable orders.status without invalidation
Computed fragments with short TTL and clear SLO Negative (“no rows”) caches that outlive inserts
Feature flags with version keys Cross-row joins stitched from independent keys

Plans and prepared statements

Caching parse/plan work (prepared statements) is different from caching row payloads. Parameter-sensitive plans can be wrong for skewed values—PostgreSQL documents generic vs custom plans for prepared statements. That is plan reuse, not a substitute for buffer cache.

Tying it to the running query

SELECT c.email, o.order_id, o.total_cents FROM customers AS c JOIN orders AS o ON o.customer_id = c.customer_id WHERE c.city = 'Fairfax' AND o.status = 'paid' AND o.created_at >= TIMESTAMP '2026-01-01'; 

Warm index and heap pages make the same Part 1 plan feel instant; cold caches after restart make it look broken. Fix warmth and buffer sizing before rewriting SQL in a panic. If you cache the result set in Redis, define freshness relative to commits that flip status.

What to practice

  1. Name which tier you mean when you say “cache.”
  2. Use BUFFERS (PostgreSQL) when diagnosing page I/O.
  3. Prefer cache-aside + invalidate-on-write for mutable orders data.
  4. Add singleflight or jitter before scaling out readers that share one hot key.

Limits and next

CDN edge caches and ORM identity maps are out of scope. Part 5 closes with connection pooling and an end-to-end request path from pool admission through planner, snapshot, buffers, storage/WAL, and response.

References

About the author

I write about Applied AI, MCP, Azure AI Search, and RAG. Follow me on Dev.to and Hashnode for more.