# ACID in InnoDB: WAL, redo log, undo log and the binlog

> ACID is not magic. In InnoDB, atomicity comes from the undo log, durability from the redo log, and the binlog is a separate log for replication and CDC.

Published 2026-11-25 · Language: en · Tags: mysql, innodb, transactions

Every database textbook lists the four ACID properties. Fewer explain which part of the engine delivers each one. Knowing that mapping makes crashes, slow commits and replication behaviour much easier to reason about. This article uses MySQL with the InnoDB storage engine, the default since MySQL 5.5.

 

## What a transaction promises

 

A transaction groups several statements into one unit of work. The classic example is a transfer: subtract from one wallet, add to another. If the server stops between the two statements, the money must not vanish.

 

```sql
START TRANSACTION;
UPDATE wallets SET balance = balance - 100000 WHERE id = 1;
UPDATE wallets SET balance = balance + 100000 WHERE id = 2;
COMMIT;
```

 

Either both updates are visible after `COMMIT`, or neither is. That is atomicity. The other letters describe what else the database owes you around that unit.

 

## Atomicity: the undo log

 

Before InnoDB changes a row, it writes the old version of that row into the **undo log**. If you issue `ROLLBACK`, or the server crashes before the commit, InnoDB walks the undo records backwards and restores the previous values. Rollback is therefore a real piece of work, and a transaction that changed millions of rows can take a long time to roll back.

 

The undo log has a second job: it keeps old row versions available so that other transactions can read a consistent snapshot without waiting for locks. That is the basis of MVCC, covered in a later article of this series.

 

## Durability: write-ahead logging and the redo log

 

Data files are large and updated at random places, so writing every changed page to disk at commit time would be slow. InnoDB uses **write-ahead logging (WAL)**: it first appends a compact description of the change to the **redo log**, which is written sequentially and is cheap to flush, and only later writes the modified pages to the data files in the background.

 

When you commit, InnoDB makes sure the redo records are on durable storage. If the server loses power, startup recovery replays the redo log and brings the data files up to date with every committed transaction. The setting that controls this is `innodb_flush_log_at_trx_commit`:

 

- **1** (default): flush the redo log to disk at every commit. Full durability.
- **2**: write at commit but flush about once per second. A crash of the operating system or the machine can lose the last second of commits.
- **0**: write and flush about once per second. A crash of the MySQL process alone can lose that second.

 

```mysql
SHOW VARIABLES LIKE 'innodb_flush_log_at_trx_commit';
SHOW VARIABLES LIKE 'sync_binlog';
SELECT @@autocommit;
```

 

For money, keep the default of 1. Lower values are a conscious trade of durability for speed, sometimes acceptable for caches or analytics staging tables, never for a ledger.

 

## The binlog is a different log

 

The **binary log** belongs to the MySQL server layer, not to InnoDB. It records changes as events so that replicas can replay them and so that tools such as Debezium can stream them (change data capture). With `binlog_format=ROW`, the default in MySQL 8, each event carries the before and after image of the changed rows.

 

InnoDB and the binlog must agree about what was committed, otherwise a replica could diverge from its source. MySQL solves this with an internal two-phase commit between the redo log and the binlog. With `sync_binlog=1` the binlog is also flushed at every commit, which completes the durability story for replicated setups.

 

## Consistency is partly your job

 

The C in ACID means the database moves from one valid state to another. The database can only enforce the rules you declare: primary keys, unique keys, foreign keys and, since MySQL 8.0.16, `CHECK` constraints. A rule such as "the sum of all ledger entries is zero" exists only if you design for it with constraints, transactions and tests.

 

**Autocommit.** MySQL starts in `autocommit=1` mode: every statement is its own transaction and commits immediately. Two statements that must succeed together need an explicit `START TRANSACTION`. A frequent bug is a connection pool handing out a connection that still holds an open transaction from earlier code, so always commit or roll back in a `finally` block.

 

## Takeaway

 

Atomicity is the undo log, durability is the redo log with WAL, isolation is MVCC plus locks, and consistency is the constraints you wrote. The binlog is a separate stream for replicas and CDC. When a commit is slow or data is missing after a crash, ask which of these logs is involved and how it is configured.

Source: https://tdphat.io.vn/blog/acid-wal-redo-undo-binlog-in-innodb
