How an SSD stores data
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.
Step by step
- A hard disk can overwrite a block in place. Raw flash cannot.
- Flash must erase a whole block before writing it again (see the table).
- Each erase wears the block. Dense NAND may manage only a few thousand.
- 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.
- The controller also manages bad blocks, so the operating system does not have to.
- 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
- UBIFS — Linux kernel documentation — erase before rewrite, erase block size, block device block size, limited erase cycles, bad blocks on NAND
- Managing flash storage with Linux — Bootlin — managed flash (controller does erasing, wear leveling and bad blocks, emulated block device), swapping hot and cold blocks, vendor-specific algorithms
- fstrim(8) — Linux man page — TRIM discards unused blocks, weekly is enough for most systems, constant trimming and low-quality SSDs