“Write a configuration file safely” sounds smaller than a distributed-systems design question. Add concurrent readers, a full disk, and a power failure just before the success response, and it becomes a useful test of engineering judgment. The difficult part is deciding exactly what the application has promised when it acknowledges an update.

This guide develops a practice answer for a senior or staff engineer interview: publish a local configuration snapshot without exposing a partially written document. The proposed protocol assumes one cooperating writer, an existing durable parent directory, a Linux local filesystem with tested synchronization behavior, and a storage stack that honors flushes. The examples are design exercises, not a company's confidential questions or official scoring criteria.

1. Separate visibility, durability, and recovery

Begin by asking what must survive. Does “saved” mean another process can read the update now, the process can restart and recover it, or the machine can lose power without losing an acknowledged update? Those are different failure models. Also ask whether the file is authoritative data or a disposable cache. A regenerable index can justify a different recovery policy from a customer's only copy.

Define the reader contract: each read opens one complete generation and never observes a half-written configuration. Define the acknowledgment contract: success means the selected generation has crossed the application's persistence boundary. Define the recovery contract: startup either loads a validated committed generation or reports that recovery is required. Do not silently replace missing authoritative state with defaults.

The 2014 OSDI paper All File Systems Are Not Created Equal found application crash consistency depended on subtle filesystem persistence properties. Treat that research as a reason to state and test assumptions, rather than evidence that one recipe works on every current platform.

2. Explain what atomic rename actually buys

On Linux, replacing an existing destination with rename(2) is atomic with respect to the destination pathname: another process does not encounter an intermediate moment where that name is absent. Open descriptors remain associated with their files. A reader that already opened the previous generation can continue reading it while a later open obtains the replacement.

Use that behavior deliberately. Build the replacement privately, stop modifying it, and then publish its name. Readers should open once and read through that descriptor. Opening the pathname separately for the header and body would allow one logical read to combine different generations. Atomic replacement does not make a series of separate opens into a transaction.

Keep the temporary file in the destination directory. Linux rename can fail with EXDEV across mounted filesystems; creating a temporary file under a global temporary directory can introduce that boundary. A fallback that copies bytes directly over the live destination changes the reader contract. Explain that you would reject the operation or use a separately designed publication mechanism instead.

Portability belongs in the answer. The Go os.Rename documentation warns that even same-directory rename is not atomic on non-Unix platforms. A familiar standard-library function name is not a universal filesystem guarantee.

3. Put the persistence boundary in the right place

The Linux fsync(2) documentation distinguishes synchronizing a file from synchronizing its containing directory entry. Flushing the file alone does not necessarily persist its name. Under the stated Linux assumptions, propose the following ordered replacement protocol and verify it on the deployed filesystem:

  1. Create a unique temporary file in the existing destination directory.
  2. Write the complete serialized snapshot; check every write result and establish the intended permissions. Flush any application-level buffer into the file before synchronizing it.
  3. Synchronize the temporary file, then close it while checking the result.
  4. Rename the temporary file over the destination without further modification.
  5. Synchronize the parent directory and check that result.
  6. Only then acknowledge durable success.

The ordering expresses two dependencies: persist the replacement's contents before publishing them, and persist the namespace change before acknowledging it. If the directory itself was just created, the protocol needs additional work to persist its creation in its parent. Keeping the example's directory pre-existing avoids hiding that dependency.

For a first-time creation, define what readers do before any generation exists. For replacement, establish that the old generation was durably committed before beginning. These preconditions matter when describing which state recovery may use; “old or new” is not a substitute for identifying a known-good old version.

A successful close is not the durability boundary. The Linux close(2) manual explicitly separates closing from ensuring storage persistence. It also warns against retrying a failed close on Linux because the descriptor may already have been released and reused. Preserve the error and handle it without blindly closing the same integer again.

4. Make error handling part of the state machine

Raw write(2) may return fewer bytes than requested, and success does not itself prove that the data reached durable storage. A syscall-level implementation must track progress and handle interruptions and errors according to the interface. If using a higher-level library, inspect its contract instead of copying a raw-syscall loop mechanically.

Before and after publication

Distinguish errors before publication from errors after publication. Before rename, the writer can abandon its private temporary file and leave the live name alone. After rename succeeds but directory synchronization fails, readers may already see the new generation, while crash persistence remains unconfirmed. Return an explicit uncertain result rather than claiming either durable success or that nothing changed.

For the practice API, attach an operation identifier and generation to each update. A caller that loses the response can inspect the current generation and resolve the result while that operation remains identifiable; later updates may replace that evidence. If the API promises durable deduplication across later updates, that history also needs durable storage; a single “current generation” field cannot remember every previous request.

Record the failing stage, error category, generation, and operation identifier. Avoid logging sensitive configuration contents. A directory-sync failure should remain distinguishable from serialization failure so operators can tell whether publication might already have occurred. Monitoring that flattens both into “save failed” removes information needed for safe recovery.

5. Choose a concurrency policy before adding retries

Two writers can each publish a complete file and still lose an update. Imagine both read generation 40, apply different edits, and publish their own generation 41. Reader atomicity does not decide whose edit should win. State whether the product accepts last-writer-wins, serializes updates, or requires an expected-generation check.

For the local exercise, serialize the whole read, validate, write, and publish sequence. Linux flock(2) provides advisory locks associated with an open file description. All participants must cooperate. Use a stable separate lock file: locking the replaceable data file can split coordination across old and new file objects after replacement. Treat that lock-file choice as a protocol design, not something rename supplies automatically.

Likewise, reserve the temporary pathname safely. The Linux open(2) documentation describes O_CREAT with O_EXCL failing when the name already exists. Prefer exclusive creation over checking existence and opening later. Assume a trusted destination directory here; attacker-controlled path traversal requires a separate security design.

If the interviewer adds multiple hosts or network filesystems, revisit the assumptions. A local cooperative locking story cannot simply become a distributed consensus protocol. For richer transactions, compare using an established database with maintaining a custom persistence mechanism, including ownership of recovery, migration, and operational testing.

6. Design recovery before deleting old generations

For one configuration file, keep a format version, generation, and integrity check inside the document. On startup, validate syntax, size bounds, and semantic constraints before loading it. A checksum can detect some accidental damage; it does not prove that a writer was authorized or that a generation was committed. Decide which failures stop startup and which permit an explicitly approved fallback.

Publishing several related files

For a bundle of files, successive renames do not provide one all-files commit. An illustrative extension is to stage an immutable generation directory, synchronize its files and required directory entries, and publish one small manifest naming that generation. Readers resolve the manifest once and use only its listed files. The manifest and generation lifecycle need their own ordered persistence and recovery protocol.

SQLite's atomic-commit description demonstrates why multi-file commit needs deliberate coordination: its rollback-journal design includes a super-journal and ordered synchronization. This is evidence of the problem's depth, not a recommendation to reimplement SQLite from a diagram.

Delay garbage collection until reader lifetimes and recovery needs are understood. An unreferenced staging file is different from the last recoverable generation. Track who created temporary files and which operation owns them; never delete every unfamiliar file on startup. Make cleanup repeatable and bounded, and document whether cleanup failure affects availability or only disk usage.

7. Test crashes against the actual promise

A unit test that writes a file and immediately reads it checks ordinary behavior. It does not establish power-loss durability. Process termination also leaves the operating system running, so it exercises a different failure from losing the machine. Build a test matrix that names the fault model and records the filesystem, kernel, mount options, and storage environment.

The ext4 documentation distinguishes data journaling modes and describes auto_da_alloc mitigation for common replacement patterns. Those details reinforce the need for explicit synchronization and environment-specific tests. Filesystem journaling is not an application-level commit protocol you can assume from the filesystem's name.

Inject failures around creation, partial writing, file synchronization, rename, directory synchronization, and response delivery. After each simulated restart, check the acknowledged-generation invariant and validate the complete recovered document. Include storage exhaustion, denied permissions, corrupt staging files, and concurrent updates. Run destructive storage fault experiments only in disposable environments.

Keep the oracle precise: under the validated storage assumptions, the latest durably committed state must recover, with any retained history meeting the separate retention contract. An unacknowledged update may have become visible or persisted, so its presence alone is not a failure. Also check that readers never assemble a mixed generation and that recovery does not silently promote an arbitrary temporary file.

8. Rehearse a defensible interview answer

Use this illustrative practice rubric: explain the failure model, reader behavior, acknowledgment boundary, concurrent-writer policy, and recovery procedure. Give yourself one follow-up for each: power loss after rename, a directory-sync error, a reader holding the old descriptor, two simultaneous edits, and startup with an incomplete staging file. This rubric is a preparation tool, not a prediction of hiring outcomes.

A concise opening might be: “I will publish immutable snapshots through a same-directory replacement, serialize cooperating writers, and acknowledge only after the required synchronization succeeds. I will validate that protocol on our Linux filesystem and distinguish uncertain outcomes from unchanged state.” Then draw the sequence and explain one failure at every boundary.

The staff-level discussion is about whether this mechanism fits the product. Explain when a disposable cache can accept weaker durability, when local state needs a database, and when multi-host coordination changes the problem. A strong practice answer connects each added mechanism to a stated requirement and explains how its behavior will be verified.

Sources & References

Primary documentation and original research checked November 22, 2026. Linux-specific behavior is identified in the text; the proposed API, recovery policy, and rehearsal rubric are illustrative engineering choices.

  1. Pillai et al., All File Systems Are Not Created Equal (OSDI 2014) — application persistence dependencies.
  2. Linux rename(2) — replacement visibility and filesystem boundaries.
  3. Go os.Rename — platform restrictions.
  4. Linux fsync(2) — file and directory synchronization.
  5. Linux close(2) — errors and descriptor reuse.
  6. Linux write(2) — partial progress and persistence limits.
  7. Linux flock(2) — cooperative locking.
  8. Linux open(2) — exclusive file creation.
  9. SQLite, Atomic Commit — rollback journals and multi-file commit.
  10. Linux kernel, ext4 General Information — journaling modes and allocation behavior.