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

Storage Engines and Document Locality

Explain how document engines organize data on disk for fast access to related fields.

In this lesson, you will learn how document-oriented storage engines arrange data on disk to minimize input/output (I/O) operations and maximize read performance.

The Concept of Locality

In relational databases, data is often normalized across multiple tables. To retrieve a complete record, the storage engine must perform "joins," which often require jumping to different locations on the disk. Document engines, however, utilize document locality. They store the entire document—including its nested objects and arrays—as a single contiguous block of data.

Because the engine writes the entire BSON document to a single place on the disk, reading that document requires only one or two sequential disk reads rather than seeking across multiple memory addresses or tables.

Visualizing Data Layout

Imagine you are storing a User profile. In a relational model, you might have a Users table and an Addresses table. To see a user’s address, the engine must look in two places. In a document model, the storage engine serializes the data together:

{
  "_id": "user_123",
  "name": "Jane Doe",
  "contact": {
    "email": "jane@example.com",
    "address": {
      "street": "123 Maple St",
      "city": "Tech City"
    }
  }
}

When the storage engine reads this document from the disk, the physical blocks containing the "name," "email," and "address" are loaded into memory simultaneously. This is the essence of locality: if data is frequently accessed together, it should be stored together.

The Trade-off

This design is highly optimized for read-heavy workloads where you need the "whole picture" of an entity. However, there is a physical limit: if a document grows significantly (for example, if you append hundreds of items to an array), it may outgrow its allocated space on the disk. The storage engine then faces fragmentation, where it must move the document to a new, larger space to maintain that contiguous locality. Balancing the document size is therefore vital to keep these I/O benefits without triggering constant document migrations on the disk.

Key Takeaway: By keeping related data within a single contiguous document, storage engines minimize disk seek time, enabling high-performance reads for complex, nested data structures.