#2342·badger

TTL-expired data in the last level is never reclaimed

Author: eutkinCreated Sep 30, 2026Updated Sep 30, 2026

Title

TTL-expired data in the last level is never reclaimed, even with LmaxCompaction=true and NumCompactors>=3; db.Flatten() also skips the last level

What version of Badger are you using?

v4.9.6

Describe the bug

In an append-only workload with monotonically increasing keys (consecutive memtables cover non-overlapping key ranges) and per-entry TTL, every table arriving in the last level has been rewritten by compactions while its data was still fresh, so StaleDataSize == 0. When the TTL later expires, the data sits in the last level forever:

  1. Score-based picking never selects the last level: pickCompactLevels drops the last priority (prios[:len(prios)-1]), and the last level's score is its own size divided by a target equal to its own size (levelTargets), which is <= 1.0 forever.
  2. The only path that can rewrite L6 is tryLmaxToLmaxCompaction, gated on opt.LmaxCompaction && compactor id == 2 and only every ~10s (levels.go:550).
  3. That path bails out in fillMaxLevelTables as soon as the top-sorted table reports StaleDataSize() == 0 (levels.go:1339). StaleDataSize is only populated via Builder.AddStaleKey for keys that were already expired at rewrite time (levels.go:851); it is never updated when keys expire in place. So for pure TTL workloads the gate never opens. (There are two further gates: tables younger than 1h and stale size < 10MB are skipped.)
  4. db.Flatten() cannot recover either: it compacts the shallowest non-empty level each iteration and stops when a single level remains (db.go ~1722), and pickCompactLevels never returns the last level — so the last level is never rewritten even on request.

The read path correctly hides expired keys, so this is purely a space-reclamation bug: iteration shows 0 live keys while the last level keeps holding the full pre-expiry volume.

Repro

  • Open badger with LmaxCompaction = true, WithNumCompactors(3).
  • Write ~512MB of incompressible values (2048B, crypto/rand) under monotonically increasing keys (%020d) with TTL 3m, in batches of 200 entries per txn.
  • Wait 90s (tree settles into L6 while fresh), then sample db.Levels() / db.Tables() / iteration count every 30s.

Observed (macOS arm64, v4.9.6):

after settle (data fresh):      L6: 219 tables, 410 MB, stale 0 MB, 262200 live keys
t+90s (TTL expired):            L6: 219 tables, 410 MB, stale 0 MB, 0 live keys
t+210s (~12 Lmax attempts):     L6: 219 tables, 410 MB, stale 0 MB, 0 live keys
after db.Flatten(8):            L6: 219 tables, 410 MB, stale 0 MB, 0 live keys
                                (only upper levels were rewritten: SST total 524MB -> 410MB)

Full repro source is a single main.go (~180 lines) — happy to attach or paste.

Related issues

#464, #562, #974, #1228, #703 describe TTL-driven growth / unreclaimed space; #2327 touches the same levelTargets() path. None of them isolate the last level: stale-data gate described here, which this report does.

Possible directions (questions, not prescriptions)

  • Track per-table max ExpiresAt (or estimated expired bytes) in the table index at build time (#562 proposes per-file TTL metadata) so fillMaxLevelTables can pick uniformly-expired tables without a stale marker.
  • Or let the Lmax->Lmax path pick tables by a cheap expiry estimate instead of bailing when StaleDataSize == 0.
  • Or make db.Flatten() include the last level explicitly.

From human: I use badgerV4 as embedded LSM-database in my antifraud engine. I had testing TTL-functional last night, 8 hours, 600 TPS, 1-2 kb per messages. Next, I decide trying use to Pebble