Sub topic 9
Delta Lake: reliable tables on open storage
This chapter assumes you know basic SQL and PySpark, understand cloud object storage, and know the difference between a data lake and a data warehouse. It also assumes you have read the earlier chapters on the lakehouse idea, compute, and notebooks. You will learn what a Delta table is on disk, how its transaction log gives you ACID guarantees, and how to read earlier versions of a table. You will also learn the errors people hit most often.
Why Delta Lake exists
A plain data lake stores files, such as Parquet, in object storage. Object storage is cheap and scales well, but it gives you no transactions. If a job fails halfway through writing 200 files, readers see a partial result. Two jobs writing at once can overwrite each other. Changing one row means rewriting files by hand.
Delta Lake is an open table format that fixes this. It keeps your data in Parquet files and adds a transaction log next to them. Together, the files and the log form a Delta table. Engines that understand the log treat the folder as a real table instead of a pile of files.
In Databricks, Delta is the default format for tables you create. The CREATE TABLE statement in the next section does not name a format, and it still produces a Delta table.
What a Delta table looks like on storage
A Delta table is a directory in object storage. It has two parts:
- Data files: ordinary Parquet files holding the rows.
- The
_delta_logfolder: JSON files that record each commit, plus periodic checkpoint files in Parquet that summarize the state so readers do not replay the whole history.
Each commit adds one numbered JSON file to _delta_log. A commit lists actions such as "add this data file" and "remove that data file". The current state of the table is the result of replaying those actions in order.
Because the data files are Parquet and the log format is documented, the storage is open. Other tools can read a Delta table without going through Databricks.
Creating a table and reading it
The following code creates a managed table in Unity Catalog. It uses the three-level name catalog.schema.table, which the next chapter covers in detail. It assumes you have permission to create objects in the main catalog.
CREATE SCHEMA IF NOT EXISTS main.tutorial;
CREATE TABLE IF NOT EXISTS main.tutorial.orders (
order_id BIGINT,
customer_id BIGINT,
amount DECIMAL(10, 2),
order_ts TIMESTAMP
);
INSERT INTO main.tutorial.orders VALUES
(1, 101, 25.00, current_timestamp()),
(2, 102, 40.50, current_timestamp()),
(3, 101, 15.25, current_timestamp());The INSERT is one commit. All three rows appear together, or none do.
In a notebook attached to compute, you can read the same table with the predefined spark session:
from pyspark.sql import functions as F
df = spark.table("main.tutorial.orders")
totals = (
df.groupBy("customer_id")
.agg(F.sum("amount").alias("total_amount"))
)
display(totals)display renders the result as an interactive table in the notebook. The SQL and PySpark paths read the same underlying files and log.
ACID guarantees
ACID stands for atomicity, consistency, isolation, and durability. Delta Lake provides these for operations on a single table.
| Property | What it means for a Delta table |
|---|---|
| Atomicity | A write either commits fully or not at all. A failed job leaves no visible partial data. |
| Consistency | A commit moves the table from one valid state to another. Schema checks reject bad writes. |
| Isolation | Readers see a consistent snapshot. Concurrent writers are checked for conflicts. |
| Durability | Once a commit is in the log in object storage, it survives cluster restarts. |
The log makes this work. A commit becomes visible only when its JSON file is written to _delta_log. Data files written before that point are invisible, because no log entry references them. Readers pick a log version at the start of the query and read only the files that version lists. A long-running query is not disturbed by a write that finishes in the middle.
For concurrent writers, Delta uses optimistic concurrency control. Each writer assumes no one else is changing the same data, does its work, and checks for conflicts at commit time. If another commit touched the same files in an incompatible way, one writer fails with a concurrency exception and you retry it.
Table history and time travel
Every commit is a numbered version. You can inspect them and read old ones:
DESCRIBE HISTORY main.tutorial.orders;
SELECT * FROM main.tutorial.orders VERSION AS OF 0;DESCRIBE HISTORY lists each version with its timestamp, the operation, and who ran it. VERSION AS OF 0 reads the table as it was at version 0. In this example, version 0 is the table creation, so the query returns no rows. The INSERT is version 1. Run SELECT * FROM main.tutorial.orders VERSION AS OF 1 to see the three rows.
You can also use TIMESTAMP AS OF '2026-12-01' to read the table as of a time.
Time travel works because old data files stay on storage after an update or delete. The log just stops listing them as current. This helps with auditing, debugging a bad pipeline run, and reproducing an earlier report.
Updating data
Because the log tracks files, Delta supports row-level changes that plain Parquet folders cannot do safely.
UPDATE main.tutorial.orders
SET amount = 26.00
WHERE order_id = 1;
DELETE FROM main.tutorial.orders
WHERE order_id = 3;
MERGE INTO main.tutorial.orders AS t
USING (SELECT 4 AS order_id, 103 AS customer_id,
12.00 AS amount, current_timestamp() AS order_ts) AS s
ON t.order_id = s.order_id
WHEN MATCHED THEN UPDATE SET *
WHEN NOT MATCHED THEN INSERT *;Each statement is one commit. Internally, an update rewrites the affected Parquet files. It writes new files containing the changed rows, then records "add new files, remove old files" in one commit. Some newer table features can mark deleted rows without rewriting files, but the commit model is the same.
MERGE is the usual tool for upserts: update rows that match and insert rows that do not.
Schema enforcement and evolution
Delta checks writes against the table schema. If you append a DataFrame with a column of the wrong type, or with an extra column the table does not have, the write fails instead of silently corrupting the table. This is schema enforcement.
When you do want to add columns, you can opt in to schema evolution. In PySpark, an append can use the mergeSchema option:
new_rows = spark.createDataFrame(
[(5, 104, 9.99, "web")],
"order_id BIGINT, customer_id BIGINT, amount DECIMAL(10,2), channel STRING",
)
(new_rows.write
.format("delta")
.mode("append")
.option("mergeSchema", "true")
.saveAsTable("main.tutorial.orders"))Without mergeSchema, this append fails because channel is not in the table. With it, the table gains a nullable channel column, and older rows read as null for it. Note that order_ts is missing from the new rows, so it is filled with null too.
Maintenance: small files and `OPTIMIZE`
Many small writes create many small Parquet files, and queries slow down because each file has overhead. The OPTIMIZE command compacts small files into larger ones:
OPTIMIZE main.tutorial.orders;OPTIMIZE is itself a commit, so it appears in the history. It does not change query results. Old files remain until VACUUM removes them. Databricks also offers automatic maintenance options for Unity Catalog managed tables, so check what is enabled in your workspace before scheduling these commands yourself.
Common errors
- Concurrent update exceptions. Two jobs changed overlapping files. Retry the failed job, or reduce overlap by writing to separate partitions or by filtering
MERGEconditions tightly. - Schema mismatch on write. The incoming data differs from the table schema. Fix the data, or opt in with
mergeSchema. Do not enable evolution everywhere by default, because a typo in a column name then creates a new column. - Time travel to a vacuumed version. The query fails because files are gone. Check the retention setting and
DESCRIBE HISTORYbefore relying on old versions. - Writing files directly into a table folder. Dropping Parquet files into the directory by hand does not add them to the log. Delta ignores them. Always write through Delta.
- Treating a Delta table as many tables. ACID applies per table. A job that updates two tables does so in two separate commits, and a failure between them leaves one changed.
Exercise
You run UPDATE on a Delta table, and a colleague queries the table at the same moment. The update has not finished. What does the colleague see, and why?
Show answer
They see the table as it was before the update. The update writes new data files first, but those files are not part of the table until the commit's JSON entry is written to _delta_log. The reader uses the log version that was current when its query started, so it reads only files that version lists.
Exercise
A table has had VACUUM run with the default retention. You try SELECT * FROM t VERSION AS OF 3 for a version that is 30 days old and the query fails. What happened, and what does this tell you about using time travel?
Show answer
Version 3 depended on data files that were no longer referenced by newer versions. VACUUM deleted files older than the 7 day default retention, so those files are gone and the version cannot be read. Time travel is useful for recent history, auditing, and debugging, but it is not a long-term backup. Keep separate backups or a longer retention period if you need older versions.