Interview: importing 10M rows takes six hours
One fsync per row. The redo log and WAL, flush settings and what a crash loses, group commit, checkpoints, crash recovery and the doublewrite buffer.
More on Ticket interviews
- Database Interviews in the AI Era: The Ticket Format
- Interview: the same job runs twice
- Deadlock Interview Question: Find and Fix It in SQL
- Slow Query Interview: One Keyword Made It 40× Slower
- Interview: rate limit is 2/min, 30 requests get through
- Interview: “just add an index” slowed ingestion
- Interview: “I changed my name, refresh shows the old one”
- Interview: everything got slow and the disk keeps growing
- Lost Update Interview: Wallet Balance Wrong Under Load
- Interview: API p99 jumps 10× every night at 2 am
- Interview: profile lookups got 40× slower, writes are fine
- Interview: inserts stall, and only one page is hot
- Interview: the write succeeded, the next read shows an empty cart
- Interview: inventory stayed locked after the coordinator restarted
- Interview: for four minutes, both regions took orders
- Interview: shoes show a 0% discount, then the report crashes
- Interview: March gained $50 after the deploy, and the months don’t add up
- Interview: the new branch’s dashboard takes 38 seconds
- Interview: the index is right there, make Postgres use it
- Interview: warehouse and checkout overwrite the same order
- Interview: the bestsellers report now takes nine minutes
- Interview: an analyst query locked checkout
- Interview: the member is active and the email went out twice
- Interview: one shard is at 96% CPU and the other is idle
- Interview: the menu is slow, and the proposal is event sourcing
- Interview: four dashboards disagree about whether the database is slow
- Interview: checkout got slow the hour the warehouse job started
- Interview: the query is fast in staging and slow in production
- Interview: login looks up an email and reads the whole customers table
- Interview: the proposal splits one database into three shards
- Interview: the order page joins four tables on every view
- Interview: every page view is a read-write query on the leader
- Interview: Sarah and John both debit version 1
- Interview: the CTO read a blog post and wants to move Postgres to MySQL
- Interview: one table takes 50,000 writes a second, another feeds dashboards. Which engine for each?
- Interview: EU users wait on the ocean for every write, can both regions take writes?
- Interview: users search cars by color, but the table is sharded by car id
- Interview: we added a fourth node and most of the data moved
- Interview: two people book the same room at noon, both get a confirmation
- Interview: everything is on the strongest consistency and far requests crawl
- Interview: the order is saved, the message to search and email never left
- Interview: rename a column on a 600 GB table while 30 servers keep serving traffic
- Interview: orders go to 16 shards in 2 regions, what should the order id be?
- Interview: one celebrity posts, and every timeline in the app slows down
- Interview: 5,000 customers share one database, and one of them slows down everyone
Keep learning
Overview
1 (default) nothing committed Redo is written and fsynced at every commit. up to ~1 s if the OS or power fails Written to the OS at commit, fsynced about once a second. A mysqld crash alone loses nothing. up to ~1 s even if only mysqld crashes Written and fsynced about once a second. Import 10,000,000 rows Rows per transaction Batch size Flush setting fsync latency NVMe + cache 0.1 ms cloud SSD 2 ms network disk 8 ms redo fsyncs import time crash can lose Time in fsync Undo per transaction One fsync per row: the disk, not the CPU, sets the speed. Batching keeps full durability and divides the fsyncs by the batch size. Faster, but you are trading away durability for every transaction on this server, not just the import. Very large transactions hold locks longer, grow undo, and replicate as one big chunk.
Importing 10M rows takes six hours
CPU is idle and the disk is barely busy. The limit is how fast the database can make each commit safe on disk. WAL / redo log group commit flush settings crash recovery 10M-row import went from 40 minutes to 6 hours One commit per row, each waiting ~2 ms for fsync
Nightly product import misses its window
Catalog team Importing 10M product rows took 40 minutes on the old server. On the new cloud database it takes 6 hours. DB CPU is under 10%. Disk throughput is under 20%. DB CPU disk MB/s 18% of max Restated: each row waits on something. Not CPU, not bandwidth. Likely a delay per commit.
How does the import write?
One statement and one transaction per row. Full durability (the safe defaults). Redo capacity is small for heavy writes. INSERT 1 row Append to redo log sequential write wait ~2 ms for disk COMMIT returns Next row What each loop iteration costs. The fsync wait dominates.
Find the real bottleneck
Is it CPU or bandwidth? metrics in the ticket Neither is busy. So each row waits on some delay. How many commits happen? read import.ts 10M. Autocommit makes every INSERT its own transaction. What does each commit cost? read my.cnf Both flush settings are 1. Redo log and binlog are fsynced (forced to disk) before each commit returns. Why did the new server get slower? compare the two disks Old disk had a battery-backed cache: fsync ≈ 0.1 ms. Cloud disk: ≈ 2 ms, so 20× slower commits. commits (one per row) fsync on the cloud disk spent just waiting on disk The math explains almost all of the six hours.
Keep durability, cut the commits
Weaker commits flush_log_at_trx_commit=0. Fast, but a crash loses ~1 s of every app’s data. Fewer commits 2,000 rows per COMMIT. ~5,000 fsyncs instead of 10M. Nothing lost. Fewer commits, not weaker ones: same safety, ~2,000× fewer fsyncs. It forbids the tempting “turn off fsync” shortcut. Batches can be retried safely because the insert is idempotent. The redo log change goes to ops as a separate ticket.
No AI from here
What is the write-ahead log, and why “ahead”? Change in memory page now “dirty” Log record on disk at COMMIT Page on disk later, in background The log record reaches disk before the data page it describes. At commit, only the small redo log append must be on disk. Changed pages stay in memory and are written later. One sequential write beats random writes to many 16 KB pages. sequential log vs random pages dirty pages flushed later What happens during crash recovery? Doublewrite check repair torn pages replay from checkpoint roll back uncommitted Redo first, then undo. Redo replays the log from the last checkpoint (last known-flushed point). Undo rolls back transactions that never committed. Doublewrite keeps a clean copy of each page, fixing half-written pages. redo then undo torn pages / doublewrite If many users commit at once, does each pay its own fsync? Single-threaded import One commit at a time. One fsync each. Many concurrent commits Group commit: they share one fsync. No. Group commit lets commits at the same moment share one fsync. So busy servers beat the “1 ÷ fsync latency” limit. A single-threaded import gets no help: it commits one at a time. So parallel import workers are another valid fix. group commit Why does a small redo log hurt a write-heavy job? Redo log fills Must flush dirty pages Writes stall The redo log is circular, so space must be freed to reuse it. Freeing space needs the dirty pages flushed first (a checkpoint). If writes outrun flushing, InnoDB flushes hard and writes stall. Bigger log: smoother writes, but longer crash recovery. circular log checkpoint age When is innodb_flush_log_at_trx_commit=2 acceptable? mysqld crash OS crash / power loss = 1 (default) Only where losing the last second is fine and data can be rebuilt. Examples: an analytics-only replica, or staging. Never on the primary for orders or payments. It applies to every transaction on the server, not just the import. knows exactly what is lost scopes it to rebuildable data
The import window is tonight
Which change do you ship? Batch: 2,000 rows per transaction About 5,000 commits instead of 10M, fully durable. Usually done in minutes. Blast radius Set flush_log_at_trx_commit=0 tonight Fast, but a crash loses ~1 s of every order and payment. And someone forgets to undo it. One giant transaction for all 10M rows One fsync, but huge undo and long-held locks. A failure means an hours-long rollback and stalled replicas. LOAD DATA INFILE into a staging table Fastest bulk path, parsed inside the server. Needs file access and a swap step after.
Did you show these?
Counted commits, not rows Explained fsync and the flush settings Explained why the cloud disk mattered Batched instead of weakening durability Described WAL, checkpoints, crash recovery Knew group commit and doublewrite
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.