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.
Begin. The program declares the start of a transaction.
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.
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.
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.
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.
Atomicity: after an abort or a crash, either all of the transaction’s writes are present or none are. The log or the separate copies supply it.
Consistency: every committed transaction takes the data from a state that obeys the application’s rules to another state that obeys them. Such a rule is called an invariant, for example “the sum of all balances never changes” or “no balance is negative”. The system enforces only the rules declared to it; the rest depend on the code inside the transaction being correct.
Isolation: transactions that run at the same time do not see or overwrite each other’s unfinished work. The strongest level, serializability, means that the outcome equals the outcome of running the same transactions one at a time in some order. Locks, or several stored versions of each item, supply it.
Durability: once the system has reported a commit, the writes survive later crashes. The system supplies it by making the commit record reach non-volatile storage, which keeps its contents without power, before it reports success.
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 . The application’s invariant is that the total stays 70. Transaction moves 10 units from A to B. Transaction moves 5 units from A to C.
Atomicity under a crash. writes A , then B . If power fails after the first write and the two writes are not grouped, the stored total is : 10 units are lost. With run as an atomic transaction, a crash before the commit point leads recovery to discard , giving ; a crash after the commit point leads recovery to complete , giving . Both outcomes obey the invariant.
Isolation under concurrency §9.1. Now both transactions run at once with no isolation. reads A . reads A . writes A and B and commits. writes A and C and commits. The final total is : the debit of 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 and a total of . 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 . 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 be a sequence of reads and writes that ends with commit or abort. The four guarantees are these.
Atomicity: after aborts, or after a crash followed by recovery, the stored data contains either every write of or none of them; after an abort, none.
Durability: if the system reported at time that committed, every later state, including the state after any later crash and recovery, contains the writes of , except where a later committed transaction overwrote them.
Isolation at the serializable level: for every execution of a set of committed transactions, there is an order of the same transactions, run one at a time, in which every transaction reads the same values and which leaves the same final state.
Consistency: let be an invariant that holds in the initial state. If every transaction, run alone from any state that satisfies , ends in a state that satisfies , then atomicity and serializable isolation together make hold in every committed state.
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.
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?
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?
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?
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].