Back
+20 XP
Concept

Writing a Custom Embedded Append-Only WAL Engine

Write-Ahead Logging (WAL) writes every mutation sequentially to an append-only log before applying it to state. Append-only writes achieve maximum disk throughput because disk heads never seek 📜.

python
class MiniWAL:
    def __init__(self):
        self.log = []

    def append(self, op, key, val):
        self.log.append((op, key, val))  # Sequential append! 📜

    def reconstruct(self):
        state = {}
        for op, k, v in self.log:
            if op == "SET": state[k] = v
            elif op == "DEL": state.pop(k, None)
        return state

wal = MiniWAL()
wal.append("SET", "user", "alex")
wal.append("SET", "role", "admin")
print(wal.reconstruct())  # {'user': 'alex', 'role': 'admin'} 📜

# 26. Writing a Custom Embedded Append-Only WAL Engine Write-Ahead Logging (WAL) writes every mutation sequentially to an append-only log before applying it to state. Append-only writes achieve maximum disk throughput because disk heads never seek 📜. ```python class MiniWAL: def __init__(self): self.log = [] def append(self, op, key, val): self.log.append((op, key, val)) # Sequential append! 📜 def reconstruct(self): state = {} for op, k, v in self.log: if op == "SET": state[k] = v elif op == "DEL": state.pop(k, None) return state wal = MiniWAL() wal.append("SET", "user", "alex") wal.append("SET", "role", "admin") print(wal.reconstruct()) # {'user': 'alex', 'role': 'admin'} 📜 ``` This append-only architecture is the foundation of high-throughput distributed engines like Apache Kafka, LevelDB, RocksDB, and SQLite WAL mode! 📜 --- ### Question Why are append-only write logs drastically faster on disk than random-write updates? 🧠 - [ ] a. Sequential disk writes avoid expensive disk head seek latency and page rewrites 📜 - [ ] b. Append-only files are automatically stored in CPU L1 cache - [ ] c. They bypass operating system filesystem drivers - [ ] d. They disable SSD wear-leveling algorithms --- ### Challenge Create `MiniWAL` with `log = []`. Append `('SET', 'x', 10)` and `('SET', 'y', 20)`. Write `reconstruct()` returning final dictionary state. Print `wal.reconstruct()`. 🎯 <details> <summary>Solution</summary> ```python class MiniWAL: def __init__(self): self.log = [] def append(self, op, k, v): self.log.append((op, k, v)) def reconstruct(self): st = {} for op, k, v in self.log: if op == "SET": st[k] = v return st wal = MiniWAL() wal.append("SET", "x", 10) wal.append("SET", "y", 20) print(wal.reconstruct()) ``` </details>

Checkpoint

Why are append-only write logs drastically faster on disk than random-write updates? 🧠

Practice

Create `MiniWAL` with `log = []`. Append `('SET', 'x', 10)` and `('SET', 'y', 20)`. Write `reconstruct()` returning final dictionary state. Print `wal.reconstruct()`. 🎯

Terminal
01
Tap code blocks below to assemble
Available Blocks