Why the first query is slow: cold reads

An empty cache means every page is a disk read. Warm-up, cache preloading, and why benchmarks must run twice. A free visual lesson with sample data.

More on Running a database in production

Keep learning

Overview

Database cache (in the server process) OS page cache (file cache in RAM) ◐ Yes, until reboot or memory pressure Local SSD / NVMe ✓ Yes Network block storage (cloud) Spinning disk Run it 3 to 5 times Count cache misses per run A warm run should add almost none. If it does, the data does not fit. Use production-sized data A table that fits in RAM hides the disk entirely. Clear both caches for a cold number Restarting the database alone may leave the OS cache warm. Report both numbers + cache size Cold and warm answer different questions. chapters · running a database in production Why the first query is slow: cold reads After a restart the database cache is empty, so every page is a disk read. The same query can take 3 s once and 30 ms the next time.

1. The first run pays for the disk

Slowness that fades on its own minutes after a restart is the cold-cache signature. Slowness that stays is a plan problem (see the query questions).

2. Two caches: the database’s and the OS’s

A page read checks the database cache first, then the OS page cache, then the disk. The same page can sit in both caches, which is double caching. Direct I/O skips the OS cache. Database cache server process · ~1 µs page 7 OS page cache kernel · ~20 µs direct I/O: skip the OS cache Restart the database: left box empties, middle box may stay warm A miss in the database cache may still be a cheap OS-cache hit. Dashed path: direct I/O. Where a read can be answered from (typical, not promised) Cost per page Survives a database restart? Direct I/O makes the database cache the only cache, so RAM is not spent twice. The price: a database restart no longer leaves a warm OS cache behind, so cold starts get colder.

3. Warm up on purpose: save the list, preload it

Partial list. Record a share of the cache at least as large as the hot set. Throttle or stop it if the preload hurts users.

5. Benchmark hygiene and the cold replica

Benchmark checklist The primary's cache holds pages that were read and pages that were written. A replica that serves no reads holds only the written pages, so after failover the read-only hot pages are cold. Primary cache Idle replica cache Promote the replica: every R becomes a disk read under full traffic Failover is the production cold start. Fixes: send a trickle of real reads to standbys, replay sampled reads on them, preload the hot list, and shift traffic to a new node gradually. ⚙ In real engines

Recap

The OS page cache is a second cache. Direct I/O avoids double caching but makes restarts colder. Read-ahead turns many small waits into one large read for in-order scans. Benchmark cold and warm on real-sized data, and warm a replica before it takes over.

More courses

  • Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
  • Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
  • React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
  • System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
  • NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
  • Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
  • Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
  • AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
  • Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
  • TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
  • CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
  • Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
  • Docker — What an image is, why layers cache, and how containers find each other on a network.
  • Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
  • Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
  • AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
  • Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.