Sub topic 1
How Delta Lake records changes: the transaction log
Prerequisites
This chapter assumes you can:
- Run basic SQL (
SELECT,WHERE,ALTER TABLE). - Work in a Databricks notebook or the SQL editor.
- Read simple PySpark code that writes and reads a DataFrame.
You do not need to know Delta internals. This chapter builds the model that the rest of the tutorial relies on.
The log is the table
A Delta table is two things stored together: Parquet data files, and a folder named _delta_log that holds the transaction log. The log is the source of truth. A file in the table's directory is part of the table only if the log says so.
The log is an ordered series of commit files. Each successful write, such as an INSERT, UPDATE, DELETE, MERGE, or schema change, adds exactly one new commit file. The file name is a zero-padded version number in JSON format:
_delta_log/
00000000000000000000.json
00000000000000000001.json
00000000000000000002.json
00000000000000000010.checkpoint.parquet
_last_checkpointVersion 0 is the commit that created the table. Every later commit increments the number by one. Chapter 2 covers versions in detail. For now, note that a table version is the state you get by replaying the log up to that number.
What a commit file contains
A commit file is a list of actions. Each action is one JSON object. The common ones are:
| Action | Meaning |
|---|---|
| `metaData` | Schema, partition columns, and table properties |
| `protocol` | Minimum reader and writer versions needed for the table |
| `add` | A data file is now part of the table. Includes path, size, and statistics |
| `remove` | A data file is no longer part of the table |
| `commitInfo` | Who did what: operation name, timestamp, parameters, metrics |
Two points matter for time travel.
First, Delta never edits a Parquet file in place. An UPDATE writes new files and records add for them, plus remove for the old files they replace.
Second, remove is a logical delete. The old file stays in storage after the commit. Later queries ignore it, but a query against an earlier version can still use it. That is the mechanism behind time travel. VACUUM eventually deletes these files, which is why it limits how far back you can go.
Reading the current state: replay and checkpoints
To find which files make up the table, an engine replays the actions in order. An add puts a file in the set, and a remove takes it out. The result is the active file list for that version.
Replaying thousands of JSON files would be slow. By default, Delta writes a checkpoint every 10 commits. A checkpoint is a Parquet file that stores the complete state at that version. The _last_checkpoint file points to the newest one. A reader loads the latest checkpoint at or before the version it wants, then replays only the JSON commits after it.
How a commit stays safe
Delta uses optimistic concurrency control. A writer does its work without locking the table:
- Read the current version of the table.
- Write new Parquet data files. Nothing is visible yet.
- Try to create the next commit file, for example
...0003.json.
Creating that file succeeds for only one writer. If two writers both aim for version 3, one wins. The loser checks whether the winner's changes conflict with what it read. If they do not conflict, it retries as version 4. If they do conflict, it fails with an exception such as ConcurrentAppendException.
Readers never see a half-finished write. Until the commit file exists, the new data files are not in the table.
Look at a log yourself
This example builds a small table at a path and inspects its log. Replace the volume path with one you can write to.
path = "/Volumes/main/sales/landing/orders_demo"
spark.range(3).write.format("delta").mode("overwrite").save(path)
spark.range(3, 5).write.format("delta").mode("append").save(path)
display(dbutils.fs.ls(path + "/_delta_log"))
(spark.read.json(path + "/_delta_log/00000000000000000001.json")
.select("commitInfo.operation", "add.path", "add.size")
.show(truncate=False))The listing shows two commit files, 00000000000000000000.json and 00000000000000000001.json. The second query shows that version 1 has operation WRITE and an add action for the new data file. The columns are null on rows where that action does not appear, because each row of the log file holds one action.
Common mistakes
- Editing or deleting files in
_delta_logby hand. This can corrupt the table or erase history. Treat the folder as read-only. - Copying only the Parquet files to another location. Without the log, the copy has no table state, schema, or history. Use
DEEP CLONEor a normal write instead. - Deleting data files directly in storage. The log still lists them, so queries fail with file-not-found errors.
- Assuming a
DELETEfrees storage right away. It only writesremoveactions. Space is reclaimed afterVACUUM.
Exercise
A notebook runs DELETE FROM main.sales.orders WHERE status = 'test'. What does this add to the transaction log, and why can you still query the table as it was before the delete?
Show answer
It adds one new commit file with remove actions for the old data files that held matching rows, add actions for any rewritten files that keep the non-matching rows, and a commitInfo action. The removed Parquet files stay in storage until VACUUM deletes them, so the earlier version, which lists those files as active, can still be read.