Interview questions
What durable means, and the log
1What does it mean for a write to be durable?
It is stored on something that survives the failures you care about. "Durable" is not one thing but a position on a ladder: from the operating system's page cache (survives a process crash), to the device, to other machines, up to verified copies in several independent failure domains. A claim of durability is only meaningful once you say which rung.
Read moreThe durability ladder
3Why does a database write a log before it writes the data pages?
Because otherwise every commit would force several random page writes, one for each page it changed. The write-ahead log turns them into one sequential append, and the data pages are written later, in bulk, when convenient.
Read moreWrite-ahead logging
4Why does a database need both redo and undo logging?
Both follow from one buffer policy. Redo is needed because a commit does not force its changed pages to disk ("no-force"), so committed changes may exist only in the log. Undo is needed because pages changed by unfinished transactions may be written to disk early to free memory ("steal"), so those changes must be reversible.
5What is a log sequence number (LSN) used for during recovery?
Every page records the LSN of the last log record applied to it. During replay, a record is applied only if the page's LSN is lower than the record's. That makes redo safe to repeat: replaying twice, or replaying onto a page that was already written, changes nothing.
Read moreLogging granularity
6Does a journaling file system protect the contents of my files?
No. By default it protects the file system's own structure (directories, allocation, inodes). After a crash the file system is consistent, but your file can still be incomplete or contain old data.
Read moreJournaling
7How do you safely replace a file's contents on Linux (for example on ext4)?
Write the new contents to a temporary file, fsync it, rename it over the old file, then fsync the directory. Skipping the first fsync can leave an empty file after a crash; skipping the last one can lose the rename itself.
Read moreThe most common durability bug
Fsync and group commit
8A database has one log, and flushing it takes 1 ms. What is the maximum commit rate, and how do you raise it?
If every commit waits for its own flush, at most 1,000 commits per second (Little's Law: one in flight, 1 ms each). Raise it with group commit: commits that arrive while one flush is running share the next flush, so the cost of a flush is spread over many commits.
Read moreGroup commit
9Is opening a file with O_DIRECT enough to make writes durable?
No. O_DIRECT bypasses the operating system's page cache but not the drive's own volatile cache, so data can still be lost on power failure. You still need a flush (fsync) or O_DSYNC.
Read moreFsync, flush barriers and O_DSYNC
10Should a database retry a failed fsync?
No. After a failed fsync, the kernel may mark the dirty pages clean anyway, so a retry can return success while the data is lost. The safe response is to stop and recover from the log.
Read moreFsync, flush barriers and O_DSYNC
11Why does fsync take 20 µs on one SSD and 1–2 ms on another?
Drives with power-loss protection have capacitors that can save their cache if power fails, so they can acknowledge a flush as soon as data reaches the cache. Drives without it must actually write to flash before acknowledging, or lie about having done so.
Read moreBattery-backed memory and NVRAMThe device may lie as well
12Why does group commit have an optimal waiting time?
Waiting W lets more commits share one fsync, but adds waiting time. With commits arriving at rate λ, the cost per commit is fsync / (λ·W) + W / 2, minimized at W* = √(2 · fsync / λ). Wait longer and you add latency; wait less and you waste fsyncs.
Read moreWhen the wait is a timer
Checkpoints, backups and retention
13Why do databases rarely use a sharp checkpoint, one that writes all dirty pages at once?
Because it causes a periodic stall: writing 20 GB of dirty pages at 1 GB/s blocks the database for 20 seconds. A steady, predictable cost is worth more than a smaller cost that arrives in spikes.
Read moreCheckpointing
14What makes fuzzy checkpoints possible?
Replay that checks each page's LSN. A page already written with newer changes ignores old log records, so recovery can start from an on-disk image that was never consistent at any single instant, written gradually while the database kept running.
Read moreCheckpointing
15How does the checkpoint interval affect the volume of log written?
After every checkpoint, the first change to each page writes a full copy of the page into the log, to protect against torn pages. More frequent checkpoints mean more of these full-page images, so much more log, up to about 80× for scattered updates. Recovery gets faster; writing gets more expensive.
Read moreThe coupling nobody expects
16What is a storage snapshot, really?
A garbage-collection rule on a layer of indirection: "these blocks may not be reclaimed." It is cheap to create, but while it is held, every write to the live data must allocate new space instead of overwriting, so it costs the live path for as long as it exists. It is not a backup.
17Backup frequency or continuous log archiving: which one sets how much data you can lose?
Log archiving. How far archiving lags behind sets the recovery point. The age of the last full backup sets how long a restore takes, since everything after it must be replayed.
Read moreBackup and point-in-time recovery
18Why did a forgotten replication slot fill the primary database's disk?
The log can be truncated only up to the point every consumer has passed. A stalled consumer freezes that minimum, so all log written since it stopped must be kept.
Read moreLog truncation and retention
19Why is replication not a backup?
Replication faithfully copies a DROP TABLE to every replica within milliseconds. A backup protects you by being separated in time and in authority from the system it copies.
Read moreWhat really makes a backup
20What metric should replace 'backup success rate'?
Time since the last successful restore, along with how long that restore took. A backup that has never been restored is a hypothesis.
Read moreThe unverified assumption
Corruption and replication
21What is a torn page, and how do database engines prevent it?
A page that is part old and part new after a crash, because the device writes atomically in units smaller than the database's page (for example 4 KB sectors under a 16 KB page). Engines prevent it with a double-write buffer (MySQL), full-page images in the log (PostgreSQL), or a device that guarantees atomic page-sized writes.
Read moreDouble-write buffers and torn pagesThree ways to close the hole
22Why is a checksum stored inside a block not enough to detect corruption?
A write that lands in the wrong place brings its own valid checksum with it, so the misplaced block looks fine. The checksum must also cover the block's identity (which block it is supposed to be), and be stored outside the block, for example in the parent pointer.
23How often must you scrub (re-read and verify) stored data?
Often enough that you find corruption while a good copy still exists: scrub period + alerting delay < retention of the last good copy. With 14 days of backups and a 1-day alert lag, scrub at least every 13 days.
24In a quorum system, why does W + R > N not tell you how many failures your data survives?
That rule is about reads overlapping writes, so a read sees the latest acknowledged write. Durability depends on W, the number of copies that existed when the write was acknowledged, and on whether those copies sit in independent failure domains.
25What is the idea behind copysets in replica placement?
Limit the number of distinct sets of nodes that hold replicas together, so that a simultaneous failure of, say, any 3 nodes is less likely to hold every replica of some partition. It trades how often data is lost for how much is lost when it happens.
Read moreCopysets
26How does group commit choose its batch size?
It does not need a setting. The batch is every commit that arrived during the previous flush: small at low load, large at high load, with no window to tune.
Read moreGroup commit
Durability across a pipeline
27With acks=all, can a Kafka producer still lose an acknowledged record?
Yes, in two ways. "All" means every in-sync replica, and that set can shrink to the leader alone: with the default min.insync.replicas=1, the acknowledgement then means one machine has the record in memory. And no broker flushes per message by default, so a power cut that takes every replica at once loses records they all acknowledged. Set min.insync.replicas=2 so the shrunken case becomes an error the producer sees, keep unclean leader election off, and put the replicas in independent failure domains.
Read moreEach hop makes its own promise
28A service commits a transfer to its database, then publishes an event. Why can no durability setting on either side make this safe?
Because each side keeps its own promise, and the loss is in the gap between them: a crash after the commit and before the publish leaves the transfer durable and the event gone for good. The fix removes the second write. Put the event in the same commit (a transactional outbox), or read it from the database's log (log-based CDC); either way the event becomes exactly as durable as the transfer.
Read moreCustody: who may forget, and when
29What are the two durability numbers of a stream?
What it survives, which is the weakest promise along its hops, and for how long, which is its retention. A consumer that is away longer than the retention silently loses the records deleted in the meantime. Size retention to the longest consumer outage you will tolerate, and measure consumer lag in time against it, not in records.
Read moreRetention is a durability horizon