The Transaction Log

Concepts covered: Distributed Primitives

A table format adds 1 directory beside the data files: a log of numbered commits. In Delta the log lives in _delta_log/, and each commit is a JSON file named for its version: 00000000000000000000.json is version 0, 00000000000000000001.json is version 1. The log defines the table. A data file that no commit adds is invisible, whatever a directory listing says.

What a commit holds

A commit is a list of actions. An add names a data file joining the table, with its size and per-column stats. A remove names a file leaving it. A metaData action carries the schema and partition columns, and commitInfo records the operation. Below is version 2 of the running example, a DELETE that dropped 10 rows from part-001.parquet:

[
  {"commitInfo": {"operation": "DELETE", "timestamp": 1710513045000}},
  {"remove": {"path": "part-001.parquet", "dataChange": true}},
  {"add": {
    "path": "part-003.parquet",
    "size": 41210,
    "dataChange": true,
    "stats": "{\"numRecords\":340,\"minValues\":{\"order_id\":1201},\"maxValues\":{\"order_id\":1890}}"
  }}
]

The DELETE left part-001.parquet untouched on disk. It wrote part-003.parquet holding the 340 surviving rows and recorded the swap. Every edit to the table works this way, as new files plus log entries, because a Parquet file is never modified after it is written.

The directory versus the table

Of the 6 files on disk, 2 make up the table. part-005.parquet came from a job that crashed before committing, so no reader ever sees it. This is what makes a write atomic: data files go down first and stay invisible, and creating the next commit file publishes all of them at once.

One writer wins each version

Nothing stops 2 writers from finishing their data files at the same moment and both trying to create version 4. Exactly 1 must succeed, which takes an atomic create-if-absent on the commit file's name. Where the storage cannot do that, the format routes commits through a lock or a catalog service that hands out versions. What the loser does next is the subject of the next section.

Replaying the log

A reader rebuilds the table's state by keeping, for each file path, its newest action across all commits: a path whose newest action is add is live, and one whose newest action is remove is gone. The Delta protocol calls this action reconciliation. The animation replays the 4 commits of the running example: a groupBy on path finds each file's newest version, and a join brings back the action recorded there.

from pyspark.sql import functions as F

latest = log_actions.groupBy("path").agg(F.max("version").alias("version"))

live = (latest
    .join(F.broadcast(log_actions), ["path", "version"])
    .filter(F.col("action") == "add"))

live.collect()
log_actions
versionactionpathrecords
0addpart-000.parquet400
0addpart-001.parquet350
1addpart-002.parquet120
2removepart-001.parquet350
2addpart-003.parquet340
3removepart-000.parquet400
3removepart-002.parquet120
3addpart-004.parquet520

The groupBy is the only shuffle, since every action for a path has to reach 1 task before its newest version is known. The join wraps log_actions in F.broadcast, a few rows here, so it adds no shuffle. On a table with millions of files that shuffle is real work, which is why the log gets checkpointed.

Every 10 commits by default (delta.checkpointInterval), the writer saves the reconciled state as a Parquet checkpoint, and a _last_checkpoint file points at the newest one. A reader loads that checkpoint and replays only the JSON commits after it: at version 1,234, the checkpoint from version 1,230 plus 4 commits.

The stats in each add pay off at planning. For a filter such as order_id = 1500, Spark compares the value against every live file's min and max from the log and drops files that cannot match, with no directory listing and before any task is scheduled. Delta collects these stats for the first 32 columns by default. In the SQL tab of the Spark UI, the scan node's number of files read shows how many files survived.

The same latest-wins rule rebuilds current state from any change history. listing_attribute_changes records each edit to a listing's attributes with an increasing change_seq. Run the cell: it keeps each listing and attribute pair's highest change_seq, joins back for the value, and shows the result.

The join is where the latest-wins rule is enforced. F.max("change_seq") finds the newest change for each pair, but the aggregate keeps no attr_value, so a join has to bring the value back. Joining on listing_ref and attr_name alone would match every change ever made to the pair and return its whole history. Adding change_seq to the join columns lets only the newest change's row through. The log replay above does the same thing with path and version.

Suppose the cell above joins newest back to changes on listing_ref and attr_name only, dropping change_seq. What does current hold?

current = newest.join(changes, ["listing_ref", "attr_name"])

About this lesson

This section is part of the Storage Formats: Advanced lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.