Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Blog/Databases

How does a database keep your data when the power goes off?

You press save and the power cuts out. Your data is still there. The trick is a plain diary called the write-ahead log.

Derpx0630 Sept · 5 min read
Contents
What goes wrong if we just write to the file?What does "write-ahead" mean?What does "flush" even mean?Why flush the log and not the tables?How does recovery work?Doesn't the log grow forever?How does the same log keep copies in sync?What would I do differently?Your turnSources
0% read · 5 min leftBack to top ↑
On this page0%
Contents
What goes wrong if we just write to the file?What does "write-ahead" mean?What does "flush" even mean?Why flush the log and not the tables?How does recovery work?Doesn't the log grow forever?How does the same log keep copies in sync?What would I do differently?Your turnSources

You press save, the app shows "done", and the power cuts out. You turn everything back on and your data is still there.

How? A database never trusts its own tables first. It writes a plain diary of every change, makes sure the diary is safe, and only then touches the real data. That diary is the write-ahead log, or WAL. It also turns out to be how database copies stay in sync, which I didn't see coming.

What goes wrong if we just write to the file?

Say you build a tiny database. It's one file. To change a row, you find it and write over it. Easy.

Now the power dies in the middle of that write. The file holds half the old value and half the new one. Nobody can tell which half is which. A crash can leave a half-finished change, and there is no way to know it.

It gets worse. PostgreSQL's docs note its 8192-byte pages are written as 512-byte sectors. So a power cut can leave a page only partly written.

So what do we do? Write the plan down first.

What does "write-ahead" mean?

The PostgreSQL docs say changes to the data files "must be written only after those changes have been logged". That means the log records are already saved on permanent storage. Log first. That's the "write-ahead" part.

Click Next and follow one change from your app to the tables.

Your app
Log (in memory)
Log (on disk)
Tables

Your app changes a row

Step 1 of 5
The log is flushed first; the tables can wait.

Notice step 4: "done" is only reported after the log is on disk. The log is added to at the end, one record after another, which is cheap. If the crash comes before step 5, there is no problem. The plan is already safe.

"Flushed to disk" might sound like the same thing as writing. It is not.

What does "flush" even mean?

When you "write" a file, it doesn't go straight to the disk. It passes through the OS cache, the disk controller's cache and the drive's own cache. A power cut can wipe any of those.

So the database asks the OS to force the data out. That call is fsync. The PostgreSQL docs add a worrying detail. The OS can't confirm the data reached truly permanent storage. Some drives (they mention many SSDs) can ignore flush commands. So safety depends on honest hardware too.

HopefulWrite the log, no fsync. It looks saved, but it sits in a cache and a power cut wipes it.
→
SafeWrite the log, then fsync. Only now does the database report committed.

So fsync is the real "save". But flushing is slow. Why not flush the tables instead?

Why flush the log and not the tables?

The docs say that to commit a transaction, only the log file has to be flushed. The data files it changed do not. The log is written in order, so saving it costs much less than flushing table pages scattered around the disk.

Bonus: group commit. When many small transactions arrive at once, one fsync of the log can commit many of them together. It is like sending one delivery van for a whole street instead of one van per house.

Fine. Now the power is back. What happens?

How does recovery work?

The database starts up and reads the log. Any change that is in the log but missing from the tables gets done again. PostgreSQL calls this roll-forward (REDO).

Half-written pages are a separate problem. PostgreSQL fixes it by writing a full copy of a page into the log before it changes that page on disk. Recovery uses that copy to repair the page.

This matters because the tables can be a mess after a crash, and the log still tells the truth. But the diary only gets longer. Doesn't recovery get slower?

Doesn't the log grow forever?

It does. Replaying a year of log after every crash would take far too long. So the database sometimes takes a checkpoint.

Checkpoint

A moment where the database flushes all changed table pages to disk and writes a marker in the log. Everything before the marker is already in the tables, so recovery can start from there.

In PostgreSQL, a checkpoint starts every 5 minutes by default. It starts sooner if the log is about to pass 1 GB (max_wal_size). Checkpoints cost real disk work, so it's a trade-off. Frequent ones mean faster recovery but more disk activity. Rare ones mean less disk activity but a longer replay.

SQLite does the same in its WAL mode. It adds changes to a separate -wal file, and a checkpoint copies them back into the main file. By default that happens when the WAL reaches 1000 pages.

So far the diary only protects one database. Here's the twist.

How does the same log keep copies in sync?

A second database that wants to copy the first just needs the same diary. A PostgreSQL standby server runs in "continuous recovery". It keeps replaying WAL from the primary, like a crash recovery that never ends.

The standby can read the log from an archive, or get it streamed over the network as it's made. By default this is asynchronous, so there is a small delay. The docs say it is typically under a second, if the standby keeps up. Replaying a backup plus the log up to a chosen time also rebuilds an earlier moment.

What would I do differently?

"Saved" really means "the log is on disk and the hardware didn't lie". For a small app I'd use a database with a WAL instead of my own file, and leave the fsync settings alone.

Your turn

  1. Your app writes a 1 KB record per click. Would you fsync every click, or batch many? What do you lose on a crash either way?
  2. If checkpoints happen very often, what gets faster and what gets slower?
  3. A standby is replaying the log and falls behind. What can you measure to see how far behind it is?
Key takeaways
  • Log the change and flush the log first, then update the tables. That's why a crash can't leave you guessing.
  • fsync forces data out of caches, and the log is cheap to flush because it's written in order.
  • After a crash the database replays the log, and checkpoints keep that replay short.
  • The same log feeds replicas and point-in-time recovery, so one idea does three jobs.

Sources

  • PostgreSQL docs, Write-Ahead Logging (WAL)
  • PostgreSQL docs, Reliability
  • PostgreSQL docs, WAL Configuration (checkpoints)
  • PostgreSQL docs, Log-Shipping Standby Servers
  • SQLite docs, Write-Ahead Logging
ShareXLinkedInEmail
Written by Derpx06A developer who builds AI search tools and the code around them. I explain what I got wrong first, so you can skip it. Spotted a mistake? Tell me — corrections get credited.
Databases · 5 minWhat is a Bloom filter? A tiny "definitely not" checkerDatabases · 4 minWhat really happens when you delete a file or a database row?Databases · 4 minB-Trees vs LSM-Trees: how databases store your data
Start here
01Why is 0.1 + 0.2 not 0.3 in code?02How does Git work inside? (Snapshots, not changes)03How does your computer know the time? (And why it can be wrong)