Interview: “just add an index” slowed ingestion

Six new indexes, 7 B+tree writes per insert and a UUID key. Price each index in CPU, disk and memory, and roll back safely.

More on Ticket interviews

Keep learning

Overview

Prefix of idx_user_type_created. 12 distinct values. Rarely selective on its own. Serves “latest events” for the admin home page. (user_id, type, created_at) Serves the per-user activity filter. UNIQUE (payload_hash) Dedupe on ingest. Unique: no change buffer. 3 distinct values. The optimizer often prefers a scan. Activity for one user, one type, last 7 days Latest 50 events overall Has this payload been ingested already? Count events per status (dashboard) Primary key type PK BIGINT auto-inc PK UUIDv4 B+trees per insert secondary size inserts / s Write CPU + redo Random disk reads Buffer pool pressure Ingest headroom Ingest keeps up with headroom. Random UUIDv4 keys insert all over the clustered index, which splits pages and keeps more pages hot. Which admin query still has an index? served by full scan of 500M rows · barely helps, 1 in 3 rows match · partly (user only) Rough model for intuition, not a benchmark. Entry size ≈ key + primary key + ~14 bytes overhead.

“Just add an index” slowed ingestion to a crawl

Indexes speed up reads and tax every write. Can you price the tax and pick which to keep? write amplification buffer pool change buffer UUID keys invisible indexes Inserts fell from 12k to 4k per second Six new indexes: seven tree writes per insert

Event ingestion falling behind since Tuesday

Data platform Tuesday: we added 6 indexes to events for admin filters. Since then inserts dropped from 12k/s to 4k/s. Ingest is now 40 minutes behind. inserts / s ingest lag 40 min buffer pool hit 1 INSERT one event row 7 B+trees table + 6 indexes Indexes > 64 GB RAM leaf pages fall out of memory Disk reads per insert random I/O ingest falls behind Restated: each insert does far more work, and the hot data no longer fits in memory.

The migration and the evidence

index usage Three indexes are written 84M times a week and never read. Never read. Prefix of the composite. Never read. Only 12 values. Never read. Only 3 values. Serves “latest events”. Serves the per-user filter. Dedupe. Read on every insert. keep, costly

Price each index

What does one insert do now? count B+trees 7 tree inserts instead of 1. Each one also writes to the redo log (crash-safety journal). Are all of them used? index usage stats No. idx_user, idx_type and idx_status were never read in 7 days. Are any redundant? Yes. idx_user is the first column of the composite. The composite covers it. Why did the buffer pool hit rate drop? index sizes vs RAM Indexes grew past the 64 GB memory cache. Inserts now wait on disk reads. Anything about the key itself? look at the PK Random UUIDv4 keys insert all over the table and split pages. Every index copies the 36-byte key.

Turn indexes on and off

Watch the B+tree count, memory, insert rate, and which admin queries lose their index.

A safe, reversible plan

Reversible first: an invisible index can come back instantly. Invisible indexes still cost writes. Only DROP wins throughput back. Treats the UUID change as a design decision, not a hotfix.

No AI from here

Walk me through what InnoDB does for one INSERT. Clustered index full row, by PK Each secondary key + PK Change buffer non-unique, page not in RAM Redo + binlog flushed at commit Dirty pages written later One row write, then one entry per secondary index, then the log flush at commit. Non-unique indexes can defer misses to the change buffer (a queue of pending index edits). Unique indexes must read the page to check duplicates, so no deferral. clustered vs secondary change buffer unique needs a read redo at commit Why does a UUIDv4 primary key hurt InnoDB? UUIDv4 key Inserts land anywhere. Many hot pages, splits, half-full pages. BIGINT or UUIDv7 Inserts append at the end. One hot page, full pages. Random keys scatter inserts, so pages split and more of them must stay in memory. Every secondary index copies the primary key, so a big key bloats them all. Fix: BIGINT auto-increment, or time-ordered UUIDv7 stored as BINARY(16). page splits PK copied into every secondary Why might the optimizer ignore idx_status? distinct statuses rows per status random lookups via index sequential scan instead Each status matches a third of the table, so a scan is cheaper. 160M random row lookups lose to one sequential read. So it taxes every write and helps no read. Low-cardinality columns go last in a composite index, or nowhere. random lookups vs sequential scan How do you decide whether an index is worth it? Weigh measured read benefit against write cost and memory. Reads: which queries use it (usage stats, EXPLAIN), how often, how slow without. Writes: insert rate on its columns, and is it unique? Prefer one composite that serves many queries over many single-column ones. measures before deciding composite over many singles The admin team still wants rich filters on 500M events. What then? Ingest DB few indexes, fast writes CDC stream change feed, e.g. Kafka Column store ClickHouse / BigQuery Admin filters rich queries Keep analytics off the ingest database. Stream changes (CDC, a change feed) to a replica or column store. separate OLTP from OLAP

Ingest is 40 minutes behind right now

What is your first move? Drop the 3 unused indexes now Stats prove nothing reads them. Online drop removes 3 tree writes per insert at once. Ingest speed Read risk Revert the whole migration Fastest way back to 12k/s. But the useful indexes go too. Bigger instance with more RAM Fixes cache misses, not the 7 tree writes. Costs money, hides the problem. Switch the PK to BIGINT today Right long-term, wrong mid-incident. Rewriting 500M rows takes hours of heavy I/O.

Did you show these?

Counted tree writes per insert Used usage stats, not guesses Found the redundant prefix index Unique indexes skip the change buffer Index size vs memory misses Why UUIDv4 keys hurt Reversible rollout (invisible index) Analytics off the ingest path

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.