Interview: API p99 jumps 10× every night at 2 am

A report scan evicts the hot set. The buffer pool, midpoint LRU, innodb_old_blocks_time, dirty pages, warm-up after restart and memory sizing.

More on Ticket interviews

Keep learning

Overview

8-page buffer pool · 5 hot API pages · a report scans 12 pages = 0 (changed last month) report ran young sublist (protected, ~5/8) · state right after the report old sublist (~3/8) · new pages enter here · tail is evicted Scan pages are touched again within 1 s, so they stay in the old sublist and get evicted there. The hot set survives. No scan, no eviction.

API p99 jumps 10× every night at 2 am

A nightly report scans a huge table. A config tweak lets that scan push the API’s hot data out of memory. buffer pool midpoint LRU dirty pages memory sizing API p99 jumps 10× every night at 2 am

Nightly latency spike, 2 am to about 3:30 am

Every night the finance report runs, API p99 goes 40 → 400 ms. It stays high for a while after the report ends. Started a month ago. The report got faster then too. API p99 buffer pool hit buffer pool scanned table Finance report 2:00 scan 300 GB through a 48 GB cache hot pages in promote every scan page hot pages pushed out read H1–H5 miss → disk hit 91% 3:00 report done full of scan pages refill one miss at a time p99 still high 3:30 normal hit 99.8% Restated: the report’s scan evicts the API’s hot pages from memory.

What changed a month ago?

my.cnf (git blame) Someone set old_blocks_time to 0. The default is 1000 ms. A full scan of a table six times larger than memory, on the primary. buffer pool (memory cache) scanned each night table vs memory old_blocks_time (default 1000)

Memory, not CPU

Is the API slow because of CPU or I/O? hit ratio + disk reads I/O. Hit ratio fell to 91%: 9 in 100 page reads hit disk. What fills the buffer pool at 2 am? what runs then The report scans 300 GB through a 48 GB pool. Doesn’t InnoDB protect against scans? LRU design Yes. New pages enter the middle. They move up only if touched again after old_blocks_time. Why isn’t that working? It was set to 0. A scan touches each page repeatedly, so every scan page gets promoted. Why slow after the report ends? think about refill Hot pages come back from disk one miss at a time. Scan pages jump to the protected list. Hot pages evicted. Scan pages age out in the old list. Hot pages stay.

Undo the tweak, move the work

Reverts the cause, with an exact value. Moves the scan off the primary instead of buying RAM. Shrinks the work itself. Adds metrics that would have caught it.

No AI from here

What is the buffer pool, and what happens on a page read? Hash lookup page id use it evict LRU tail Read 16 KB from disk Insert at midpoint head of old list Read: hash lookup. Miss: evict the tail, read from disk, insert at the midpoint. Writes change the page in memory and mark it dirty (not yet on disk). pages, not rows hash lookup dirty pages flushed later Why does InnoDB use a midpoint LRU instead of a plain LRU? Plain LRU New page goes to the head. One big scan flushes everything. Midpoint LRU New page enters 3/8 from the tail. Promoted only if re-read later. So one big scan cannot flush the hot pages. New pages enter the old sublist, about 3/8 from the tail. They are promoted only if touched again after old_blocks_time (1000 ms). Scan pages are touched in a burst, then never again, so they age out. scan resistance promotion delay How big should the buffer pool be? The rest is for the OS and per-connection buffers (sort, join, temp). Size by working set, not total data. Watch hit ratio and reads/s. Too big causes OS swapping, which is far worse. working set vs data size avoid swap After a restart, the API is slow for 20 minutes. Why, and what helps? The cache starts empty, so every hot page is a disk read. Dump page ids at shutdown, reload them at startup (on by default). For failovers, keep the replica warm with some read traffic. cold cache warm standby Can a scan cause problems even with the default setting? Yes, just less. It still competes for disk and CPU. It churns the old sublist, evicting warm pages. Its long snapshot delays purge (cleanup of old row versions). Heavy scans belong on a replica or warehouse. I/O competition purge delay separate workloads

Finance needs the report by 6 am

What is your plan? Revert the setting + run on a replica The primary’s cache stays hot. Seconds-old data is fine for a daily report. API latency Report speed Double the RAM 96 GB still can’t hold a 300 GB scan. With the bad setting, hot pages still go. Moves the pain to a quieter hour. Other time zones still suffer. Incremental daily totals table

Have one ready

Tell me about a performance problem caused by memory, not CPU. Hit ratio drops at set times. What filled the cache. Isolate workloads, shrink the hot set.

Did you show these?

Hit ratio proves I/O, not CPU Midpoint LRU and old_blocks_time Found the config change (git blame) Why latency lingers after the report Size by working set, not data Heavy scans off the primary Dump/load for restarts

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.