Definition
Atomicity is the property that an operation of several steps appears to a named observer to happen completely or not at all: that observer never sees some of the steps done and others not done. For concurrency §9.1, the observers are other threads, sequences of instructions that run at the same time and share memory. For failures, the observer is the recovery code that runs after a crash or loss of power, which must find the whole old state or the whole new state.
The idea
A bank moves 100 from account X to account Y. The program performs two writes: it subtracts 100 from X, then adds 100 to Y. If power fails between the two writes, 100 has left X and arrived nowhere. If another thread adds up all balances between the two writes, it reports a total that is 100 too low, although nothing was lost. Neither result could occur if the transfer happened in one indivisible step.
Atomicity makes a multi-step operation behave like a single step for a chosen observer; the word comes from the Greek atomos, uncuttable. The two observers are defeated by different things. A thread runs while the operation is in progress, so the operation must hide its intermediate states from that thread, typically with a lock. Recovery code runs after the failure, when RAM and every lock held in it are gone, so the operation must arrange its writes in storage so that recovery can always tell the old state from the new one. A guarantee against one observer gives nothing against the other.
How it works
Atomicity against threads. Three tools are common.
Hardware atomic operations §9.8. Common processors perform a load or store of one machine word at an aligned address (a multiple of the word’s size), and a few read-modify-write instructions such as compare-and-swap §9.9, as one indivisible step: another thread sees the whole old word or the whole new word, never a mixture.
Locks. A mutex §9.5 (mutual-exclusion lock) lets only one thread at a time run the code between acquiring and releasing it, which is called a critical section §9.4. If every reader and writer of X and Y holds the same mutex while it touches them, no reader sees the transfer half done.
Publishing a prepared copy. The writer builds the complete new state in separate memory, then makes it current with one atomic store of a pointer. Readers follow the pointer and see either the old copy or the new one.
The precise meaning of “appears indivisible to other threads” is linearizability, which the Linearization point §8.6 entry explains.
Atomicity against failures, also called failure atomicity §8.5, follows one rule: never overwrite the only good copy.
Write the complete new state to a separate place in storage and leave the old state untouched.
Check that the new copy is complete and correct, for example by reading it back and comparing a check value, a short number computed from the data.
Write one small record, the commit record §8.21, that tells recovery which copy is current. Recovery reads this record first and uses only the copy it names.
Before the commit record changes, recovery uses the old copy; after it changes, recovery uses the new copy. The whole operation, however many records it changes, now depends on one small write.
That one write must itself be atomic against failures, or every outcome of interrupting it must be one that recovery reads as the old value or the new value. Whether a single write to flash memory §6.6 is atomic under a loss of power depends on the device, so designs commonly keep two copies of the commit record, each with a sequence number and a check value, and recovery takes the valid copy with the higher sequence number. Shadow paging §8.9, copy-on-write §8.11 and A/B update §8.10 are forms of this pattern. Write-ahead logging §8.12 reaches the same goal differently: it first stores a log record of the intended change, so that recovery can complete or cancel a change that a failure interrupted.
Worked example
Accounts X and Y hold 500 and 200, a total of . The transfer of 100 should leave X at and Y at , a total of . If the program overwrites the stored balances in two steps, a failure leaves one of three stored pairs:
failure before step 1: (500, 200), total 700, the old state;
failure between the steps: (400, 200), total 600, a mixture in which 100 has vanished;
failure after step 2: (400, 300), total 700, the new state.
The middle case violates atomicity against failures.
Now store the pair in one of two slots, slot 0 and slot 1, and keep a commit record that names the current slot. At the start, slot 0 holds (500, 200) and the commit record names slot 0. The transfer writes (400, 300) into slot 1, checks it, and then changes the commit record to name slot 1. A failure at any moment before the commit record changes leaves recovery reading slot 0, total 700. A failure after the change leaves recovery reading slot 1, total 700. A half-written slot 1 may sit in storage after a failure, but recovery never reads it.
The thread case is different. Suppose the program also keeps X and Y in RAM, updates them one after the other, and a reader thread reads them without a lock. If the reader reads X = 500 before the transfer and Y = 300 after it, the reader computes , a total that never existed. The two-slot scheme in storage does not prevent this, because the reader reads the variables in RAM. A mutex held by both the transfer and the reader does prevent it.
Stated precisely
Let be the state before an operation and the state the operation is meant to produce. The operation is atomic with respect to an observer if every state the observer can see is or . The two kinds of observer give two precise conditions.
Threads. Each operation appears to take effect at one instant between the moment it is called and the moment it returns, and the results that the threads see agree with the operations taking effect one at a time in the order of those instants. This condition is linearizability.
Failures. Let be the content of persistent storage after a failure at any instant during the operation, including every partial outcome that the failure model §1.30 allows for a write in progress. Let be the state that the recovery procedure builds from . Failure atomicity requires for every such . The two-slot scheme meets this requirement under three assumptions.
The new slot is completely written before the write of the commit record begins, which needs ordered writes.
Every outcome of an interrupted commit record write is read by recovery as either the old or the new slot number.
Recovery reads only the slot that the commit record names.
Limits and common mistakes
A claim of atomicity must name its observer. “This update is atomic” is incomplete until it says against which threads, against which failures and over which records.
Atomic pieces do not combine into an atomic whole. Two atomic stores in a row are two steps, and an observer can see the state between them. Writing each record atomically does not make a set of records atomic.
A lock gives no protection against a loss of power. The lock lives in RAM and vanishes at the failure; the half-written storage remains. Conversely, a crash-safe commit record does not prevent a data race §9.3, in which two threads access the same memory at the same time without coordination and at least one of them writes.
In database terminology, atomicity, the A in ACID (atomicity, consistency, isolation, durability §8.2), means all or nothing in the face of failures and of transactions §8.7 that are canceled before they finish. What other transactions running at the same time can see is a separate property, isolation. A reader who takes atomic to mean isolated will misread database documentation.
Atomic is not durable. Failure atomicity allows the old state after a failure, so a finished operation can still be lost unless the system reported it complete only after its commit record was safely stored, as the Durability entry explains.
Atomic is not correct. An operation that replaces a good configuration with a wrong one does so atomically just as well.
Atomicity across several machines needs a protocol in which the machines agree on the outcome, such as two-phase commit §8.8.
Questions answered
Atomic with respect to threads, crashes, or multiple persistent records?
These are three different guarantees, and a claim of atomicity must say which one it makes. Atomic with respect to threads means that no other thread running at the same time can observe the operation half done; a hardware atomic instruction gives this for one machine word, and a lock, or the publication of a prepared copy by one pointer store, gives it for larger data. Atomic with respect to crashes means that after a crash or a loss of power, the recovery procedure finds either the complete old state or the complete new state.
Atomic across multiple persistent records is the crash guarantee applied to an operation that changes several stored records, for example a program and the configuration that must match it. Writing each record atomically is not enough, because a failure between two records leaves one new and one old. The usual solution writes the new records to a separate place and then switches to them with one small commit record that recovery reads, so that the whole set changes with that one write. The guarantees do not imply each other: a lock in RAM does nothing against a loss of power, and a crash-safe commit record does nothing to stop two live threads from interfering.
Related entries: Crash consistency (§8.4), Failure atomicity (§8.5), Linearization point (§8.6), Commit record and selector (§8.21), Atomic operation (§9.8).