Section 8.7

Transaction

Definition

A transaction is a group of reads and writes on stored data that a system treats as one unit. A transaction ends in one of two ways: it commits, and all of its writes take effect together, or it aborts, and none of them takes effect. The guarantees a system may give for transactions are summarized as ACID (atomicity §8.3, consistency, isolation and durability §8.2). A system that offers transactions must say which of the four it supplies and how strongly; the word transaction alone does not promise all four.

The idea

A bank moves 10 units from account A, which holds 50, to account B, which holds 20. The program makes two writes: it sets A to 40, then it sets B to 30. If the machine loses power between the two writes, the stored data says that A holds 40 and B holds 20. Ten units have disappeared, and nothing in the stored data shows that a transfer was under way. A second danger needs no crash: if two transfers from A run at the same time, each may read the old balance of A, and one debit then overwrites the other.

A transaction answers both dangers with one promise: the group of writes behaves as if it happened in a single instant, or not at all. A checkout counter is a fair picture. A shopper collects items one by one, but nothing is bought until the single act of paying, and a shopper who walks away before paying has bought nothing. The picture covers only this all-or-nothing part, the A of ACID. Two shoppers reaching for the last item at once, and records that must survive a fire, are the separate guarantees I and D.

How it works

A transaction system works in five steps.

  1. Begin. The program declares the start of a transaction.

  2. Tentative work. The reads and writes run, but the system keeps the writes reversible or hidden. It records them in a log, a storage area to which descriptions of changes are appended, as write-ahead logging §8.12 does; or it writes them to free locations instead of over the old data, as shadow paging §8.9 does. It also keeps concurrent transactions apart, for example by locking the data each one touches, so that no other transaction can change that data meanwhile.

  3. Commit request. The program asks to commit. The system checks what must hold, such as rules declared to it or the absence of a conflict with another transaction, and aborts the transaction if a check fails.

  4. Commit point. The system performs one indivisible step, such as making a commit record §8.21 permanent in the log or switching one stored pointer to the new data. Before this step the transaction has not happened. After it, the transaction has happened, and the system reports success and makes the writes visible to other transactions.

  5. Recovery. After a crash, the system completes every transaction whose commit point it finds and removes every trace of the others.

Each letter of ACID is a separate guarantee with its own mechanism.

Theo Härder and Andreas Reuter introduced the name ACID in 1983 [514].

Worked example

Accounts A, B and C hold 50, 20 and 0 units, so the total is 50+20+0=7050 + 20 + 0 = 70. The application’s invariant is that the total stays 70. Transaction T1T_1 moves 10 units from A to B. Transaction T2T_2 moves 5 units from A to C.

Atomicity under a crash. T1T_1 writes A =50−10=40= 50 - 10 = 40, then B =20+10=30= 20 + 10 = 30. If power fails after the first write and the two writes are not grouped, the stored total is 40+20+0=6040 + 20 + 0 = 60: 10 units are lost. With T1T_1 run as an atomic transaction, a crash before the commit point leads recovery to discard T1T_1, giving 50+20+0=7050 + 20 + 0 = 70; a crash after the commit point leads recovery to complete T1T_1, giving 40+30+0=7040 + 30 + 0 = 70. Both outcomes obey the invariant.

Isolation under concurrency §9.1. Now both transactions run at once with no isolation. T1T_1 reads A =50= 50. T2T_2 reads A =50= 50. T1T_1 writes A =40= 40 and B =30= 30 and commits. T2T_2 writes A =50−5=45= 50 - 5 = 45 and C =0+5=5= 0 + 5 = 5 and commits. The final total is 45+30+5=8045 + 30 + 5 = 80: the debit of T1T_1 was overwritten, and 10 units were created. This is called a lost update. Each transaction was atomic and durable, yet the invariant is broken. Run one after the other, in either order, the two transactions give A =50−10−5=35= 50 - 10 - 5 = 35 and a total of 35+30+5=7035 + 30 + 5 = 70. A serializable system must produce that result.

Consistency is the program’s job. A third transaction moves 60 units from A, which holds 50. The arithmetic gives A =50−60=−10= 50 - 60 = -10. The total is still 70, so the sum invariant holds, but the rule “no balance is negative” is broken. The system rejects this transaction only if that rule was declared to it, for example as a check attached to the balance field. Otherwise it commits the negative balance atomically and durably, exactly as it would commit a correct transfer.

Stated precisely

Let a transaction TT be a sequence of reads and writes that ends with commit or abort. The four guarantees are these.

These guarantees rest on a fault model §1.30, a statement of which failures the system is built to survive. The usual one is crash and restart: the machine may stop at any instant and loses the contents of RAM, but data that reached non-volatile storage and was confirmed by a flush survives intact. A flush is a command that returns only after earlier writes are stored permanently. Storage that loses or corrupts confirmed data, such as a failed disk, is outside this model; durability against it needs copies on other devices.

Limits and common mistakes

Durability begins at a precise moment, and that moment can come after the commit call returns. SQLite, a widely used database library that keeps a database in one file, can run in write-ahead logging mode with its synchronous setting at NORMAL. It then reports a commit without first forcing the log to storage, so a power loss can lose the most recent commits, although the database stays intact.

Isolation comes in levels, and the default is often weaker than serializable. The SQL (Structured Query Language) standard names four levels: read uncommitted, read committed, repeatable read and serializable. PostgreSQL, a widely used database system, runs transactions at read committed unless told otherwise, and the lost update of the worked example can occur at that level.

Atomicity in ACID concerns crashes and aborts. An atomic operation §9.8 in concurrent programming is a different idea: a single step that other threads never see half done. A transaction can be atomic in the ACID sense while, under weak isolation, other transactions see its writes one at a time.

A transaction covers only the data inside the system that runs it. Sending a radio message, moving a motor or writing to a second database from inside a transaction is not undone by an abort. Making several systems commit together needs a protocol such as two-phase commit §8.8. An action outside the system is usually delayed until after the commit point and designed so that repeating it does no harm.

Questions answered

Which transaction properties are actually supplied by the implementation?

The properties actually supplied are the ones that the implementation’s mechanism and documentation establish, letter by letter; calling an operation a transaction does not establish any of the four ACID properties (atomicity, consistency, isolation and durability). To find out which are supplied, ask four questions.

  1. Atomicity: which writes are grouped, which single step is the commit point, and what does recovery do with a group interrupted before that step and with one interrupted after it?

  2. Isolation: can two transactions run at the same time, and if so, at what level, and which anomalies, such as lost updates, does that level allow?

  3. Durability: at what moment does a committed transaction survive a power loss, when the commit call returns or only after a later flush, and which failures, such as a worn-out flash block, lie outside the guarantee?

  4. Consistency: which rules does the system check before it commits, and which are left to the code inside the transaction?

For example, a key-value store on a microcontroller §6.1 that commits a batch of writes by writing one record with a checksum §4.32 supplies atomicity for that batch and durability once that record is flushed. It supplies no isolation between concurrent transactions, which is acceptable only if one writer at a time is enforced, and no consistency beyond the checks the calling code makes itself.

Related entries: Atomicity (§8.3), Linearization point (§8.6), Two-phase commit (§8.8), Exactly-once effect (§8.32), Write-ahead logging (§8.12), Shadow paging (§8.9), Durability (§8.2). Further reading: [179, 514].