ACID and Time Travel

Concepts covered: Distributed Primitives

With the log as the table, each property a database promises becomes a rule about commits. Atomicity comes from the single commit file and durability from storing it durably. Isolation and consistency between writers come from how readers pick a version and how writers claim the next one.

Readers pin a snapshot

A query resolves the table once, at planning: it reads the newest commit, say version 2, and fixes the list of files that version names. Every task in the query reads from that list for as long as the query runs. This is snapshot isolation: a query sees the table as of 1 committed version and never sees a write halfway applied.

Follow it through the running example. A report plans against version 2 and is handed part-000.parquet, part-002.parquet and part-003.parquet. Midway, a compaction commits version 3, removing part-000.parquet and part-002.parquet and adding part-004.parquet. The report keeps reading part-000.parquet without error, because a remove only writes a log entry. The file stays on disk until a cleanup deletes it, and the cleanup waits out a retention window first.

Both versions hold 860 rows: version 3 packed 400 and 120 rows into 1 file of 520 without changing a value. The log flags such commits with dataChange: false.

Writers race optimistically

Writers hold no lock while they work. Each records the version it started from, writes its data files, and then tries to create the next commit. If that version is taken, it reads the commits that beat it and runs 2 checks. Did any of them remove or rewrite a file it read? Did any of them add files inside the data it read, such as a partition its filter selected? If both answers are no, it retries the commit at the following version, reusing the data files it already wrote. If either is yes, committing would act on a table it never saw, and it fails with a concurrent modification error. So a delete of order_date = '2024-03-01' fails when another job appends into that partition first, because the new rows match its filter and the delete would skip them. When 2 blind appends hit 1 table, both commit, since neither reads anything.

Two jobs start from version 7 of orders, partitioned by order_date. Job A appends files to order_date=2024-03-15 while Job B deletes rows where order_date is 2024-03-01, and A commits version 8 first. What happens to Job B?

Reading an old version on purpose

Replaying the log only up to version N gives the table exactly as version N left it. Spark exposes that as a read option on the format:

path = "/lake/orders"

v1 = spark.read.format("delta").option("versionAsOf", 1).load(path)
latest = spark.read.format("delta").load(path)

removed = v1.join(latest, "order_id", "left_anti")

timestampAsOf takes a time instead and resolves to the newest version committed at or before it. Use either to rerun last Tuesday's report on last Tuesday's data, to audit what a bad job changed, or to roll back by committing an old version's file list as a new version.

How far back you can go

Time travel needs 2 pieces to survive: the log entries for the version and the data files they name. Delta keeps log entries for 30 days by default (delta.logRetentionDuration), and its VACUUM command deletes data files that were removed from the table more than 7 days ago (delta.deletedFileRetentionDuration). Retention is a storage budget: a table fully rewritten every day keeps about 1 extra copy of itself per retained day, and once a version's files are vacuumed, reading it fails even while its log entries remain.

On the change history from the last section, time travel is the same replay stopped early. Run the cell: it rebuilds the listings as of the midpoint of their history and as of the newest change, then lines the 2 up so you can spot values that moved.

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.