Delta Lake ACID Transaction Log Mock in Python

Simulates Delta Lake's transactional log with JSON files for atomic commits, versioned operations, and crash recovery

Medium Python 3.9+ Aug 9, 2026 Big data & Spark 15 views 0 copies

Python code

69 lines
Python 3.9+
import json
import time
from pathlib import Path

class DeltaLog:
    def __init__(self, path):
        self.log_dir = Path(path)
        self.log_dir.mkdir(parents=True, exist_ok=True)
        self.version = 0

    def _write_txn(self, action, payload):
        txn = {
            "version": self.version,
            "timestamp": int(time.time() * 1000),
            "action": action,
            **payload
        }
        file_name = f"{self.version:020d}.json"
        (self.log_dir / file_name).write_text(
            json.dumps(txn, indent=2) + "\n"
        )
        self.version += 1
        return txn

    def begin_txn(self):
        return self._write_txn("BEGIN", {})

    def add_file(self, path, rows):
        return self._write_txn("ADD", {"path": path, "row_count": rows})

    def remove_file(self, path):
        return self._write_txn("REMOVE", {"path": path})

    def commit(self):
        return self._write_txn("COMMIT", {})

    def read_log(self):
        log = []
        for f in sorted(self.log_dir.glob("*.json")):
            log.append(json.loads(f.read_text()))
        return log

    def has_open_txn(self):
        opens = 0
        for txn in self.read_log():
            if txn["action"] == "BEGIN":
                opens += 1
            elif txn["action"] == "COMMIT":
                opens -= 1
        return opens > 0


if __name__ == "__main__":
    dlog = DeltaLog("delta_log_mock")
    txn1 = dlog.begin_txn()
    dlog.add_file("data/part-0001.parquet", 1200)
    dlog.add_file("data/part-0002.parquet", 850)
    dlog.commit()

    txn2 = dlog.begin_txn()
    dlog.remove_file("data/part-0001.parquet")
    dlog.add_file("data/part-0003.parquet", 1500)
    dlog.commit()

    log_entries = dlog.read_log()
    print(f"Total versions: {len(log_entries)}")
    print(f"Open txn exists: {dlog.has_open_txn()}")
    print(f"Latest version: {log_entries[-1]['version']}")
    print(f"Last action: {log_entries[-1]['action']}")

Output

stdout
Total versions: 6
Open txn exists: False
Latest version: 5
Last action: COMMIT

How it works

The DeltaLog class persists each transaction action (BEGIN, ADD, REMOVE, COMMIT) as a versioned JSON file mirroring Delta Lake's design. Each write increments the log version, enabling point-in-time querying and recovery. The has_open_txn method simulates crash detection by counting BEGIN vs COMMIT entries. Files are read chronologically to reconstruct the full transaction history.

Common mistakes

  • Not using a zero-padded filename format, breaking version ordering
  • Forgetting to increment version in commit or handling rollback scenarios
  • Reading uncommitted transactions when reconstructing table state

Variations

  1. Use a single JSON array file instead of individual versioned files
  2. Add a `rollback` method to undo uncommitted changes

Real-world use cases

  • Prototyping a Delta table writer before integrating with the actual Delta Lake library
  • Teaching the transaction log format for data engineering interviews or documentation
  • Testing consumer/query logic against a lightweight, deterministic log format

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Big data & Spark

Related tutorials and quizzes for this topic.