Use Atomic Operations for Updating Data

about | archive


[ 2007-January-14 14:43 ]

Tim Bray recently complained about Apple Mail losing all of its settings after his computer crashed. Application programmers should only use atomic updates when updating data stored on disk. This technique is easy on Unix operating systems, like Mac OS X or Linux. Simply write a new copy of the updated file to a temporary file name in the same directory, then rename it to replace the old version of the file. The worst thing that can happen with this technique is that if the system crashes, a temporary file could be left in the directory and your last update will be lost. This is actually the reason that many applications use a database to store settings, since all proper databases provide atomic updates.