In this lesson, you will learn how to prevent data corruption during file updates by implementing a Write-Ahead Log (WAL) to ensure that every change is safely persisted before applying it to your main data file.
The Problem: Partial Writes
When you update a file in Node.js, your process might crash mid-write. If this happens while fs.writeFile is halfway through modifying your data, your file ends up in a corrupted, "half-baked" state. Because file I/O operations aren't inherently atomic, we need a strategy to guarantee that either the entire change happens or none of it does.
The Solution: The WAL Pattern
Instead of overwriting your primary file directly, you treat the disk as a two-step process:
- The Log: Append the intended change to a secondary
log.txtfile first. Once this write is flushed to disk, the "intent" is safely recorded. - The Checkpoint: After the log is secure, perform the update on your main data file.
If the application crashes, you simply check the log file upon restart. If an unapplied log entry exists, you "replay" it to finish the interrupted update before resuming normal operation.
Simple Implementation
Here is a pattern for performing a safe append:
const fs = require('fs/promises');
async function safeUpdate(data, logPath, dataPath) {
// 1. Write the intent to the log
await fs.appendFile(logPath, JSON.stringify(data) + '\n');
// 2. Perform the actual update to the data file
await fs.writeFile(dataPath, JSON.stringify(data));
// 3. Clear the log (or mark as processed)
await fs.truncate(logPath, 0);
}
By ensuring the appendFile operation finishes before we touch the main dataPath, we create a reliable recovery trail. Even if the process dies immediately after the log is written, our recovery logic can read the log entry and finalize the dataPath update on the next boot, ensuring that no data is ever lost in the "in-between" moments of disk I/O.
Key Takeaway: Always record your intent in a durable log file before modifying the primary data state to ensure you can recover from partial write failures.