Why interviewers love this: recovery is where ACID stops being a slogan and becomes a real safety net.
Question: What does recovery mean in SQL transactions?
Answer: Recovery is the database’s way of getting back to a correct state after a crash, error, or aborted transaction. It means the database can undo work that did not commit and redo work that did commit, so data stays consistent. In simple terms: unfinished changes disappear, finished changes survive.
Interview-Ready Answer: I think of recovery as the database’s repair system for transactions. It uses transaction logs, often with write-ahead logging, so it can undo incomplete work and redo committed work after a failure. That is what protects atomicity and durability: either the whole transaction happens, or none of it does.
Recovery is not usually a SQL command. It is the database engine’s internal process for restoring correctness after a crash, power loss, process kill, or transaction abort. The goal is simple: the database should never look half-updated. A transaction is either fully visible after COMMIT, or fully removed after rollback/recovery.
WAL (meaning the log is written before the data pages), the engine flushes the log first. This is the key rule: if the power dies, the database still has enough history to repair itself.Recovery is what makes COMMIT mean more than “the app thinks it worked.” It gives you two guarantees: committed work survives, and incomplete work is not left behind. Without recovery, a crash could leave one table updated and another table stale, which is a classic corruption pattern.
| Action | Meaning | Recovery impact |
|---|---|---|
| COMMIT | Make changes permanent | Redo if needed |
| ROLLBACK | Cancel the whole transaction | Undo changes now |
| SAVEPOINT | Mark a partial undo point | Undo only part of txn |
| CHECKPOINT | Record a safe restart point | Speeds crash recovery |
fsync; on SSDs this is commonly a few milliseconds, but it can spike under load.Memory mental model: write the receipt first, then move the furniture. The receipt is the log; the furniture is the table data.
Real-World Example: Imagine an e-commerce checkout service that creates an order, subtracts inventory, and writes a payment record. A developer splits those writes across separate autocommit statements instead of one transaction. Then the service crashes after the payment row is written but before the order row is inserted. After restart, recovery does its job correctly for the database, but the application logic is already broken: the user may be charged, yet no order exists. Symptoms look like missing order IDs, mismatched totals in reconciliation jobs, and support tickets saying, “I paid, but my cart vanished.” The lesson is that recovery only protects what you put inside a real transaction; it cannot fix work that was never grouped together.
-- Transaction recovery demo using a savepoint.
-- This is standard SQL style and shows how a database can undo
-- only the risky part of a transaction while keeping earlier work.
DROP TABLE IF EXISTS accounts;
CREATE TABLE accounts (
account_id INTEGER PRIMARY KEY,
owner VARCHAR(50) NOT NULL,
balance DECIMAL(12,2) NOT NULL CHECK (balance >= 0)
);
INSERT INTO accounts (account_id, owner, balance) VALUES
(1, 'Alice', 100.00),
(2, 'Bob', 50.00),
(3, 'Carol', 25.00);
-- Start one unit of work so the change is all-or-nothing.
BEGIN TRANSACTION;
-- Step 1: debit Alice.
UPDATE accounts
SET balance = balance - 30.00
WHERE account_id = 1;
-- Save a recovery point after the safe part.
SAVEPOINT after_debit;
-- Step 2: we accidentally credit the wrong person.
UPDATE accounts
SET balance = balance + 30.00
WHERE account_id = 2;
-- Edge case: we discover the mistake before committing.
-- We do not want to lose Alice's debit, only the bad credit.
ROLLBACK TO SAVEPOINT after_debit;
-- Fix the mistake correctly.
UPDATE accounts
SET balance = balance + 30.00
WHERE account_id = 3;
COMMIT;
-- Final state after recovery-style correction.
SELECT account_id, owner, balance
FROM accounts
ORDER BY account_id;
-- Failure-path note:
-- The CHECK constraint prevents negative balances. If you uncomment
-- the line below, many engines will reject it and the transaction
-- would need a rollback instead of a commit.
-- UPDATE accounts SET balance = balance - 1000.00 WHERE account_id = 1;Follow-up & Tricky Questions:
Common Mistakes:
Memory Hook: Receipt before furniture. The database writes the receipt first, then moves the data, and on a crash it uses the receipt to undo or redo safely.
Cheat Sheet:
Practice Tasks: