shlogg · Early preview
Franck Pachot @franckpachot

MongoDB Storage Size With TTL Indexes

MongoDB's TTL indexes don't cause fragmentation when used with deletion, as space is reused automatically, maintaining a constant free space of 25%. Manual compaction can temporarily reclaim more space but returns to normal volume.

In a previous blog post, I explained how MongoDB TTL indexes work and their optimization to avoid fragmentation during scans. However, I didn’t cover the details of on-disk storage. A recent Reddit question is the occasion to explore this aspect further.

  
  
  Reproducible example

Here is a small program that inserts documents in a loop, with a timestamp and some random text:

// random string to be not too friendly with compression
function getRandomString(length) {
  const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  let result = '';
  const characters...