How data consistency is maintained for incomplete files
mainWhen downloading large blobs incrementally (e.g., in 16 KiB chunk groups), maintaining consistency between the .data, .outboard, and .bitfield files is challenging due to the lack of transactional guarantees in file systems.
The Consistency Model
To prevent a crash from leaving the store in an inconsistent state (where the .bitfield claims data exists that hasn't actually been flushed to the .data or .outboard files), iroh-blobs uses a specific write strategy:
- Write Data and Outboard: The
.dataand.outboardfiles are updated as chunks arrive. These writes can happen in any order and without explicitfsynccalls. - Update Bitfield Last: The
.bitfieldfile (which tracks valid chunks) is only updated after the data/outboard writes.
Crucially: If a crash occurs, the system relies on the fact that if the .bitfield was not updated, the changes to the data and outboard files are treated as invisible. This avoids the massive performance penalty of calling fsync after every 16 KiB chunk, which would otherwise limit download speeds to a few megabytes per second.