A small LRU cache saves most disk reads

A hash map finds the page. A linked list remembers use order. A hit costs nothing, a miss costs a read, and writes must invalidate or update the cached copy.

More on Build a mini database engine

Keep learning

Overview

chapters · build a mini database engine A small LRU cache saves most disk reads Every index lookup reads 3 or 4 pages, and a page from disk is about 1,000x slower than one from memory. Keep recent pages in memory and evict the one used longest ago.

1. A few misses cost more than all the hits

The cache holds whole pages keyed by page id: the buffer pool (InnoDB) or shared buffers (PostgreSQL).

2. LRU is a hash map plus a linked list

Replay the lookups: the index root is a hit every time after the first, which is why upper tree levels almost never touch disk. Then run the scan with capacity 2: every request misses. Why two structures Is page P1 cached, and where? Hash map: id → node Which page is the oldest? Doubly linked list, tail Mark P1 as just used Unlink, relink at head (prev and next pointers)

3. The hit ratio depends on the pattern, not just the size

Hot set: 10 pages catch about 80% of reads, then returns flatten. Loop: a cliff: 0% until the whole loop fits. Size the pool for the working set (pages touched in a typical hour), not the whole database.

4. One big scan can flush the whole cache

Clock approximates LRU without relinking: pages sit in a ring with a reference bit, and a hand evicts the first page whose bit is clear.

5. Writes: write-through or write-back

Two ways to write On UPDATE Memory and disk, then answer Memory only, page marked dirty Disk writes One per update One per page at eviction or checkpoint After a crash ✓ Nothing acknowledged is lost ✕ Dirty pages lost, unless a log has them Real engines pick write-back and survive crashes with the write-ahead log: the change is appended to the log first, and the log is replayed on restart. One copy: updating the cached page in place is the invalidation. Freed page: drop its cache entry, or a new page with the same id gets stale bytes. ↗ Real engines do more

Recap

Disk is 1,000x to 100,000x slower than memory, so the miss ratio sets the average. Write-back is fast but needs a log to survive a crash. Next: text search, where a B-tree on the whole value cannot help. Build a full-text index.

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.