Definition
A torn write is a write to storage that a crash or loss of power cuts short after it has changed some, but not all, of the bytes it was meant to change. Storage is left holding a mixture: new bytes up to where the write reached, old or erased bytes after it, and on flash memory §6.6 possibly one unit with bits in an uncertain state. Nothing marks where the write stopped, so only evidence built into the data format can reveal a torn write.
The idea
A write is not one instant. A microcontroller §6.1 (a small computer on one chip) programs its flash memory one unit at a time, often 4 bytes, so a 16-byte record takes 4 program operations, each up to 41 microseconds on the nRF52840 §6.4 microcontroller. A disk with 512-byte sectors writes a 4096-byte page as eight sectors. Power can fail between or during these steps.
The danger is that a torn record looks like a record. A printer that loses power half-way leaves a visibly blank half page; storage leaves no blank. The bytes after the tear are old data or the erased value, and each is a legal byte. A reader without a test accepts a value that nobody wrote, so detection needs a test that ties all parts of a record together.
How it works
What a tear leaves depends on the medium.
Flash memory, written into erased space: new units, then units that still read as erased (typically
0xFF). The unit being programmed at the instant of failure may be half-programmed, with partly charged cells that read differently from one read to the next.A medium that overwrites in place: new units, then old ones, a mix of two versions in which each part looks plausible.
An erase cut short: a block whose content is undefined until it is erased again.
Many flash memories guarantee no unit, not even one word, that is written all or nothing; software must then assume that any program operation can tear.
Detection relies on evidence written as part of the record.
Commit marker §8.21: the body is stored first, and a small field saying that the record is complete is written last, in a separate write. It catches every tear of the body if the device stores the marker only after the body and the marker carries its own check.
Check value over the whole record, such as a CRC §4.32 (cyclic redundancy check): a short code computed from all the bytes, stored with them and recomputed by the reader. A tear makes it mismatch except by rare coincidence.
Length and bounds tests: a half-written length field usually declares a size that does not fit the storage area.
Erased-value tests: a field that still holds the erased value marks the record as incomplete, but only if the format forbids that value in that field of a complete record.
Authenticated records: a message authentication code §5.7 or a digital signature §5.14, which needs a secret key to produce, detects tears and deliberate changes alike.
Detection only rejects the damaged record; the value survives only if another complete copy exists. In an append-only log §8.13 the previous record of the key is that copy, because the new record was written beside it. When a write overwrites the only copy in place and tears, even a perfect test can report only the loss. Safe designs write new versions elsewhere and switch over after checking them, as copy-on-write §8.11 and A/B update §8.10 schemes do.
Worked example
A 16-byte record is written as four 4-byte words, every number stored low byte first (little-endian §4.21 order). Word 0 holds key 7 and length 8, word 1 a temperature set-point, word 2 a humidity limit, and word 3 the CRC-32 (the 32-bit CRC used by zlib and Ethernet) of words 0 to 2. The new record, set-point 21 and limit 45, goes into erased flash, every byte 0xFF, and power fails after words 0 and 1 are stored.
A reader without a test sees key 7, set-point 21 and limit 0xFFFFFFFF, which is 4,294,967,295 if the field is unsigned and if it is signed: values nobody wrote. A reader with the test computes the CRC-32 of 07 00 08 00 15 00 00 00 FF FF FF FF, which is 0xF67A70B8, finds 0xFFFFFFFF in word 3, and rejects the record. The previous record of key 7, intact beside it, supplies the value.
Written in place over an old record with set-point 20 and limit 40, the same tear leaves set-point 21, limit 40 and the old CRC-32. Both numbers are plausible, so a range test does not notice. The CRC test does, because 20 and 21 differ in a single bit and a CRC detects every single-bit change. But the old record is gone, so the program has no valid value at all.
Stated precisely
Model the write of a record as units issued in that order. Assume that the device stores them in that order and that a failure affects only the unit being stored. A failure then leaves, for some with , units to new, unit (if ) in any state, and the remaining units unchanged. The write is torn when and the region differs from the intended record. A validity test detects tears if it accepts only regions that hold a complete record.
A commit marker in , stored after to and carrying its own check, rejects every tear while the order assumption holds. A CRC with an -bit check value detects every damage confined to at most consecutive bits; longer damage escapes in about one pattern in when it behaves like random data. The bytes a tear leaves are not random, so that figure is a guide, not a proof, and the erased-value test covers the commonest case, a check field never written, if the format forbids the erased value as a check value. A volatile write cache can reorder writes; a flush must then separate body and marker, as the write ordering §8.20 entry explains.
Limits and common mistakes
Atomicity §8.3 of a small unit does not extend to a larger object. A disk that writes each 512-byte sector whole can still tear a 4096-byte page.
A returned write call is no proof of storage: on a disk or file system, data may sit in a cache until a flush such as fsync returns.
A CRC detects accidents, not an attacker, who can recompute it. A digital signature detects deliberate change, but it makes no write all or nothing and keeps no fallback copy.
Partly programmed or partly erased flash cells can read differently on different reads. A region judged incomplete at one start-up may be judged differently at the next, and a block whose erase was cut short must be erased again before reuse.
Between threads, a 64-bit value written as two 32-bit halves can be read half updated. That tearing concerns atomic operations §9.8 and data races §9.3, not power failure.
Questions answered
What detects a partially programmed record?
A partially programmed record is detected by evidence that the record format requires every complete record to carry, never by the write itself, which leaves no sign that it stopped early. The two main forms are a check value over the whole record, such as a CRC (cyclic redundancy check), which the reader recomputes and compares with the stored value, and a commit marker or metadata entry written in a separate final step after the rest of the record is stored, whose absence or failed check marks the record as incomplete. Supporting tests reject a declared length that does not fit the storage area, and the erased value in any field in which the format forbids that value for a complete record.
The marker detects every tear as long as the device stores writes in the order issued; a CRC with check bits misses about one random-looking damage pattern in . Detection only rejects the damaged record: keeping the value needs an older complete copy, so safe designs write a new record beside the old one, not over it.
Related entries: Crash consistency (§8.4), Append-only log (§8.13), NVS record (§8.15), Write ordering (§8.20). Further reading: [56].