Overview

9 Introducing the External-Memory Model

This chapter reframes algorithm analysis for data that lives beyond RAM by introducing the external-memory (Disk-Access) model. The central idea is that data transfer between disk and main memory dominates cost, while computation on data already in RAM is comparatively negligible. Data moves in blocks of size B, main memory has capacity M, and datasets of size N far exceed M. Because I/O operations are expensive and sequential access is far faster than random access (with reads often cheaper than writes), the chapter advocates measuring efficiency in number of block transfers, using Big-Oh bounds parameterized by N, M, and B. It also emphasizes that storage can mean HDDs, SSDs, or cloud systems—collectively termed “disk”—and that optimal on-disk data structures often look very different from their in-RAM counterparts.

Through concrete examples, the chapter builds intuition for I/O-efficient algorithm design. A linear scan to find a minimum costs Θ(N/B) I/Os, illustrating how a single pass over contiguous blocks is “linear time” in this model. Adapting binary search to disk yields roughly log2(N) comparisons but only about log2(N/B) block reads, since the last log2 B comparisons occur within the same block. The chapter then shows how reorganizing data to exploit each block fully leads to optimal searching via high-branching nodes (a B-tree view), reducing search to Θ(log_B N) I/Os—a dramatic improvement when B is large. It also presents K-way merging of sorted files: with one input block per list and an output buffer, a heap drives selection while the I/O cost remains Θ(N/B), revealing that external memory can merge many lists in a single linear pass when memory can buffer them.

Finally, the chapter discusses model fidelity and practicalities. Although the two-level RAM–disk abstraction hides real hierarchies (caches, multiple disks) and ignores important effects like sequential prefetching and random-access penalties, it remains a powerful guide; parameters M and B can be tuned to the level where data overflows. The model also allows parallelism across multiple disks and treats record sizes uniformly to keep analysis tractable. With this foundation—counting I/Os instead of CPU steps—the book proceeds to data structures and techniques optimized for external memory, notably B-trees (and their variants) for querying, and later write-optimized structures such as buffer trees and LSM trees.

: The external-memory model. This model is suitable for analyzing massive-data applications, where the cost of computation in RAM is subsumed by a much larger cost of transferring data from disk to main memory and back. Computation is not exactly free ---- but it is so much cheaper than that cost of transferring data that in many applications, it is effectively free.
: Finding a minimum in external memory. There is a total of 11 elements that occupy 4 blocks of size 3. The last block is not full. In this example, the beginning of the file is block-aligned.
: The difference in the way bounds look for in-RAM algorithm (left), and an equivalent on-disk algorithm (right.) It is important not to directly compare them as they represent different units. The comparison in the way the bounds look is there to understand how algorithms change as the consequence of different design.
: Binary search in external memory. The algorithm accesses a separate block for each pivot accessed, except from the last few pivots that all reside in the same block.
: How to transform a sorted array into a structure that enables optimal searching in external memory (a B-tree.) An optimal searching data structure in RAM look something like 9.5(d), where each node has one pivot based on which it is decided how to continue down the tree. The optimal searching data structure in external memory has large nodes (size of a block) that have many elements, because we pull data into memory block by block.
: Another difference in the way bounds can look in the RAM model and the external-memory model. Often, the runtime with a logarithm base 2 can turn into logarithm base B, as we can examine B different elements simultaneously with the help of block. Base 2 does not always turn into base B, as we will see later.
: K-way merging of sorted files. Each file has one block of data allotted in main memory, and through that block, each file sends its remaining minimum elements into the heap. Minima is repeatedly extracted from the heap and sent to the output. This scheme works independently on the total accumulated file size, as long as .
: Difference in the bounds between merging many sorted lists in main memory and in external memory. In external memory, we can merge many sorted lists with just one sweep over the input data. In internal memory, merging more than a constant number of lists results in more than linear cost in number of comparisons. This same internal-memory cost remains in our external-memory -way merging algorithm, it is just not the most important cost.

 Summary

  • Many data-intensive applications maintain large amounts of data on disk. Large files and large databases residing on disk need a different set of data structural and algorithmic tricks to function efficiently
  • The external-memory model is a useful tool in analyzing algorithms for large datasets that can not fit into main memory. This model assumes that all data initially resides on disk of infinite size, and in order to perform computation, chunks of data are brought to and from the main memory of a limited size.
  • The external-memory model foregoes the computation cost of an algorithm in order to emphasize the cost of data transfers that tend to be up to 1000x more expensive than computation operations in RAM.
  • When scanning sequential data, external-memory algorithm will tend to have an “” part, suggesting that we packed items into consecutive blocks
  • Unlike in internal memory, binary search is not the optimal searching algorithm in external memory, as it does not make a very good use of block inputs and outputs. A better runtime can be achieved by repackaging blocks and building a data structure called -tree.
  • One major advantage of having a large main memory is that we can simultaneously merge a large number of sorted list/files, in just one linear sweep, independently of how large the total file sizes are. This process is also a base for the optimal external-memory sorting algorithm we study later.

FAQ

What is the external-memory (Disk-Access) model and why use it?The external-memory (DAM) model abstracts a machine with a very large external storage (“disk”) and a smaller main memory of size M. Data moves between them in blocks of size B, and the cost of an algorithm is the number of block transfers (I/Os). Computation on data already in RAM is considered free compared to I/O. This model is used to design and analyze algorithms for massive datasets where data movement dominates runtime.
What do N, M, and B represent, and which quantities matter most?N is the total number of records in the dataset on disk, M is the number of records that fit in main memory, and B is the number of records per disk block. The most informative quantities are ratios: N/B (how many blocks the data occupies) and M/B (how many blocks fit in RAM). Pick a consistent “record” unit so these ratios are meaningful.
What does “disk” mean here—does the model cover SSDs and cloud storage?“Disk” is a shorthand for any storage tier much larger and slower than RAM, including HDDs, SSDs, and remote/cloud storage. While SSDs are faster than HDDs, they are still slow enough relative to RAM that I/O dominates many workloads, so the model’s guidance remains useful.
How is running time measured in the external-memory model?Complexity is expressed in Big-Oh as the number of I/Os. Unlike in RAM algorithms, M and B are kept as parameters in bounds rather than being treated as hidden constants. For example, a linear scan over N records costs Θ(N/B) I/Os.
Why is sequential access preferable to random access, and does the model capture that?Sequential access leverages high bandwidth and avoids repeated latency penalties; random access suffers high latency per block. The basic DAM model charges 1 I/O per block regardless of locality, so it does not distinguish sequential from random patterns. In practice, locality matters a lot (and OS prefetching/buffering amplifies that), so algorithm designs should prefer sequential scans when possible.
What is the I/O cost of scanning to find a minimum in a file?Reading the file block by block and tracking the minimum requires scanning all blocks that contain the N records, which is Θ(N/B) I/Os. Only one block buffer and a few variables are needed in RAM.
How do you adapt binary search to disk, and what I/O bound does it achieve?Read the block containing the current mid, compare x with the block’s boundary elements to decide which half to keep, and continue until the remaining range fits in one block, then finish in RAM. This costs Θ(log(N/B)) I/Os, because the last log B comparisons happen on a single in-memory block.
Why are B-trees superior to binary search for on-disk lookups?Binary search uses one or two pivots per I/O, underutilizing each block. B-trees pack many pivots into each node (about B records’ worth), giving a branching factor proportional to B. This reduces the height to Θ(log_B N) and thus query I/O to Θ(log_B N), often just a handful of block reads even for huge datasets.
How do you merge K sorted files efficiently in external memory?Allocate one input block per file (and an output buffer) if K ≲ M/B. Repeatedly pick the smallest current element (e.g., via a min-heap over the K front elements), refill an input buffer when it empties, and flush the output buffer when full. The I/O cost is Θ(N/B) total transfers; CPU work is O(N log K), but in I/O-bound settings the transfer cost dominates.
What are the main limitations of the external-memory model?The model assumes only two levels (RAM and disk), ignores differences between sequential and random access, treats computation as free, and assumes infinite disk. In practice, machines have multiple memory levels, OS caching/prefetching, and sometimes multiple disks (parallel I/O). Despite these simplifications, the model reliably predicts bottlenecks and guides designs that minimize I/Os.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Algorithms and Data Structures for Massive Datasets ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Algorithms and Data Structures for Massive Datasets ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Algorithms and Data Structures for Massive Datasets ebook for free