Skip to content
Tutorials Center
My learning

Topics

Lesson 48 of 67 ยท Virtual Memory and Storage

Disks, SSDs and RAID

Compute hard-disk access time from seek and rotation, see why SSDs need garbage collection and wear management, and rebuild a failed disk with RAID 5 parity.

Below main memory sits secondary storage: devices that keep data when the power is off and hold far more than RAM, at a price in speed. For decades that meant the spinning hard disk. Today most computers also, or only, use solid-state drives (SSDs) built from flash memory. Their physics is very different, so their performance, their failure modes and the tricks that make them dependable are different too.

This lesson covers three things: how long a hard disk takes to deliver data (and why random access is so painful), what makes an SSD fast and why it needs a controller that is almost a small computer, and how RAID combines several drives to gain speed, reliability or both. You will compute access times, simulate flash garbage collection, and rebuild a dead disk from parity. For the storage technologies as devices, see also RAM, ROM, SRAM, DRAM and Flash.

Anatomy of a hard disk

A hard disk drive stores bits as magnetic patterns on one or more rotating platters. Each platter surface has a read/write head mounted on an arm that moves across the surface.

  • A track is one circle on a surface.
  • A sector is the smallest addressable piece of a track (traditionally 512 bytes, now often 4,096).
  • A cylinder is the set of tracks at the same arm position across all surfaces.

To read a sector, the drive has to do three things in sequence, and each takes time:

  1. Seek time: move the arm to the right track. This is mechanical and takes several milliseconds (average around 4 to 10 ms).
  2. Rotational latency: wait for the target sector to rotate under the head. On average that is half a revolution. At 7,200 rpm one revolution takes 60,000 / 7,200 = 8.33 ms, so the average wait is 4.17 ms.
  3. Transfer time: read the data as it passes under the head, at the media rate (over 100 MB/s for a modern disk).
flowchart LR
  R["Request: read sector S"] --> SK["Seek<br/>move arm to track<br/>3-10 ms"]
  SK --> RL["Rotational latency<br/>wait for sector<br/>avg half a turn"]
  RL --> TR["Transfer<br/>data under the head<br/>microseconds to ms"]
  TR --> D["Data returned"]

The first two steps are mechanical delays that do not depend on how much data you want.

The consequence is the single most important fact about disks: seek plus rotation costs about the same whether you read 4 KB or 1 MB, so small random reads waste almost all the time. The calculation below uses typical numbers for a desktop disk and a fast server disk, with a transfer rate of 150 MB/s.

Python Random access time on a hard disk

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A random 4 KB read on the desktop disk takes 13.19 ms and delivers just 0.30 MB/s, although the same disk streams at 150 MB/s when reading sequentially. Reading 1 MB per request raises the useful rate to about 50 MB/s, because the fixed seek and rotation costs are spread over more bytes. The SSD figure (0.1 ms, about 39 MB/s per outstanding 4 KB request, and far higher when many requests run in parallel) is two orders of magnitude better for random access.

Operating systems fight the mechanics in two ways. They schedule requests to cut arm movement (the elevator algorithm serves requests in the order the arm passes them, instead of first come, first served), and they group small writes into large sequential ones. File systems also try to lay out related data contiguously.

Solid-state drives

An SSD stores bits in NAND flash cells and has no moving parts. Reads take tens of microseconds, and many chips work in parallel, which is why SSDs also deliver large sequential throughput. But flash has two rules that shape the whole design:

  1. Data is read and programmed in pages (typically 4 to 16 KB).
  2. A programmed page cannot be overwritten in place. Memory must first be erased, and erasing works only on a whole block of many pages (hundreds of pages, a few MB).

On top of that, each cell wears out: after a few thousand to tens of thousands of erase cycles (depending on how many bits per cell) it can no longer hold data reliably.

The controller hides these rules behind a flash translation layer (FTL). When the host overwrites logical page 7, the FTL does not erase anything. It writes the new data to a fresh, already-erased page elsewhere, updates its mapping table, and marks the old copy as stale. Over time blocks fill with a mixture of live and stale pages. When free space runs low, garbage collection picks a block with few live pages, copies those live pages elsewhere, and erases the whole block. The extra copying is write amplification: the flash receives more writes than the host asked for.

flowchart TD
  H["Host overwrites logical page"] --> W["FTL writes to a free page<br/>and updates the mapping"]
  W --> S["Old copy marked stale"]
  S --> L{"Free pages low?"}
  L -- "no" --> D["Done"]
  L -- "yes" --> G["Garbage collect:<br/>copy live pages out of a mostly-stale block"]
  G --> E["Erase that block<br/>(one erase cycle used)"]
  E --> D

Overwrites are redirected; erasing happens later, a whole block at a time.

How bad is the amplification? It depends on how much spare space the drive has. The toy SSD below has blocks of 4 pages, a host that sees 12 logical pages, and random overwrites. We vary the number of physical blocks, hence the spare capacity (over-provisioning).

Python Garbage collection, write amplification and wear

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With only 25 percent spare space, the drive writes 1.75 flash pages for every host page, so each logical write consumes 75 percent more of the flash's endurance, and it performs 2,183 erases in total. With 62 percent spare, garbage collection nearly always finds a block that is entirely stale and the amplification falls to 1.01. This is why SSDs keep a hidden reserve, and why a drive that is almost full gets slower and wears faster.

The numbers also show uneven wear: in the 4-block run the busiest block was erased 621 times and the quietest 451. Real controllers add wear leveling, which deliberately moves rarely changed ("cold") data onto heavily worn blocks so that erase counts stay balanced. Other SSD features include the TRIM command (the OS tells the drive which logical pages are free so garbage collection need not copy them), error-correcting codes on every page, and a small DRAM or SRAM cache for the mapping table.

RAID: combining disks

A single drive limits you in three ways: capacity, speed, and the fact that when it dies your data is gone. RAID (redundant array of independent disks) combines several drives to improve some of these. The main levels:

Level Idea Usable capacity (N disks) Survives
RAID 0 Striping: spread chunks across all disks N No failure (one loss destroys all data)
RAID 1 Mirroring: every write goes to two disks N / 2 One disk per mirror pair
RAID 5 Striping with distributed parity N - 1 Any single disk
RAID 6 Striping with two parity blocks N - 2 Any two disks

RAID 0 multiplies throughput (reads and writes use all disks at once) but multiplies the failure risk too. RAID 1 doubles read capacity and is simple, at a 50 percent capacity cost. RAID 5 is the economical middle: it stores for each stripe one parity chunk that is the bitwise XOR of the data chunks. Because XOR undoes itself, the missing chunk of any stripe is the XOR of all the chunks that survive. The parity is spread over all disks (it rotates from stripe to stripe) so that no single disk becomes a bottleneck.

flowchart LR
  subgraph S0["Stripe 0"]
    A0["Disk 0<br/>D0"] --- A1["Disk 1<br/>D1"] --- A2["Disk 2<br/>D2"] --- A3["Disk 3<br/>P0 = D0 xor D1 xor D2"]
  end
  subgraph S1["Stripe 1"]
    B0["Disk 0<br/>D3"] --- B1["Disk 1<br/>D4"] --- B2["Disk 2<br/>P1"] --- B3["Disk 3<br/>D5"]
  end

In RAID 5 the parity chunk moves to a different disk in each stripe.

The program below writes a sentence into a 4-disk RAID 5 array with 4-byte chunks, prints the layout, then "kills" disk 2 and reconstructs it from the other three.

Python RAID 5 parity and recovery

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Disk 2 held data in stripes 0, 2 and 3 and parity in stripe 1, yet the XOR of the other three disks reproduces every chunk exactly, and the recovered text matches the original. That is the power of parity: one extra disk protects a whole array against any single failure.

The last lines show why a small RAID 5 write is expensive. Changing one data chunk would need the other chunks to recompute parity, but the identity new parity = old parity xor old data xor new data avoids reading them. Even so, the array performs four I/Os for one small write (read old data, read old parity, write new data, write new parity). That "write penalty" is why databases with heavy random writes often prefer RAID 10 (striped mirrors).

RAID has limits. It protects against disk failure, not against deleting a file, a corrupted write, or a fire, so it is not a backup. During a rebuild the surviving disks are read in full and a second failure (or an unreadable sector) can lose the array, which is why large arrays use RAID 6 or other double-parity schemes.

Parity here is the same XOR idea you met in Parity, Checksums and Error-Correcting Codes. A single parity bit can detect one wrong bit; RAID parity can correct an entire missing disk because we know exactly which disk failed.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A disk spins at 6,000 rpm. What is its average rotational latency?

    One revolution takes 60,000 / 6,000 = 10 ms. On average the wait is half a revolution, 5 ms.
  2. 2. Why does reading 4 KB from random places on a hard disk give such low throughput?

    Each request pays several milliseconds of mechanical delay but only microseconds of transfer, so useful bytes per second collapse.
  3. 3. Why can flash memory not simply overwrite a page in place?

    NAND flash is programmed per page but erased per block. The FTL therefore writes new data to a fresh page and cleans up old copies later.
  4. 4. What is write amplification in an SSD?

    When garbage collection copies live pages out of a block to erase it, the drive performs extra internal writes. The ratio of flash writes to host writes is the amplification.
  5. 5. In a 5-disk RAID 5 array of 2 TB drives, what is the usable capacity?

    RAID 5 stores one disk's worth of parity (spread across all disks), so usable capacity is (N - 1) x 2 TB = 8 TB.

Summary

  • A hard disk access costs seek time, rotational latency (half a revolution on average) and transfer time; the first two make small random reads very slow.
  • SSDs have no moving parts but must program pages and erase whole blocks; the flash translation layer redirects writes and garbage-collects stale pages.
  • Write amplification grows when spare space is small; wear leveling, TRIM and over-provisioning keep drives fast and durable.
  • RAID 0 stripes for speed, RAID 1 mirrors for safety, RAID 5 stripes with rotating XOR parity and survives one disk failure, RAID 6 survives two.
  • A missing RAID 5 chunk is the XOR of the surviving chunks of its stripe; small writes cost four I/Os.
  • RAID is redundancy for availability, not a replacement for backups.

Continue with I/O Organization: Polling, Interrupts and DMA.