Skip to content

Lesson 1 of 9 · 24m

InnoDB Internals: Pages, Buffer Pool, Redo, and Undo

The mental model for InnoDB durability: what is in memory, what is on disk, and which knobs trade safety for speed.

The worst InnoDB outage I ever inherited was not caused by a bad query. A well-meaning admin had copied the data directory with cp -a while MySQL was running, called it a backup, and only discovered the truth when a restore produced half-written pages. InnoDB is not a pile of files you can copy casually; it is a transactional storage engine with a very specific crash-recovery protocol.

Everything is a page

InnoDB stores tables and indexes in fixed-size pages, 16 KiB by default. Rows live in clustered index leaf pages ordered by primary key. Secondary index leaf pages contain the indexed columns plus the primary key value, so a secondary lookup often becomes two B-tree walks: first the secondary index, then the clustered index.

SELECT NAME, SPACE_TYPE, FILE_SIZE, ALLOCATED_SIZE
FROM information_schema.INNODB_TABLESPACES
WHERE NAME LIKE 'app/%'
ORDER BY ALLOCATED_SIZE DESC;

Large TEXT and BLOB values may be stored off-page depending on row format and size. This is why a table with a few JSON blobs can have surprising storage and cache behavior even when the row count looks small.

The buffer pool is the real database

The buffer pool is InnoDB's memory cache for pages. A hot database should serve most reads from it. Writes normally modify pages in memory first; dirty pages are flushed to data files later by background threads. That means a committed write does not have to touch the tablespace immediately — but it absolutely must be durable somewhere.

[mysqld]
innodb_buffer_pool_size = 24G
innodb_buffer_pool_instances = 8

Size the pool for the working set, not for the whole database. If the working set fits in memory, reads are mostly CPU and memory. If it does not, storage latency suddenly becomes part of every query plan.

Redo logs make crashes survivable

The redo log records physical changes to pages. InnoDB writes redo records before acknowledging a commit when durability settings are strict, then uses those records during crash recovery to re-apply changes that never reached the data files.

[mysqld]
innodb_flush_log_at_trx_commit = 1
sync_binlog = 1

innodb_flush_log_at_trx_commit=1 means fsync the redo log at commit. Setting it to 0 or 2 can buy write throughput, but you are explicitly accepting transaction loss after an OS or machine crash. MySQL 8.0.30+ manages redo with innodb_redo_log_capacity; older MySQL and MariaDB use innodb_log_file_size and file counts.

Undo logs power MVCC and rollback

Undo records preserve the old version of a row. They let readers see a consistent snapshot without blocking writers, and they provide the data needed to roll back an uncommitted transaction. Long-running transactions pin undo history and prevent purge, which is how a forgotten reporting session can make the undo tablespace grow for hours.

The doublewrite buffer guards torn pages

A 16 KiB InnoDB page is not written atomically by most storage systems. InnoDB first writes pages to the doublewrite buffer, then to their real locations. During recovery, a torn data page can be restored from the doublewrite copy. Disabling doublewrite because a benchmark told you to is usually the wrong trade on general-purpose storage.

Field notes

  • ibdata1, ib_logfile*, redo log directories, and .ibd files are not independent artifacts. Do not copy some of them and expect consistency.
  • Watch Innodb_buffer_pool_reads versus Innodb_buffer_pool_read_requests; logical reads are normal, physical reads tell you when cache misses reach storage.
  • A big buffer pool does not fix a missing index; it only hides it until the data grows.
  • Never edit InnoDB files by hand. Recovery belongs to the server, backup tools, and occasionally innodb_force_recovery as a controlled last resort.

Was this lesson helpful?

Sign in to rate