Sub topic 2
How the transaction log makes changes atomic
This chapter explains the mechanism that lets a Delta table survive failed jobs, parallel writers and concurrent readers. It assumes you can run SQL and PySpark in a Databricks notebook and know what a Parquet file is. Later chapters cover the commands that create commits, such as UPDATE, DELETE and MERGE. Here you learn what a commit is.
The problem the log solves
A plain folder of Parquet files has no notion of a transaction. Suppose a job writes 200 files and fails after 120. Readers now see a half-written dataset. Suppose two jobs write at once. Their files mix with no record of which belong together. Suppose you want to change one row. You must rewrite a file by hand and hope nobody reads it mid-swap.
Delta Lake fixes this by separating two things:
- The data files are ordinary Parquet files, and they are never modified once written.
- The table itself is defined by a separate ordered record, the transaction log, stored in a
_delta_logfolder next to the data.
A file belongs to the table only if the log says so. Writing a file does not change the table. Only a successful write to the log does.
What the log contains
Each folder _delta_log holds one JSON file per commit. A commit is one atomic change to the table. File names are the version number, zero-padded to 20 digits.
orders/
_delta_log/
00000000000000000000.json
00000000000000000001.json
00000000000000000002.json
part-00000-a1b2.snappy.parquet
part-00001-c3d4.snappy.parquetEach JSON file is a list of actions, one per line. The common actions are:
| Action | Meaning |
|---|---|
| `protocol` | The reader and writer feature versions a client must support to use the table. |
| `metaData` | The schema, partition or clustering columns, table properties, and table ID. |
| `add` | A data file is now part of the table. Includes size and per-column statistics. |
| `remove` | A data file is no longer part of the table. The file stays on storage for now. |
| `commitInfo` | Audit information: operation name, user, timestamp, and metrics. |
| `txn` | An application ID and version, used by streaming writes to avoid duplicates. |
A simplified commit for the first insert into a table looks like this. Real entries carry more fields, and each action sits on one line.
{"commitInfo": {"operation": "WRITE", "operationParameters": {"mode": "Append"}}}
{"add": {"path": "part-00000-a1b2.snappy.parquet", "size": 1843, "dataChange": true}}
{"add": {"path": "part-00001-c3d4.snappy.parquet", "size": 1790, "dataChange": true}}The table state at version N is the result of replaying commits 0 through N. Every add puts a file into the set. Every remove takes one out. What remains is the list of files a query reads.
How an update becomes a single commit
Take this statement from your orders table:
UPDATE main.sales.orders
SET status = 'shipped'
WHERE order_id = 1001;Delta does not edit a Parquet file. It runs these steps:
- Read the current version of the log and find the files that may contain
order_id = 1001. Per-file min and max statistics in theaddactions let it skip files that cannot match. - Read the matching files, apply the change, and write new Parquet files.
- Prepare one commit file containing
removeactions for the old files andaddactions for the new ones. - Try to create the next numbered JSON file in
_delta_log. This is the commit.
Until step 4 succeeds, no reader or other writer can see any of the work. After step 4, all of it is visible at once. That is atomicity: the change is applied as a whole or not at all.
The ACID guarantees, one by one
ACID is a set of four properties. Here is how the log provides each.
- Atomicity. A commit is one log file, and creating that file is a single operation on cloud object storage. Delta relies on the storage layer rejecting a create if the file already exists, or on a coordination service in the Databricks case. Either the file exists and the whole commit applies, or it doesn't and none of it does.
- Consistency. Constraints such as NOT NULL and CHECK are verified before the commit is attempted. A write that violates them fails and never reaches the log. Schema checks work the same way. Later chapters cover both.
- Isolation. A query reads one specific version. It resolves the file list when it starts and keeps using it, even if other writers commit while it runs. Writers use optimistic concurrency control, described below.
- Durability. Once the commit file is in cloud storage, the change is as durable as the storage itself.
Concurrent writers and optimistic concurrency
Delta does not lock tables. Each writer works from the version it started with. When it tries to commit version N+1 and another writer got there first, the log rejects the create. The loser then checks whether the winning commit changed anything it read or wants to change.
- If the two changes are independent, such as appends to a table or updates to different files, the loser rebases onto the new version and commits again as N+2. You never notice.
- If they overlap, for example both rewrite the same file, the loser fails with a concurrent modification error such as
ConcurrentAppendExceptionorConcurrentDeleteReadException. The data stays correct and you retry the operation.
Which pairs of operations conflict, and how to reduce conflicts, is the subject of the chapter on concurrent writes. Here, remember that the numbered-file rule is what makes a conflict detectable.
Inspecting the log yourself
You rarely read the JSON directly. Two commands give you the useful view.
DESCRIBE HISTORY main.sales.orders;
DESCRIBE DETAIL main.sales.orders;DESCRIBE HISTORY returns one row per commit, with version, timestamp, user, operation and metrics such as rows written. It is built from the commitInfo actions. DESCRIBE DETAIL returns table-level facts, including the storage location, the number of current files, and the total size.
To see the log files, take the location from DESCRIBE DETAIL and list the folder:
location = spark.sql("DESCRIBE DETAIL main.sales.orders").first()["location"]
for f in dbutils.fs.ls(location + "/_delta_log"):
print(f.name, f.size)Unity Catalog may block direct path access to a managed table's storage. If the listing is denied, that is expected. Use DESCRIBE HISTORY instead, or try the listing on an external table you own. Never write into this folder. Use the table commands only.
What stays on storage after a commit
Because data files are immutable, a remove action does not delete anything. The old file remains in the table folder, and the log simply stops listing it. This has three consequences.
- Readers that started before your commit can finish reading the old files safely.
- Earlier versions stay readable, which is what time travel uses. The next chapters on history and RESTORE build on this.
- Storage grows until you run VACUUM, which deletes files that the log no longer references and that are older than the retention period.
A job that fails after writing files but before committing leaves orphan files. They appear in no version, so no query ever reads them. VACUUM removes them eventually.
Edge cases to know
- Deletion vectors. On tables with this feature enabled, a DELETE or UPDATE may not rewrite the Parquet file. Instead it records which rows in the file are removed, and the rewrite is deferred. The log still carries the change as one atomic commit. The difference is that fewer data files are rewritten.
- No-op statements. An UPDATE or DELETE that matches no rows still runs but may write a commit with no data changes, or none at all. Check
DESCRIBE HISTORYmetrics rather than assuming. - Multi-table changes. A commit covers one table. Two statements on two tables are two separate commits, and a failure between them leaves one applied. Delta does not offer a transaction across tables in the way a classic database does.
- Multi-statement work on one table. Each SQL statement is its own commit. A notebook with an INSERT followed by an UPDATE produces two versions, and readers can see the state between them. If a change must appear together, express it as one statement, for example a single MERGE.
Exercise
A job runs UPDATE main.sales.orders SET status = 'shipped' WHERE status = 'new'. It writes 40 new Parquet files, then the cluster dies before the commit file is created. What does a reader of the table see afterward, and what happens to the 40 files?
Show answer
The reader sees the table exactly as it was before the update. A file counts as part of the table only if an add action in the log lists it, and no commit was written. The 40 files are orphans on storage. No query reads them, and VACUUM can delete them once they are older than the retention period.
Exercise
Two jobs both read version 7 of a table and both try to commit. Job A creates 00000000000000000008.json first. What happens to job B when it tries to create the same file, and what decides the outcome?
Show answer
B's create is rejected because version 8 already exists. B then checks whether A's commit conflicts with what B read or changed. If the changes are independent, B rebases and commits as version 9. If they overlap, B fails with a concurrent modification exception and you retry the operation.
Exercise
Why does a DELETE statement not immediately reduce the storage used by the table?
Show answer
DELETE commits remove actions in the log, but the old Parquet files stay on storage so that earlier versions and running queries can still use them. Space is freed only when VACUUM deletes unreferenced files older than the retention period.