Course contentsShow
← Node.js
Lesson 331 of 1,9804. I/O and File System OperationsFree lesson

Atomic File System Operations

Perform atomic write operations to prevent data corruption during process interruption.

In this lesson, you will learn how to guarantee that your file writes are never partially saved, even if your Node.js process crashes mid-operation.

The Problem: Partial Writes

When you use fs.writeFile to update a configuration file or a data log, the operating system performs a series of low-level I/O operations. If your server experiences a power failure or an unhandled exception exactly while these bytes are being streamed to the disk, you end up with a corrupted file. The file might contain half of the old data and half of the new data, rendering it unusable.

The Solution: The "Write-Rename" Pattern

To achieve atomicity, we perform the write operation in two distinct steps that rely on the POSIX guarantee that renaming a file is an atomic operation within the same file system.

  1. Write to a temporary file: You write your new content to a unique, temporary filename (e.g., data.json.tmp). If the process crashes here, your original data.json remains untouched.
  2. Rename the file: Once the write is confirmed successful, you use fs.rename to move the temporary file to the final destination (data.json).

Because the OS handles the rename as a single, indivisible operation, the destination file is either the old version or the new version—it is never in an inconsistent state.

Implementation Example

const fs = require('fs').promises;
const path = require('path');

async function atomicWrite(filePath, data) {
  const tempPath = `${filePath}.tmp`;

  // 1. Write to the temporary file
  await fs.writeFile(tempPath, data, 'utf8');

  // 2. Atomically swap it with the target file
  await fs.rename(tempPath, filePath);
}

By ensuring that the rename occurs only after the buffer has been fully flushed to the temporary file, you protect your application's state from corruption during process interruptions. This pattern is a standard requirement for robust system-level development in Node.js, ensuring that your I/O operations respect the integrity of your data.

Key Takeaway: Never overwrite a file directly; instead, write to a temporary file and use an atomic rename to replace the original safely.