The problem with plain files
Before Delta, a data lake was just a folder of Parquet files. Real problems ShopKart would hit:
- A job crashes halfway: some new files are written, some aren't. Reports now count half a day. π¬
- Fixing one order's status means rewriting the whole folder.
- Someone writes a file with the wrong columns, and every query breaks.
- A bad job overwrites yesterday's data, and there's no undo.
How Delta works: the transaction log
orders/ βββ _delta_log/ β βββ 00000000000000000000.json β version 0: "added files A, B" β βββ 00000000000000000001.json β version 1: "removed B, added C" (an UPDATE) β βββ 00000000000000000002.json β version 2: ... β βββ 00000000000000000010.checkpoint.parquet β summary every 10 commits βββ part-00000-A.snappy.parquet βββ part-00001-B.snappy.parquet β still here (needed for time travel) βββ part-00002-C.snappy.parquet
βοΈ1. Write new files
Parquet files are written, but nobody sees them yet
π2. Commit
Add the next JSON file to _delta_log, saying which files were added/removed
π3. Readers
Read the log to find the current files; half-written work is invisible
That's the whole trick. Files are never changed in place: an update writes new files and the log marks old ones as removed. A reader only trusts files listed in committed log entries, so a crash mid-write leaves the table untouched. If two writers commit at once, Delta uses optimistic concurrency: one wins, and the other checks for conflicts and retries or fails.
| ACID | Meaning | How Delta does it |
|---|---|---|
| Atomicity | All or nothing | A change only counts once its log commit exists |
| Consistency | Rules always hold | Schema enforcement, constraints |
| Isolation | No half-finished reads | Readers use a snapshot (one log version) |
| Durability | Committed = permanent | Files and log live in cloud storage |
UPDATE, DELETE and history
We continue with the shopkart.demo.orders table from the last lesson.
-- Change one order
UPDATE shopkart.demo.orders SET status = 'CANCELLED' WHERE order_id = 'O1002';
-- Remove one order
DELETE FROM shopkart.demo.orders WHERE order_id = 'O1004';
-- Every change is a new version of the table
DESCRIBE HISTORY shopkart.demo.orders;| version | operation |
|---|---|
| 2 | DELETE |
| 1 | UPDATE |
| 0 | CREATE OR REPLACE TABLE AS SELECT |
Time travel and RESTORE
-- The table as it was at version 0 (straight after we created it)
SELECT order_id, status FROM shopkart.demo.orders VERSION AS OF 0;
-- Oops? Put the whole table back to version 0
RESTORE TABLE shopkart.demo.orders TO VERSION AS OF 0;You can also travel by time, and in Python with spark.read.option("versionAsOf", 0).table(...):
SELECT * FROM shopkart.demo.orders TIMESTAMP AS OF '2026-11-20T09:00:00';Uses: undo a bad job, audit βwhat did this table say last Monday?β, reproduce an old report.
MERGE: the upsert (most important command)
Day two's file arrives. Order O1003 changed from PLACED to DELIVERED, and there are new orders. We want to update rows that exist and insert new ones in one safe step: an upsert.
# Tomorrow's file: one order changed (O1003 is now DELIVERED), two are new.
# The file has O1008 twice, so we drop exact duplicates first.
updates = (spark.read.schema(orders_schema)
.json("/Volumes/shopkart/raw/landing/orders/orders_2026-11-19.json")
.dropDuplicates(["order_id"])
.withColumn("amount", F.col("quantity") * F.col("price"))
.withColumn("order_date", F.to_date("order_ts")))
updates.createOrReplaceTempView("updates")MERGE INTO shopkart.demo.orders AS t
USING updates AS s
ON t.order_id = s.order_id
WHEN MATCHED THEN UPDATE SET * -- order exists β update it
WHEN NOT MATCHED THEN INSERT *; -- new order β add itResult: 4 rows became 6, and O1003 is now DELIVERED. MERGE can also delete (WHEN MATCHED AND s.deleted THEN DELETE) and add conditions (WHEN MATCHED AND s.updated_at > t.updated_at THEN UPDATE ...).
Schema enforcement and evolution
# A new column the table doesn't have: "coupon"
with_coupon = (updates.filter("order_id = 'O1007'")
.withColumn("order_id", F.lit("O1010"))
.withColumn("coupon", F.lit("DIWALI10")))
try:
with_coupon.write.mode("append").saveAsTable("shopkart.demo.orders")
except Exception as e:
print("Blocked by schema enforcement:", type(e).__name__)Delta refuses data that doesn't match the table (schema enforcement). When the change is intended, allow it (schema evolution):
(with_coupon.write
.mode("append")
.option("mergeSchema", "true") # yes, I really want the new column
.saveAsTable("shopkart.demo.orders"))
spark.table("shopkart.demo.orders").printSchema() # coupon is now a columnKeeping tables fast: OPTIMIZE, Z-ORDER, VACUUM
Many small writes create many small files, and reading 10,000 tiny files is much slower than reading 10 big ones (the small files problem).
-- Combine many small files into fewer big ones, sorted by customer_id
OPTIMIZE shopkart.demo.orders ZORDER BY (customer_id);
-- Delete old files no version needs any more (default: older than 7 days)
VACUUM shopkart.demo.orders RETAIN 168 HOURS;- OPTIMIZE: compacts small files into big ones (about 1 GB).
- ZORDER BY: sorts data inside files so similar values sit together. Delta keeps min/max stats per file, so a query
WHERE customer_id = 'C01'skips files that can't contain it (data skipping). - VACUUM: deletes old files no version within the retention period needs. After it, you can't time travel past that point.
-- New tables: let Delta keep data clustered for you (replaces partitioning + ZORDER)
CREATE OR REPLACE TABLE shopkart.demo.orders_clustered
CLUSTER BY (order_date)
AS SELECT * FROM shopkart.demo.orders;Liquid clustering is the modern replacement for partitioning and Z-ORDER on new tables: you choose clustering keys, can change them later, and Databricks clusters new data incrementally. With predictive optimization on, Databricks even runs OPTIMIZE and VACUUM for you.
Partitioning: when (not) to use it
PARTITIONED BY (order_date) splits a table into one folder per value. It helps only for big tables (1 TB+) and low-cardinality columns. Partitioning by a column with many values (like customer_id) creates millions of tiny files: a classic mistake. For most tables today: no partitions, use liquid clustering.
Other features to know
| Feature | What it does |
|---|---|
| Change Data Feed | Records row-level inserts/updates/deletes so downstream reads only changes |
| CHECK constraints | ALTER TABLE t ADD CONSTRAINT qty_positive CHECK (quantity > 0) |
| Generated columns | Columns computed automatically, e.g. order_date from order_ts |
| Deletion vectors | Mark deleted rows without rewriting whole files: faster DELETE/UPDATE/MERGE |
| DESCRIBE DETAIL | Location, number of files, size of a table |
| Shallow / deep CLONE | Copy a table's metadata only, or its data too (great for testing) |
π― Interview questions on this topic
Try to answer out loud first, then tap to check.
Q1What is Delta Lake and why use it over plain Parquet?Fresher+
Q2How does the Delta transaction log work?1β3 yrs+
Q3How do you implement an upsert in Databricks?1β3 yrs+
Q4Someone ran a bad job that deleted rows. How do you recover?1β3 yrs+
Q5OPTIMIZE vs VACUUM?1β3 yrs+
Q6Partitioning vs Z-ORDER vs liquid clustering?3+ yrs+
Q7What happens when two jobs write to the same Delta table at the same time?3+ yrs+
Key takeaways
- βThe transaction log is what turns files into a trustworthy table.
- βMERGE is the everyday tool for keeping tables up to date.
- βTime travel and RESTORE give you an undo button.
- βSmall files hurt; OPTIMIZE and clustering fix them, VACUUM cleans up.
Frequently asked questions
What is Delta Lake in simple words?
Delta Lake turns a folder of Parquet files into a reliable table. It keeps a transaction log (_delta_log) that records every change, which gives you safe concurrent writes, updates, deletes, MERGE and the ability to see older versions.
What does ACID mean?
Atomic (a write fully happens or not at all), Consistent (the table always follows its rules, like its schema), Isolated (readers never see half-finished writes), Durable (once committed, it stays). Delta gets these from its transaction log.
How long can I time travel?
As long as the old data files and log entries still exist. VACUUM deletes data files older than the retention period (7 days by default), and log entries are kept for 30 days by default. After that, older versions can't be read.
Z-ORDER or liquid clustering?
For new tables, Databricks recommends liquid clustering (CLUSTER BY): you can change the clustering columns later and it clusters incrementally. Z-ORDER is the older way, run with OPTIMIZE, and still common in existing tables and interviews.
What is Change Data Feed?
A table setting (delta.enableChangeDataFeed = true) that records row-level changes: inserts, updates (before and after) and deletes. Downstream jobs read only what changed instead of the whole table.
π§ Quick check: Delta Lake
1. Where does Delta record every change?
2. Which statement reads an old version?
3. MERGE is used for...
4. You append a DataFrame with an extra column. By default Delta...
5. What does VACUUM do?
Checking your accessβ¦