Dev Learning Lab

The labs on this site need JavaScript to run. The same content is written out below.

Learn › Database › Storage and recovery

Buffer pool and pages

Why does reading one row read a whole 16KB page?

Why it exists — If every read and change of a row went directly to the disk, a frequently used row would have to be read from the same place again and again, and every change would have to be written straight to that place in the data file. InnoDB's buffer pool takes this repeated disk work into one place in memory, so that the more often data is used, the fewer trips to the disk it needs.

Where it's used today — The most direct use is setting innodb_buffer_pool_size when you configure a MySQL server. On a server that runs only the database, a large share of the memory is given to the buffer pool. If frequently used data cannot all stay in the buffer pool, even the same query reads the disk more and becomes slower, so when you look for the cause of a slow query, also check how many times data was read from disk.

InnoDB reads and writes data on disk in blocks called pages (16KB by default), not row by row. A page that has been read is kept in memory in the buffer pool, and later reads come from memory instead of going to disk. When a row is changed, only the page in memory is changed, and it is written to disk later.

Checked against real execution · All fourteen combinations were run on MySQL 8.4.11 in a dedicated container (128MB buffer pool). One page on the screen was scaled up to a group of real pages and innodb_old_blocks_time was set to 0; the six combinations with no eviction were also run with the SQL on the screen as it is

Concepts in this lab

Buffer pool
The memory area where InnoDB keeps the pages it has read from disk. If a needed page is in the buffer pool, it is read from memory without going to disk. If it is not, it is read from disk and put into the buffer pool. The size is set with innodb_buffer_pool_size, and when the pool is full, pages that have not been used for a long time are evicted to make room. A disk is much slower than memory, so giving the buffer pool enough size to hold the frequently used data has a large effect on performance.
Page (InnoDB)
Why it exists — On a disk, reading one contiguous chunk at once is usually faster than reading many small pieces separately. InnoDB groups rows and indexes into pages to read and write them, which reduces the number of trips to the disk.
The basic unit in which InnoDB stores, reads, and writes data on disk. The default size is 16KB, and one page holds several rows together. Even when only one row is needed, the whole page that holds it is read, so the other rows on the same page are loaded into memory as well. Indexes are stored in pages too, not only the rows of a table. For this reason, reading rows that are stored close together reads less from disk, while reading scattered rows reads a different page for each row.
Dirty pages and flushing
Why it exists — If you write to the data file every time the same page is changed several times in a short span, the number of writes equals the number of changes. If InnoDB keeps a changed page as a dirty page and flushes it later, several changes can be combined into one write.
Where it's used today — On a MySQL server with many writes, you tune the flush speed and the proportion of dirty pages that can accumulate with settings such as innodb_io_capacity and innodb_max_dirty_pages_pct.
A dirty page is a page in the buffer pool whose content has been changed but has not yet been written to the data file on disk. When InnoDB changes a row, it changes only the page in memory, and a background thread collects the changed pages and writes them to the data file later. This writing is called flushing. A commit does not wait for flushing, and if the server goes down in the meantime, the changes are applied again from the redo log to recover. To evict a dirty page from the buffer pool, it must first be written to disk.
Eviction policy
Why it exists — If you have not decided what to remove when memory is full, no new value can be added. An eviction policy decides the order of removal, so that even when the cache is full, it deletes the values used least first and keeps accepting new ones.
Where it's used today — When Redis is used only as a cache, set it to allkeys-lru or allkeys-lfu. When it is used together with values that must not be deleted, choose volatile-lru, which removes only keys that have a TTL. The default, noeviction, deletes nothing when maxmemory is reached and returns an error for write commands.
The rule that decides which data to remove when the memory of a cache is full. Examples are LRU, which removes the least recently used data, and LFU, which removes the least frequently used data. In Redis, you set it with the maxmemory-policy setting.
Undo log and redo log
An undo log records data as it was before a change. It is used to roll back a transaction, and in InnoDB it is also used to show past versions to other transactions (MVCC). A redo log records the changes themselves. If a failure occurs before committed changes are fully written to the data files, the changes are applied again at restart to recover them.

Try these

Running with the default settings

  1. The 16 rows of the item table are stored in the data file on disk in four pages (A, B, C, and D). A page is 16KB and holds 4 rows. The buffer pool (memory) does not hold any page of this table yet.
  2. SELECT … WHERE id = 1: row 1 is in page A. Page A is not in the buffer pool, so the whole page A (16KB) is read from disk and placed in a free slot. Rows 2, 3, 4 on the same page are loaded into memory with it.
  3. UPDATE item SET name = 'changed' WHERE id = 1: Row 1 is modified in page A, which is in the buffer pool. The disk is neither read nor written. A page whose content in memory now differs from the disk is called a dirty page.
  4. COMMIT: The record of what was changed (the redo log) is written to disk, and the commit is complete (with the default settings of MySQL 8, the binary log used for replication is also written at this point). Page A stays in the buffer pool in the dirty state, and it has not been written to the data file. Writing to the data file is postponed until later.
  5. SELECT … WHERE id = 2: row 2 is in page A. Page A is already in the buffer pool, so it is read directly from memory without going to disk. This is the first time row 2 is read, but it was loaded along with the page when another row on the same page was read.
  6. SELECT … WHERE id = 1: row 1 is in page A. Page A is already in the buffer pool, so it is read directly from memory without going to disk. The modified value changed is read. The data file on disk still has the old value item1.
  7. A little later: a background thread writes dirty page A to the data file. The page stays in the buffer pool, and only its dirty mark is cleared. InnoDB decides when to write, and the commit does not wait for it.

Result of this run

Code

-- item table: 16 rows. One row is about 3.5KB, so 4 rows fit in one 16KB page
--   Page A: rows 1-4    Page B: rows 5-8
--   Page C: rows 9-12   Page D: rows 13-16
-- At the start, the buffer pool holds no pages of this table

SELECT name FROM item WHERE id = 1;

START TRANSACTION;
UPDATE item SET name = 'changed' WHERE id = 1;
COMMIT;

SELECT name FROM item WHERE id = 2;
SELECT name FROM item WHERE id = 1;

Assumptions of this model

This is a model drawn at a reduced size, with a buffer pool of only 2 to 4 slots and the simple rule "evict the page that has gone unused the longest." A real buffer pool cannot be made smaller than 5MB (320 pages), so there is no buffer pool with 3 slots. So I ran it in two ways on MySQL 8.4.11 (a dedicated container; the buffer pool has the default size of 128MB, which is 8192 pages of 16KB) and compared the results. First, the six combinations with no eviction were run with the SQL on the screen as it is. I created a 16-row table in which one row is about 3.5KB, confirmed that each leaf page holds 4 rows, restarted the server to empty the buffer pool, and ran the queries in the same order as on the screen. Reads of the data file of that table increased only for a page that was read for the first time (3 reads for the first query, including the upper pages of the index, and 1 read for each new page after that), and there were 0 reads when another row or the same row on a page already loaded was read. Right after UPDATE and COMMIT, there were 0 file reads and 0 file writes and there was 1 dirty page, and the data file was written by itself within 2 to 5 seconds. Second, all fourteen combinations were run with the sizes scaled up by the same ratio. One page on the screen became a group of K real leaf pages (the first part of one table), one slot became room for K pages, and K was set a little smaller than the number of pages in the buffer pool divided by the number of slots (3,660 pages for 2 slots, 2,473 for 3 slots, and 1,857 for 4 slots). One query on the screen became a SELECT that reads that group to the end, and the change to row 1 became an UPDATE of row 1 in the first group followed by COMMIT. For each query I measured the number of pages read from the file and the number of pages of each group left in the buffer pool, and the number of disk reads, the queries that did not go to disk, and the evicted groups and their order matched the screen in all fourteen combinations. In the three combinations in which the changed group is evicted, the changed page never left the buffer pool without being written. Either it left after a background flush had written it, or that one page stayed in the buffer pool even after the other pages of the same group were evicted and it was written after the queries finished (over several runs, which of the two happened differed from combination to combination). A write at the very moment of eviction (what the step on the screen shows) could not be singled out in this run. In one run, a measurement right after UPDATE and COMMIT showed that a background flush had already written the page within those few milliseconds; such a run is not the situation on the screen, so it is run again. Two server settings were changed for this run: innodb_old_blocks_time was set to 0 (the default is 1000) and innodb_lru_scan_depth to 100 (the default is 1024). Real InnoDB divides the list into a new sublist and an old sublist, inserts a newly read page between them (by default at the point 3/8 from the tail), and moves it to the new sublist only if it is read again after innodb_old_blocks_time has passed since it was first read (MySQL 8.4 Reference Manual, 17.5.1 Buffer Pool). With this value set to 0, a page moves to the head of the list every time it is read, which is the same as the simple rule on the screen. When the same sequences were run without a pause with the default value, none of the twelve combinations matched the screen. A group that had just been read stayed only in the old sublist and was evicted as soon as the next group was read, and a group that was read again was also read from disk (for example, with 3 slots and the order 1 → 5 → 9 → 13 → 1, the screen evicts A and B, but with the default value A, B, C, and D were evicted one after another). So the order on the screen is the order of InnoDB with this safeguard turned off. innodb_lru_scan_depth is the number of free pages that the page cleaner keeps available in advance; with the default value, 1/8 of the pool is always free and the boundaries between groups become blurred, so it was set to the smallest value. Also, a real query reads the upper pages of the index in addition to the page that holds the row and sometimes reads the following pages ahead, so a few dozen more pages than K were read when one group was read.

Previous lab: Undo and redo logs · Next lab: Replication lag and CAP

Concepts in this lab

What this lab shows

Settings

Code


      
    

Diagram

Run comparison

Try these