Sub topic 9
Deleting Data with Tombstones
This chapter assumes you know how the memtable, SSTables, and the read path fit together. It covers how an LSM store deletes a key, and why that is harder than it sounds.
Why a delete cannot remove data
SSTables are immutable. A key you want to delete may sit in several old SSTables, and you cannot edit those files. Removing it from the memtable alone would let an older value reappear on the next read.
The fix is to write a new entry that says "this key is deleted." That entry is a tombstone. It goes through the same path as any write: memtable, flush, SSTable.
How reads treat tombstones
The read path checks the memtable first, then SSTables from newest to oldest. The first entry found for a key decides the result. If that entry is a tombstone, the key is deleted and the search stops. Older tables are never consulted.
import bisect
TOMBSTONE = object()
class SSTable:
"""Immutable sorted run of (key, value) pairs."""
def __init__(self, items):
self.keys = [k for k, _ in items]
self.values = [v for _, v in items]
def get(self, key):
i = bisect.bisect_left(self.keys, key)
if i < len(self.keys) and self.keys[i] == key:
return True, self.values[i]
return False, None
class TinyLSM:
def __init__(self, memtable_limit=4):
self.memtable = {}
self.sstables = [] # oldest first, newest last
self.memtable_limit = memtable_limit
def put(self, key, value):
self.memtable[key] = value
if len(self.memtable) >= self.memtable_limit:
self.flush()
def delete(self, key):
self.put(key, TOMBSTONE)
def flush(self):
if not self.memtable:
return
self.sstables.append(SSTable(sorted(self.memtable.items())))
self.memtable = {}
def get(self, key):
if key in self.memtable:
value = self.memtable[key]
return None if value is TOMBSTONE else value
for table in reversed(self.sstables): # newest first
found, value = table.get(key)
if found:
return None if value is TOMBSTONE else value
return None
db = TinyLSM(memtable_limit=2)
db.put("a", 1)
db.put("b", 2) # flush: table 1 holds a and b
db.delete("a")
db.put("c", 3) # flush: table 2 holds the tombstone for a, and c
print(db.get("a")) # None: the newer tombstone hides the old value
print(db.get("b")) # 2
print(len(db.sstables)) # 2The old value of a is still on disk in the first table. Only the tombstone hides it.
Dropping tombstones during compaction
Tombstones take space, and they keep the dead value on disk. Compaction reclaims both. When it merges tables, the newest entry for each key wins, so the tombstone replaces the older value.
A tombstone can be dropped only when no older value of that key can exist outside the merge. Here is a version that does it when every table is part of the merge:
def compact(tables, drop_tombstones):
"""Merge SSTables (given oldest first) into one; newer values win."""
merged = {}
for table in tables:
for key, value in zip(table.keys, table.values):
merged[key] = value
items = sorted(merged.items())
if drop_tombstones:
items = [(k, v) for k, v in items if v is not TOMBSTONE]
return SSTable(items)
def compact_all(db):
if db.sstables:
db.sstables = [compact(db.sstables, drop_tombstones=True)]Dropping a tombstone too early is a real bug. Suppose you merge only the two newest tables and drop a tombstone, while an older table outside the merge still holds a = 1. The next read falls through to that table and returns the deleted value. The key comes back from the dead.
Real engines add one more rule. In replicated systems such as Cassandra, a tombstone must also outlive a grace period, so a replica that was offline during the delete cannot later re-send the old value.
Costs to watch
- A delete-heavy workload leaves many tombstones that reads must step over, especially in range scans.
- Space is not freed until compaction reaches both the tombstone and the value it hides.
- A tombstone is a write, so deletes count toward write amplification.
Exercise
A store has three SSTables. The oldest holds x = 5. The middle one is empty of x. The newest holds a tombstone for x. A compaction merges only the middle and newest tables. Can it drop the tombstone for x, and why?
Show answer
No. The oldest table is outside the merge and still holds x = 5. If the tombstone were dropped, a read would fall through to the oldest table and return 5, so the deleted key would reappear. The merge must keep the tombstone.