Why three logs, and who waits when two transactions want the same row?
Four ideas explain most of what InnoDB does in production. The buffer pool keeps hot pages in memory, three logs make commits durable, replicable and reversible, locks decide who waits, and the optimizer decides how a query reads.
Watch it first, then read the details:
Your table lives on disk. Memory is about a thousand times faster than an SSD, and your app keeps reading the same rows: active users, today's orders, the top of every index. Going to disk for the same bytes every time is waste.
A big area of RAM where InnoDB keeps 16 KB pages from your tables and indexes. Every read and every write goes through it. A page read once stays for the next query.
Writes change the page in memory and add a small record to the redo log. That record is enough to rebuild the change after a crash, so the full page can go to disk later, in the background.
For your app: if the hot data fits, most queries never touch the disk. If it stops fitting, the same queries get slower with no code change.
innodb_buffer_pool_size defaults to 128 MB, which is tiny. On a server that runs only MySQL it is usually set to 50 to 75 percent of RAM, leaving room for connections and the OS. innodb_dedicated_server = ON sizes it for you.Innodb_buffer_pool_reads counts pages that had to come from disk. Innodb_buffer_pool_read_requests counts all page reads. If the first one climbs fast during normal traffic, the hot data no longer fits.Innodb_buffer_pool_wait_free above zero means queries waited for a clean page to free up: writes are coming in faster than the page cleaners can flush.innodb_old_blocks_pct and innodb_old_blocks_time.SHOW GLOBAL STATUS LIKE 'Innodb_buffer_pool_read%'; SHOW GLOBAL STATUS LIKE 'Innodb_buffer_pool_wait_free'; SELECT POOL_SIZE, FREE_BUFFERS, DATABASE_PAGES, MODIFIED_DATABASE_PAGES FROM information_schema.INNODB_BUFFER_POOL_STATS;
The same idea elsewhere: PostgreSQL calls it shared_buffers and also leans on the OS page cache, so it is usually set lower, around 25 percent of RAM. MongoDB calls it the WiredTiger cache, by default 50 percent of (RAM minus 1 GB) or 256 MB, whichever is larger.
InnoDB needs three different things from a log. Survive a crash. Let readers see old versions and roll back a transaction. Let replicas copy every change. One log cannot do all three well.
Redo log: physical changes to pages, for crash recovery. A fixed-size circular log (#ib_redoN, sized by innodb_redo_log_capacity).
Undo log: the old version of each changed row, for rollback and for readers who started earlier. Purge threads remove it when no one needs it.
Binary log: logical events, for replicas and point-in-time recovery. Kept as files until they expire.
UPDATE copies the old row into the undo log, changes the row in the buffer pool page, and writes a redo record.COMMIT prepares in the redo log, writes and flushes the binlog, then marks the commit. With innodb_flush_log_at_trx_commit = 1 and sync_binlog = 1 that is two flushes.For your app: the two flushes are the price of never losing a commit. Group commit lets many concurrent transactions share them, which is why throughput holds up under load.
binlog_expire_logs_seconds decides how long binlogs stay. A replica that is far behind needs them.SELECT count FROM information_schema.INNODB_METRICS WHERE name = 'trx_rseg_history_len'; SELECT trx_id, trx_started, trx_mysql_thread_id FROM information_schema.INNODB_TRX ORDER BY trx_started LIMIT 5; SHOW BINARY LOGS;
The same idea elsewhere: PostgreSQL has one WAL for both crash recovery and replication, and keeps old row versions in the table instead of an undo log. MongoDB has the journal for crashes and the oplog for replicas.
Watch it first, then read the details:
Two customers buy the last ticket at the same moment. Two transactions change the same row. One has to wait, or the count goes wrong.
InnoDB locks index records. UPDATE, DELETE and SELECT ... FOR UPDATE take an exclusive lock on every index entry they scan, and hold it until COMMIT or ROLLBACK. Under the default REPEATABLE READ, locking a range also locks the gaps between entries, so no new matching row can be inserted until you finish.
UPDATE tickets SET sold = 1 WHERE id = 42 locks primary key entry 42.innodb_lock_wait_timeout (50 s), it gets error 1205.For your app: keep transactions short, and never call a slow service while one is open.
ALTER TABLE waits behind any open transaction that touched the table, and every new query on that table queues behind the ALTER.SELECT waiting_pid, waiting_query, blocking_pid, blocking_query, wait_age FROM sys.innodb_lock_waits; SELECT trx_mysql_thread_id, trx_started, trx_rows_locked FROM information_schema.INNODB_TRX ORDER BY trx_started; SHOW ENGINE INNODB STATUS; -- LATEST DETECTED DEADLOCK
The same idea elsewhere: PostgreSQL marks a locked row in the row itself and has no gap locks. In a MongoDB transaction, two writers on the same document do not queue: one fails with a write conflict and retries.
Problem. I add an index. The query ignores it. I run EXPLAIN first. It says full scan. That feels wrong. It is often right.
Mechanism. An index lookup reads pages one at a time, in key order. Those pages sit scattered across the .ibd file. Each one is a random read. A full scan reads the same file front to back, in one pass. One sequential pass can beat thousands of random reads. The planner compares those two costs. It picks the cheaper one. It skips the index when the query must return a large share of the table.
The planner estimates row counts from statistics. A bulk load can leave those statistics stale. The planner then guesses wrong. ANALYZE TABLE refreshes the statistics. Run it after a bulk load. Then read EXPLAIN again.
A secondary index does not hold the row. It holds the primary key value. Each match needs a second read into the clustered index. That read fetches the row from users.ibd. This is the bookmark lookup. One match costs two reads. A thousand matches cost two thousand reads.
Secondary index lookup: two reads per match
Every secondary entry carries a copy of the primary key. A long string key makes every secondary entry longer. Longer entries mean more pages per index. Keep the primary key short. Use an integer id when you can.
Exact check. Run EXPLAIN. Then refresh statistics. Then check what the index holds.
Failover with GTIDs: subtraction, not forensics
Read EXPLAIN for the access type. A scan on a wide query is the planner doing its job. A scan on a narrow query means stale statistics or a missing index.
How do databases actually work? From one row on disk to transactions: the words you need, PostgreSQL, MySQL and MongoDB from the files up, then Redis, Cassandra and vector search, side by side, and the query and concurrency traps.