Skip to content
Gold 6 min read · All formats 2 practice problems ↓

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

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

Update the tier of customer 42, stored in a 512 MB file with 4 million rows.

The command

spark.sql("UPDATE customers SET tier = 'gold' WHERE customer_id = 42")
UPDATE customers SET tier = 'gold' WHERE customer_id = 42;

CoW · write

Read 512 MB, write a new 512 MB file with the updated row, commit. Cost: one full file rewrite per touched file.

MoR · write

Write a tiny delete file (or deletion vectordeletion vector: A small list of the deleted row positions in one data file, so a delete does not rewrite the file. Learn more →) marking the old row, plus a small data file with the new row version. Cost: kilobytes.

MoR · read

Every reader of that file now also reads the delete file and filters the old row out. Thousands of updates mean thousands of small delete and data files until compaction runs.

Trade-offs

Copy-on-writeMerge-on-read
Write costHigh: rewrite touched filesLow: write small change files
Read costLowest: plain filesHigher: apply deletes while reading
Write latencySlow for scattered updatesFast, good for streaming upsertsupsert: Update a row if its key exists, insert it if not. Done with MERGE. Learn more →
MaintenanceCompaction for small files onlyRegular compaction needed to keep reads fast
Best forRead-heavy tables, batch updates, few changesFrequent updates and deletes, CDC, GDPR deletes

In each format

FormatHow
Delta LakeCopy-on-write by default. Deletion vectors (delta.enableDeletionVectors) give merge-on-read behaviour for DELETE, UPDATE and MERGE.
Apache IcebergPer 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 HudiChosen 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.
PySparkSpark SQL
# 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);
Why this matters: the choice is about where you pay: once at write time (CoW) or on every read until compaction (MoR). Tables updated constantly and read occasionally favour MoR; dashboards over rarely changing tables favour CoW.

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.

Production patternPySpark · Merge-on-read for a frequently updated table, with compaction
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-deletes cleaned 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.

ExplorePySpark · The same delete, copy-on-write vs merge-on-read
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?"

With copy-on-write, each touched file is rewritten. Switch merge mode to merge-on-read so updates write small delete and data files, schedule compaction to keep reads fast, and also reduce touched files with clustering on the merge key and pruning predicates. Discuss the read cost trade-off.

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?"
Position deletes mark specific rows by file and position; readers skip those rows. Equality deletes mark rows matching column values, for example a key, and are cheaper to write but more expensive to read. v3 adds deletion vectors as a compact form of position deletes.
Scenario"After switching to merge-on-read, BI queries got slower over a week. What is missing?"
Compaction. Delete files accumulate and every read must apply them. Schedule 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."
No. Hudi MOR writes changes to row-based log files per file group, merged on read or by compaction. Iceberg MOR writes delete files (and new data files). Same idea, different mechanics.

Check yourself

3 questions

Which 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 read
Apache 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

Previous: Hidden partitioning and partition evolution

Primary sources: Iceberg: write properties · Hudi: table types · Delta: deletion vectors