Course contentsShow
← Databases & SQL
Lesson 1224 of 1,95311. NoSQL and Document ModelsFree lesson

Data Compaction and Fragmentation

Manage storage engine maintenance tasks to prevent space leakage over time.

In this lesson, you will learn how document-oriented storage engines manage physical layout to prevent performance degradation caused by space leakage.

The Lifecycle of Document Storage

When you update a document in a NoSQL database, the engine doesn’t always overwrite the existing bytes in place. If an update increases the document's size—perhaps by adding a new field or appending an item to an array—the storage engine may find that the new, larger document no longer fits in its original physical location on disk.

To handle this, the engine often writes the updated document to a new location and marks the old space as "free." Over time, frequent updates and deletes create "holes" in your data files. This phenomenon is known as fragmentation.

The Cost of Fragmentation

Fragmentation acts as "space leakage." If your storage engine has 10GB of data, but 4GB of that is scattered, unusable empty holes, your I/O throughput drops significantly. The database must scan through these empty gaps to retrieve actual data, wasting precious disk bandwidth and increasing latency.

To reclaim this space, storage engines utilize compaction. This process rewrites the data files by copying all active documents into a new, contiguous file, effectively "squeezing out" the empty holes.

Maintenance Strategies

While many modern databases perform this process automatically in the background, you must monitor your storage engine's behavior:

  1. Background Compaction: Most engines run a background process to reclaim space. Ensure this process isn't competing for CPU or I/O during your peak traffic hours.
  2. Scheduled Maintenance: In some systems, fragmentation can grow faster than background processes can clean it. You might need to trigger a "compact" command during a maintenance window.
  3. Capacity Planning: Because compaction often requires creating a temporary copy of your data, you must maintain enough free disk space (often 20-30%) to allow the engine to perform these operations without running out of room.

By understanding that document updates aren't always surgical, you can better tune your maintenance windows to ensure your storage stays lean and performant.

Key Takeaway: Fragmentation is a natural consequence of dynamic document updates; proactive monitoring and well-timed compaction are essential to preventing storage bloat and maintaining high-performance I/O.