How an SSD stores data

· 3 min read

#how-it-works#hardware

An SSD stores data in flash memory, and flash cannot overwrite a block in place the way a hard disk can. The idea to keep: a controller inside the drive hides that rule and spreads the wear, so the operating system sees an ordinary disk.

The short version

Flash must erase a whole erase block before it can be written again, and erase blocks are large. Each erase wears the block a little, and some blocks go bad. Bootlin's overview of managed flash says the controller deals with erasing, wear leveling and bad blocks, while the operating system sees an emulated block device. How exactly a drive does this is usually the vendor's secret.

How an SSD hides flash rules from the operating system Three boxes in a row: Operating system, Controller and Flash memory. Arrow 1 goes from the operating system to the controller as a write. Arrow 2 goes from the controller to the flash memory, which must erase a block before writing it again. A dashed arrow 3 goes from the operating system to the controller to tell it which blocks are unused, called TRIM. Operating system sees a block device Controller hides the flash rules Flash memory erase, then write 1 write 2 erase 3 TRIM: unused blocks
Simplified: real controllers differ, and their algorithms are often the vendor's secret.

Step by step

  1. A hard disk can overwrite a block in place. Raw flash cannot.
  2. Flash must erase a whole block before writing it again (see the table).
  3. Each erase wears the block. Dense NAND may manage only a few thousand.
  4. Wear leveling spreads erases so no block dies far ahead of the rest, for example by swapping a heavily written block with a rarely used one.
  5. The controller also manages bad blocks, so the operating system does not have to.
  6. TRIM lets the file system tell the drive which blocks are unused.
Hard disk block Flash erase block
Rewrite overwritten in place erased first
Typical size 512 bytes about 128 KiB (example)
Bad blocks usually hidden by hardware must be handled by software or the controller

Why it matters

The upside is that the operating system needs no special handling for flash. The downside is that every erase uses up some of a block's life and the wear logic is hidden. Running fstrim weekly is enough for most desktops and servers, but trimming constantly can shorten the life of low-quality SSDs.

Sources