Skip to content
Crash-safe file writes for Go

Never lose a byte to a crash.

If two processes write the same file, one silently wins and the other silently loses data. This makes that impossible, in three lines of Go.

go get github.com/larsartmann/go-atomic-write@latest
main.go Go
package main

import (
    "os"

    atomicwrite "github.com/larsartmann/go-atomic-write"
)

func main() {
    path := "/etc/app/config.json"

    data, _ := os.ReadFile(path)
    fp := atomicwrite.FingerprintFromBytes(data)

    newData := []byte(`{"updated": true}`)

    err := atomicwrite.WriteVerified(path, newData, fp)
    // err == ErrConcurrentModification if someone
    // else wrote between your read and write
    _ = err
}
2
Dependencies
~23
GB/s hash
0
Allocations
0
Lost updates

What can go wrong, and what can't.

Six ways a plain file write loses data. This library closes every one of them.

No silent overwrites

A fingerprint check catches concurrent modification between your read and your write, so you get an error instead of a lost update.

Zero-cost fingerprinting

~11x faster than SHA-256, zero allocations, ~23 GB/s throughput. Memory-bound, not compute-bound.

Safe across processes

flock on Unix, LockFileEx on Windows. Protects across processes, not just goroutines.

The file is never missing

Single rename(2) on POSIX — no window where the file is missing. MoveFileEx on Windows.

Survives power loss

fsync on the temp file before rename, fsync on the directory after. Data survives power loss.

Two deliberate dependencies

Only xxhash for hashing and flock for locking. Both intentional, neither replaceable.

Four phases. No window for corruption.

Every write passes through this pipeline. If any check fails, the original file is untouched.

1

Fingerprint

Compute an xxhash64 digest when you read the file.

fp := atomicwrite.FingerprintFile(path)
2

Stage + fsync

Write new content to a unique temp file, then fsync for durability.

atomicwrite.WriteVerified(path, data, fp)
3

Lock + Verify

Acquire exclusive lock, re-read the file, verify fingerprint still matches.

// flock.Lock() + Fingerprint.Matches()
4

Atomic Rename

Single rename(2) replaces the target. Directory is fsync'd (POSIX).

// os.Rename(tmp, path) + syncDir()

if fingerprint != disk

Stage 4 never runs: the temp file is discarded, the original file is left byte-for-byte untouched, and the caller gets ErrConcurrentModification — not a lost update. Exactly one writer wins; the loser knows it and can retry with fresh data.

Why go-atomic-write?

os.WriteFile handles none of this. A naive temp+rename handles one.

Feature os.WriteFile DIY go-atomic-write
TOCTOU-safe
Crash-durable (fsync)
Concurrent-write safe
Atomic rename
Fingerprint verification
Cross-platform locking
Dependencies 0 0 2

Where lost updates cost hours.

Config, checkpoints, and lock files: read-modify-write cycles shared between processes.

Config Files

Read-modify-write cycles safe from concurrent edits

State & Checkpoints

Corruption-free persistence for databases and jobs

CI/CD Pipelines

Concurrent writers on shared artifacts and lock files

Config Reloaders

SIGHUP loops that must never publish a half-written file

Three operating systems. One contract.

Every guarantee holds on every platform CI builds — the differences are the mechanism names, not the promises.

Guarantee Linux macOS Windows
File locking flock (advisory, cross-process) flock (advisory, cross-process) LockFileEx
Replace rename(2) — atomic rename(2) — atomic MoveFileEx + 5 retries on AV locks
Crash durability fsync file + directory fsync file + directory FlushFileBuffers (no dir sync exists)
Tested CI: race + lint + govulncheck CI: amd64 + arm64 builds CI: amd64 build per push

Full notes per platform in Platform Support.

The objections, answered.

Including when this library is the wrong tool for the job.

Why not just write a temp file and rename it?

That handles crashes and missing files, but not races. If another process changes the file between your read and your write, one of the two updates is silently lost. go-atomic-write locks the file, re-reads it, and verifies the fingerprint first. On a mismatch you get ErrConcurrentModification instead of a lost update.

When should I not use this?

If one process owns the file, never crashes mid-write, and you do not need durability across reboots, plain os.WriteFile is simpler. This library earns its keep when a file is shared between processes or has to survive a crash.

The full wrong-tool list

What does a write cost?

One xxhash64 pass (~23 GB/s, zero allocations), one advisory lock, and one full re-read of the file while the lock is held. Negligible for config, state, and checkpoint files; measurable for multi-gigabyte files.

Full benchmark numbers

What happens when two writers race?

Exactly one wins. The loser gets ErrConcurrentModification and the original file is left untouched, because the temp file is discarded before any rename. Writers holding fresh data can retry; writers that do not should surface the error.

Handling ErrConcurrentModification

Does it work on Windows?

Yes. LockFileEx replaces flock, a failed os.Rename is retried five times with exponential backoff on ERROR_ACCESS_DENIED and ERROR_SHARING_VIOLATION (antivirus, open handles), and directory fsync is a no-op because Windows has no equivalent.

Do I have to change how I read files?

No. Reads stay as they are. You only change the write: compute a fingerprint when you read, then call WriteVerified. WriteIfChanged skips the write entirely when the bytes already match, which keeps file watchers and mtimes quiet on no-op runs.

Never lose a byte.

One fingerprint. Six writers. Zero corruption.

WriteWriteVerifiedWriteIfChangedWriteFuncWriteFuncVerifiedWriteWithPermWriteFuncWithPermFingerprintFile