Interview: inserts stall, and only one page is hot

Auto-increment inserts queue on the rightmost leaf. Page splits, separator keys, overflow pages, and what it costs to spread the writes.

More on Ticket interviews

Keep learning

Overview

The leaf is full (the next key is higher than every key in it). Each split keeps the left page packed and opens a new rightmost page. All 8 sessions take that same latch, and the split also latches the parent to insert the separator key. The same 8 inserts land on 6 different leaves, each of which has room. The busiest latch has one session waiting. No parent update. 8 checkout workers insert at the same moment Primary key key spread by customer leaf pages · number is inserts sharing that latch this burst hot latchquiet busiest latch sessions waiting page splits

Inserts stall at peak, and only one page is hot

Every new order id is the highest yet, so every insert hits the same page. CPU is idle while dozens of sessions queue. page split separator keys rightmost leaf sibling pointers overflow pages Insert p99 hits 400 ms while CPU sits at 15% Auto-increment ids pile every insert onto one leaf page Hash-partition by customer so writes use many leaves

Checkout p99 is 400 ms. CPU is at 15%.

Started when the sale went from 50 to 400 orders/s. Each order is one INSERT. Slow ones are waiting, not running. InnoDB shows a long queue on one index page. INSERT p99 DB CPU threads on one page Restated: inserts take turns on one leaf page. More CPU will not help.

Which key does every insert touch?

The primary key is the clustered index. Every new id is higher than every id already stored, so every insert walks to the rightmost leaf. The statement is fine. The contention is in the index, not in the SQL text. Walk to rightmost leaf the only page that fits Take its latch a short page lock 63 others wait Every insert follows the same path to the same page.

Find the latch, then the reason it is one page

Is the CPU the limit? ticket metrics 15% CPU with 64 threads queued. They wait on a latch, not on work. Which structure is the queue on? InnoDB status + schema One leaf page of the primary key. That index holds the rows, ordered by id. Why that page, every time? AUTO_INCREMENT direction Ids only grow. Only the rightmost leaf can take the next one. Why does a full page make the stall worse? page capacity A full leaf splits and updates its parent too. The same edge splits again and again.

Spread the writes without pretending UUID is free

Random UUID key Writes spread, but pages end half full. Bigger cache, random I/O, big migration. Hash partition × 16 16 rightmost leaves. Id stays sequential. Lookups by id need customer_id. Names the latch and refuses the “more CPU” fix. Writes down the lookup cost in the PR. Rejects a random primary key as this patch.

No AI from here

What happens, step by step, when a leaf splits? Leaf full New page move ~half the keys Separator → parent tells search which child Parent full? split again, up to root Root split tree gains a level New page, move half the keys, push a separator key into the parent. A separator key copies the right page’s first key, guiding searches. A full parent splits too, so latches can climb several levels. If the root splits, the tree gets a new root and one more level. half the keys move separator goes to the parent root split adds a level Why is a right-only append different from a split in the middle? Right-only (sequential key) Pages stay full. Every session queues on one new page. Middle (random key) Many pages, parallel inserts. Pages half full, random I/O. Sequential keys make one convoy on the rightmost page. Random keys spread latches but leave pages half full and I/O random. Sibling pointers (leaf-to-leaf links) help range scans, not this latch. rightmost leaf is the convoy random keys trade latch contention for space and random I/O sibling pointers are for scans When does a row land on an overflow page? file format Leaf cell start of row + pointer Overflow page(s) rest of the big value Two random reads for one row. When a row is too big for the leaf’s free space. Each fetch then costs extra random reads, e.g. big JSON rows. Defragmenting packs holes but does not cool a hot edge. pointer in the cell extra random I/O fragmentation is a different problem from the hotspot How does a copy-on-write B-tree avoid updating the page in place? Write new leaf Copy each parent up to the root Swing root pointer one atomic write Readers keep the old root until they finish. It copies the changed path and atomically swings the root pointer. Readers keep the old root: a stable tree, no page latches. LMDB does this. A crash before the swing leaves the old tree intact, so no separate WAL. Cost: extra writes per change, and space until old pages are freed. path is copied, root pointer swings readers need no leaf latch space until old roots are dead Someone suggests building the index by inserting a million rows one by one. What do you say? bulk load One by one Splits everywhere. Half-empty pages. Random I/O. Bulk load Sort keys, pack leaves left to right, build parents on top. Use a bulk load: sort, then pack leaves full, then build upward. One-by-one inserts split pages and leave them half full. sort then pack bulk load is a rebuild tool

The sale is today. A full repartition is a week of work.

What do you ship before the next peak? Keep the key. Drain the queue with a single writer that batches inserts. One writer owns the leaf, so the convoy goes. Capacity is capped at one writer’s speed. Latch queue Peak capacity Change size Hash-partition by customer_id Replace the primary key with a random UUID Inserts spread, but pages go half full and I/O turns random. A painful migration, too. That eases the auto-increment lock, not the page latch. The queue stays.

Have one ready

Tell me about a time the database was idle and the queue was still long. Latency, concurrency, low CPU. The one page or lock everyone needed. Batch, partition, or new key, and its cost. An alert for a single hot page.

Did you show these?

Read low CPU + long queue as latch contention Tied AUTO_INCREMENT to the rightmost leaf Walked through a split, up to a new root Contrasted right-only vs middle splits Mentioned overflow pages and copy-on-write Refused “add CPU” and “UUID” as first patch Named the cost of a spread-out key

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.