Update-in-place
Contents
Description
Updating in place refers to the process of updating information in a file by navigating to the place we want to write the new data to (which may be an append operation) and simply overwriting it.
- update-in-place should be considered dangerous by all programmers, as the following example will show.
Example
Suppose we have the following hex data, and we wish to update a certain portion of it, highlighted in green:
Offset | Data |
---|---|
0000000 | 796D B26F B43D 9DAE 8DA2 8224 FD50 6847 1C23 9B18 0465 3309 54CF 686D EB65 9A1E |
0000020 | C4BE 98A6 2B6E 3F7C 0671 3382 A056 011E 83CF F814 8E29 960B 5FC0 9FD3 CAF9 58F4 |
0000040 | F6FF ED81 3552 CF37 7C80 D172 506E E558 2D0B 224B EE19 6837 08F1 758A E6EF 98B8 |
0000060 | 38AA 0CB1 4C1D 2E7C 58E8 44DF 17C1 3A5F 80BE 1F0D 433E 5578 AE82 1214 FA9E 9637 |
0000080 | F83D EEE5 5249 0B11 D5C7 4BE9 9789 3850 176E 5929 6DF7 C0B2 C397 6059 488B 21BA |
00000a0 | 5C26 AE59 2B14 0775 F4A2 C349 34E6 D93D DEA3 028E 38C3 4958 C35B 857F 2DAB 361C |
00000c0 |
If we assume this device writes in 32 byte blocks (a row in this table) then the update to byte 0x3f (the first highlighted byte, 0xB8) might (due to block caches and fragmentation) separately to the three bytes of the write starting in the next 32 byte block, at 0x60 onwards.
If the operating system crashes between these writes the data will read xx38 AA0C where xx is whatever was written before the crash, this is bad because neither the old or the new data exists!
Power cuts rarely considered (often an OS crash is seen as the only event involved in durability, and as such it becomes about atomic block operations) but power can cut out, drives have caches that wont survive this, nor will the block cache.
As such update-in-place should be considered dangerous by all programmers
It is desirable to have a situation where, in the event of a crash:
- if the write (or writes) didn't complete, the old data is considered "active" and used, and
- only if the write (or writes) completed do we see this new data.