CSE Roadmap

Core tier · about 2 hours · 6 parts

Part 5 of 6 · 3 min read

Storage locality, and its cost

Storing a whole record as one continuous block makes reading it fast, but makes reading or updating a small part of it wasteful.

Storage locality, and its cost

Think of a single stapled packet holding every page of one customer's order. It is quick to hand over as a whole. Compare that to a thin single page. Making one small correction to it still means pulling apart and restapling the entire packet.

In plain words: a document is typically stored as one continuous block. Reading an entire order, with all its line items, takes one lookup instead of a join across several tables. That is a real performance advantage when an application usually needs the whole record at once. The same continuity becomes a cost in two cases. Reading only a small part of a large document still typically requires loading the whole thing. And most document stores rewrite the entire document on any update, even a change to one field, rather than modifying just that field in place.

A worked example. Rendering a single order confirmation page needs the order, the customer's name, and all line items together. This benefits from storage locality: one document read returns everything needed. Changing only the quantity field on one line item, inside a very large order document, works differently. The entire document is typically rewritten, line items, totals and all, even though only one small value actually changed.

The key lines. No new SQL; this part is a storage-layout property of how a document database writes data to disk, not something expressed in a query.

Storage locality

Storage locality is the performance advantage of storing all of a record's data together as one continuous block. Reading the whole record then requires only one lookup, instead of a join across several separately stored pieces. Its cost is that a partial read, or a small update, typically still requires loading, or rewriting, the entire block.

Example: loading one order document in a single read to render a confirmation page is storage locality helping; rewriting that entire document just to update one line item's quantity is its cost.

Remember this

A document database trades the relational model's join-anywhere flexibility for storage locality and a natural fit to self-contained, one-to-many records. It pays for that trade in two ways: weak support for many-to-many relationships, and a cost on partial reads or small updates to a large document. The next page covers key-value stores, which strip the document model down even further.

Quick notes

  • The object-relational mismatch is the gap between in-memory objects and a relational schema that spreads the same data across tables; the document model closes that gap for naturally nested data.
  • A one-to-many relationship nests directly inside a document; a many-to-many relationship forces either duplication or an application-level lookup.
  • Schema-on-read enforces no fixed shape at write time, leaving structure to the reading code; schema-on-write enforces a declared shape on every row before it is stored.
  • Storage locality lets a whole document be read in one lookup, but typically costs a full rewrite on even a small update.
  • Document models fit self-contained, one-to-many data well, and fit highly interconnected, many-to-many data poorly.