server room power failure
Technology
·
Data And Storage
·
the database that survives a power cut…
Medium
6 min
The Database That Never Forgets a Power Cut
Why your bank balance survives a blackout at the worst possible moment
Share
You tap 'Pay' on a banking app. A split-second later, the server loses power. The transfer was halfway through — your account debited, the recipient's not yet credited. When the lights come back on, where did that money go?
Every app you use — banking, messaging, e-commerce — stores its state in a database. A database is just structured storage: rows and columns of facts the system needs to remember. The tricky part isn't storing data when everything goes right. It's guaranteeing that a crash, a power cut, or a kernel panic at the worst possible moment doesn't leave your data in a broken half-state. That guarantee has a name: durability. And the mechanism that makes it possible is called write-ahead logging, or WAL.
To understand why WAL exists, you need to know what a database is actually doing when it writes. It doesn't just flip bits in one place. A single bank transfer touches multiple records — your balance, the recipient's balance, a transaction history entry, maybe an audit trail. The database has to update all of them, or none of them. Updating some but not others is worse than updating nothing. That all-or-nothing property is called atomicity, and it's the core problem WAL solves.
Make your call
The server crashes right in the middle of writing those records. When it restarts, how does it figure out what state the data should be in?
A
It scans every record to find inconsistencies and repairs them
B
It checks a separate log of what it was about to do, then finishes or undoes it
C
It rolls back to the last backup automatically
Write-ahead logging works like this: before the database changes a single byte of real data, it writes a description of the intended change to a separate log file. That log entry says, in effect, 'I am about to do X.' Only after that entry is safely on disk does the database go ahead and make the actual change. The log comes first. Always. That's the 'write-ahead' part — the log is written ahead of the data. Now when the power cuts out mid-write and the server restarts, the database reads the log. If it finds a log entry with no corresponding completed change, it knows exactly what was interrupted. It can either finish the write (roll forward) or undo it entirely (roll back). Either way, the data ends up consistent. The money is either transferred or it isn't — never half-transferred.
Key Insight
The log is the source of truth. The actual database files are just a cache of what the log says happened. If they ever disagree, the log wins.
This is why WAL shows up in almost every serious database — PostgreSQL, SQLite, MySQL's InnoDB engine. It's also why your phone's filesystem doesn't corrupt itself every time the battery dies mid-save. Journaling filesystems, which are everywhere now, use the same idea: log the intent, then act on it. There's a cost. Writing to the log before writing to the data means every operation takes slightly longer — you're writing twice. But the alternative is a database that lies to you. Given the choice between slightly slower and occasionally wrong, every bank, every hospital records system, every airline reservation platform picks slightly slower. So does your phone. So does your laptop. The log is boring infrastructure that you never think about, right up until the moment it saves everything.
Related threads
Technology
·
Why a Damaged QR Code Still Works
·
a QR code survives a coffee stain…
Why a Damaged QR Code Still Works
Technology
·
Every File Has a Secret Fingerprint
·
any file, one fingerprint…
Every File Has a Secret Fingerprint
Technology
·
How a Two-Hour Movie Fits on Your Phone
·
how a movie fits in your pocket (compression
How a Two-Hour Movie Fits on Your Phone
Sources
Images from Wikimedia Commons. Attribution required by license.
server room power failure
Generated by Grok Imagine (xAI), prompt: Generate a single 16:9 horizontal image for an educational lesson. Lesson title: "The Database That Never Forgets a Power Cut" · Why your bank balance survives a blackout at the worst possible moment …
write ahead log diagram database
Generated by Grok Imagine (xAI), prompt: Generate a single 4:3 horizontal image for an educational lesson. Lesson title: "The Database That Never Forgets a Power Cut" · Why your bank balance survives a blackout at the worst possible moment D…
Quiz yourself