Sub topic 2
How the Transaction Log Makes Changes Atomic
This chapter assumes you know basic SQL, can run cells in a Databricks notebook, and understand that a table has a schema and typed columns. You don't need to have created a Delta table yet. The next chapters cover that.
The problem the log solves
A Delta table is a folder of Parquet data files plus a folder named _delta_log. Parquet files are immutable, so an update can't edit a file in place. It has to write new files and stop using old ones. If readers simply listed the folder, they could see a half-finished change. The transaction log prevents that.
The log is an ordered series of JSON files in _delta_log. Each file is one commit, and each commit creates one table version. Version 0 is 00000000000000000000.json, version 1 is 00000000000000000001.json, and so on.
What a commit contains
A commit file lists actions. The common ones are:
| Action | Meaning |
|---|---|
| `metaData` | Schema, partition or clustering columns, and table properties |
| `protocol` | Minimum reader and writer versions needed for the table |
| `add` | A data file that is now part of the table, with its size and column statistics |
| `remove` | A data file that is no longer part of the table |
| `commitInfo` | Who ran what operation and when |
The table's current contents are the data files that have been added and not removed, when you replay the log from the start. Every so often Delta writes a checkpoint, a Parquet summary of the log up to some version. A reader can start from the latest checkpoint and replay only the JSON commits after it.
Why a change is atomic
Consider UPDATE ... SET status = 'shipped' WHERE order_id = 1001. Delta does the following:
- Reads the current table version and finds the files that contain matching rows.
- Writes new Parquet files with the changed rows. Nothing reads these yet, because no commit mentions them.
- Attempts to write one new commit file that has
removeactions for the old files andaddactions for the new ones.
The commit file is the only thing that changes what the table contains. Writing it either succeeds or it doesn't, so readers see the table entirely before the change or entirely after it. If the job fails at step 2, the new files are never referenced and the table is unchanged. They are orphaned files that VACUUM removes later.
ACID in practice
The log gives Delta the four ACID properties on top of cloud object storage.
- Atomicity: each statement commits as one version or not at all.
- Consistency: a commit that would break the schema or a constraint is rejected before it is written.
- Isolation: a query reads one fixed table version, so concurrent writers don't change the result partway through.
- Durability: a commit is a file in cloud storage, so it survives cluster restarts.
Seeing the log
You don't need to open the JSON files. DESCRIBE HISTORY shows one row for each commit.
DESCRIBE HISTORY main.sales.orders;The result has columns such as version, timestamp, operation and operationMetrics. The metrics report counts like files added and removed, and rows updated.
Common mistakes
- Don't edit or delete files in
_delta_logor in the table's data folder by hand. Doing so can corrupt the table. - Don't assume that a multi-statement notebook is one transaction. Each statement is its own commit.
- Don't expect readers to block. They read an older version until your commit lands.
Exercise
A job runs an UPDATE that rewrites 50 data files. It crashes after writing 30 of the new files and before writing the commit. What does a reader see, and what happens to the 30 files?
Show answer
The reader sees the table exactly as it was before the update, because the log has no commit for it. The 30 new files are not referenced by any commit. They remain in storage as orphaned files until VACUUM deletes them.