Database Transaction Log: Your System's Safety Net
A transaction log is your database's safety journal. Before changing data, it records the intended action in a durable file. This is vital for crash recovery, ensuring data isn't left corrupt. The footgun: its primary role is integrity, not just auditing.
WHY IT EXISTS: Databases must be reliable, even if the power cuts out mid-operation. Without a record of intent, a crash could leave data in an inconsistent, corrupted state. The transaction log solves this by creating a durable history of all changes before they are made.
THE MENTAL MODEL: Imagine it as a cashier's journal. Before touching the money in the register, the cashier first writes down the transaction: "Sale, +$20.50." If the power goes out, they can look at the journal to reconstruct the drawer's correct balance. The log entry is written before the action is taken. This is called Write-Ahead Logging (WAL).
HOW IT WORKS: When a transaction begins, the database writes a description of the intended changes to the log file first. This log write must be confirmed as saved to stable storage (like a hard drive) before the system modifies the actual data pages. If the system crashes, upon restart it reads the log. It can "redo" committed transactions that weren't fully saved to the main data files and "undo" (or roll back) any transactions that were in progress but never committed. This ensures atomicity and durability.
WHEN TO USE IT: This is a fundamental component of nearly all ACID-compliant relational databases like PostgreSQL, MySQL, and SQL Server. It's essential for three main functions: first, crash recovery to ensure data integrity; second, streaming replication, where replicas replay the log to stay in sync; and third, point-in-time recovery, allowing you to restore a database to a specific moment.
WHEN NOT TO USE IT: Systems that prioritize extreme write performance over durability might operate without one. For example, some in-memory caches or analytics databases where losing a few recent writes is acceptable might disable it. This is a trade-off: you gain speed but lose the guarantee of durability.
ONE CANONICAL EXAMPLE: A bank transfer from Account A to Account B. The database first writes to the log: "BEGIN; DEBIT A by 100; CREDIT B by 100; COMMIT;". Only after this log is safely on disk does it change the account balances. If the power fails after the first update, the recovery process reads the log, sees the transaction wasn't committed, and rolls back the debit from Account A, preventing the money from vanishing.
Read the original → en.wikipedia.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.