Copy-on-write vs merge-on-read
Table formats can apply an update or delete in two ways: rewrite the affected files at once, or write down the change and apply it when reading. This lesson compares copy-on-write and merge-on-read, what each costs writers and readers, and which fits which workload.
Step 3 · Data lake & lakehouse · Gold · Iceberg and Hudi, lesson 3 of 4
On this page
Show code in
Every code block on the page follows this.
Read first: MERGE INTO and upserts (skip if you know it)
In plain words first
To fix one wrong line in a printed book, you can reprint the whole page (slow to fix, easy to read), or stick an erratum slip in the back (quick to fix, but every reader must check the slip).
Copy-on-write reprints the page; merge-on-read uses the slip.
Copy-on-write
To delete one row from a 512 MB file, copy-on-write reads the whole file, writes a new 512 MB file without that row, and commitscommit: One atomic change to a table: a new version that lists which files were added and removed. Learn more → "remove old file, add new file". Readers only ever see plain data files.
Merge-on-read
Merge-on-read leaves the data file alone and writes a small file saying "row 1,234 of file X is deleted" (or, for updates, a delete plus the new row version). Readers apply those deletes while scanning. Periodic compactioncompaction: Rewriting many small files into fewer large ones, without changing the rows. Learn more → rewrites data files with the changes applied and drops the delete files.
Change · the update
The command
spark.sql("UPDATE customers SET tier = 'gold' WHERE customer_id = 42")
UPDATE customers SET tier = 'gold' WHERE customer_id = 42;
CoW · write
MoR · write
MoR · read
Trade-offs
| Copy-on-write | Merge-on-read | |
|---|---|---|
| Write cost | High: rewrite touched files | Low: write small change files |
| Read cost | Lowest: plain files | Higher: apply deletes while reading |
| Write latency | Slow for scattered updates | Fast, good for streaming upsertsupsert: Update a row if its key exists, insert it if not. Done with MERGE. Learn more → |
| Maintenance | Compaction for small files only | Regular compaction needed to keep reads fast |
| Best for | Read-heavy tables, batch updates, few changes | Frequent updates and deletes, CDC, GDPR deletes |
In each format
| Format | How |
|---|---|
| Delta Lake | Copy-on-write by default. Deletion vectors (delta.enableDeletionVectors) give merge-on-read behaviour for DELETE, UPDATE and MERGE. |
| Apache Iceberg | Per operation: write.delete.mode, write.update.mode, write.merge.mode = copy-on-write or merge-on-read. MoR uses position or equality delete files, and deletion vectors in format v3. |
| Apache Hudi | Chosen per table: COPY_ON_WRITE or MERGE_ON_READ table types. MoR stores base ParquetParquet: A columnar file format: values of each column are stored together, so queries read only the columns they need. Learn more → files plus row-based log files, merged by compaction. |
# Iceberg: make updates and merges merge-on-read spark.sql(""" ALTER TABLE db.customers SET TBLPROPERTIES ( 'write.update.mode' = 'merge-on-read', 'write.merge.mode' = 'merge-on-read', 'write.delete.mode' = 'merge-on-read') """) # Delta: enable deletion vectors spark.sql(""" ALTER TABLE customers SET TBLPROPERTIES ('delta.enableDeletionVectors' = true) """)
-- Iceberg: make updates and merges merge-on-read ALTER TABLE db.customers SET TBLPROPERTIES ( 'write.update.mode' = 'merge-on-read', 'write.merge.mode' = 'merge-on-read', 'write.delete.mode' = 'merge-on-read'); -- Delta: enable deletion vectors ALTER TABLE customers SET TBLPROPERTIES ('delta.enableDeletionVectors' = true);
Try it yourself
Each example says what it shows, then the output from a real run and what to notice in it, so you can follow without running anything. To try one yourself, paste it into a local Spark session with Iceberg, with spark already created: each builds its own data.
Goal: Use merge-on-read for a table with frequent small updates, and compact it.
t = "local.lab_mor.orders" spark.sql(f"DROP TABLE IF EXISTS {t} PURGE") spark.sql(f"""CREATE TABLE {t} (id BIGINT, status STRING) USING iceberg TBLPROPERTIES ('format-version' = '2', 'write.update.mode' = 'merge-on-read', 'write.delete.mode' = 'merge-on-read', 'write.merge.mode' = 'merge-on-read')""") spark.sql(f"INSERT INTO {t} SELECT id, 'NEW' FROM range(100000)") for i in range(3): # frequent small updates spark.sql(f"UPDATE {t} SET status = 'SHIPPED' WHERE id % 1000 = {i}") def delete_files(): return spark.sql(f"SELECT COUNT(*) FROM {t}.delete_files").first()[0] print("delete files before compaction:", delete_files()) spark.sql("""CALL local.system.rewrite_data_files( table => 'lab_mor.orders', options => map('rewrite-all', 'true', -- small table: force it 'remove-dangling-deletes', 'true'))""") print("delete files after compaction:", delete_files()) shipped = spark.sql(f"SELECT COUNT(*) FROM {t} WHERE status = 'SHIPPED'").first()[0] print("rows shipped:", shipped)
Output from a real run
delete files before compaction: 4 delete files after compaction: 0 rows shipped: 300
What to notice:
- Three updates left 4 delete files: merge-on-read wrote small delete files instead of rewriting data.
- Compaction applied them and left 0 delete files;
remove-dangling-deletescleaned up the ones that pointed at removed files. - 300 rows are
SHIPPED: the updates survived the compaction.
Goal: Delete one row with copy-on-write and with merge-on-read, and compare what each writes.
for mode in ["copy-on-write", "merge-on-read"]: t = "local.lab_mor." + mode.replace("-", "_") spark.sql(f"DROP TABLE IF EXISTS {t} PURGE") spark.sql(f"""CREATE TABLE {t} (id BIGINT, v STRING) USING iceberg TBLPROPERTIES ('format-version' = '2', 'write.delete.mode' = '{mode}')""") spark.sql(f"INSERT INTO {t} SELECT id, 'x' FROM range(100000)") spark.sql(f"DELETE FROM {t} WHERE id = 7") last = spark.sql(f"""SELECT summary FROM {t}.snapshots ORDER BY committed_at DESC""").first() s = last.summary print(f"{mode}: data files rewritten {s.get('added-data-files', '0')}, " f"delete files added {s.get('added-delete-files', '0')}")
Output from a real run
copy-on-write: data files rewritten 1, delete files added 0 merge-on-read: data files rewritten 0, delete files added 1
What to notice:
- Copy-on-write rewrote a whole data file to drop one row.
- Merge-on-read rewrote nothing and added one small delete file, which every reader applies until compaction folds it in.
Common mistakes
- Using MoR without scheduled compaction — Reads slow down as delete files pile up.
- Using CoW for streaming upserts — Each micro-batch rewrites many large files.
- Forgetting reader compatibility — Older engines may not read delete files or deletion vectors.
Key takeaways
- Copy-on-write rewrites touched files: costly writes, simple reads.
- Merge-on-read writes small change files and merges at read time.
- MoR needs regular compaction.
- Delta uses deletion vectors; Iceberg sets modes per operation; Hudi per table type.
Interview prep
THE QUESTION
"Our MERGE into a large Iceberg table takes an hour for a few thousand updates. What would you change?"
Avoid: "merge-on-read is always better because writes are faster". Reads pay to apply delete files until compaction.
Follow-ups · answer out loud, then open
Follow-up"What are position deletes and equality deletes in Iceberg?"
Scenario"After switching to merge-on-read, BI queries got slower over a week. What is missing?"
rewrite_data_files (or rewrite_position_delete_files) regularly so reads stay fast.Trap"Hudi's merge-on-read and Iceberg's merge-on-read store changes the same way."
Check yourself
3 questionsWhich is cheaper for frequent single-row updates?
Show the answer
Merge-on-read. MoR writes small delete/change files instead of rewriting whole files.
What must readers do with merge-on-read tables?
Show the answer
Apply delete files or deletion vectors while scanning. Changes are stored separately and merged at read time.
How does Delta Lake get merge-on-read behaviour?
Show the answer
Deletion vectors. Deletion vectors mark rows deleted without rewriting files.
Practise this
Problems that use this, graded on hidden tests
Keep going
Up next · Iceberg and Hudi · lesson 4 of 4 · 7 min readApache Hudi: keys, indexes and table services
Record keys, COW and MOR tables, the index, the timeline, and when Hudi is the right choice.
Related lessons
Deletion vectorsData lake & lakehouse · 7 min read
MERGE INTO and upsertsData lake & lakehouse · 5 min read
Compaction, OPTIMIZE and Z-order
Previous: Hidden partitioning and partition evolution
Primary sources: Iceberg: write properties · Hudi: table types · Delta: deletion vectors