How an SSD works: flash, wear and TRIM
Video 046 of 140 in Virtualization Foundations, in the unit “Storage devices, buses and network cards”. 4:23, with English and Hindi audio.
A solid-state drive, or SSD, has no spinning platter and no moving head, yet it keeps data when the power is off. It uses flash memory, which follows strict rules. Those rules explain why a changed file costs extra work, why flash wears out, and why the TRIM message matters. This article covers each one, then shows three Linux commands for checking whether a disk accepts TRIM.
What you will learn
- A flash page can be written only when it is empty, but erasing works on a whole block.
- A changed file leaves a stale copy behind, and it must be cleared by a block erase.
- Each erase wears a cell a little, so the controller spreads writes across the chips.
- TRIM tells the drive which blocks are free, so later writes do not have to wait.
- Check the wear counter and TRIM support on a Linux machine before a drive fails.
How flash memory keeps data
Flash memory holds data as electric charge in tiny cells. The cells are grouped into pages, and pages are grouped into blocks. A block holds many pages.
A small processor in the drive, called the controller, decides where each piece of data goes. It also hides these details from the computer.
Flash has one strict rule. A page can be written only when it is empty. Erasing, however, works on a whole block at a time.
Why a changed file costs extra work
When a file changes, the controller writes the new data to a free page and marks the old page as stale, which means it is no longer needed.
Before a block can be reused, the controller must move the pages still in use to safety. Then it erases the whole block, which is empty again. The moving and erasing is called garbage collection, and it takes time. The exact steps differ between drive makers.
Wear: why every erase costs a little
Each cell survives only a limited number of erase cycles. After that, it can no longer hold data reliably.
To spread that damage, the controller spreads writes across all the chips. This is called wear levelling, so that no single spot wears out first.
The drive keeps a health counter that reports how much of its life is used. Read it from time to time, so you can plan a replacement before the drive fails.
Note: Busy logs and databases write all day, and they use up erase cycles faster than you might expect.
TRIM: telling the drive what is free
When you delete a file, the operating system marks its space as free. The drive does not know that. It still treats those blocks as full, so it keeps copying data that nobody uses.
TRIM is the message that passes that news. With it, the drive can erase those blocks early. Without it, a write may wait while a block is erased, and a busy drive slows down as it fills.
Check TRIM support on Linux
Three commands show whether a disk accepts TRIM, and let you run the trim job yourself. Each one is shown below.
lsblk --discardsystemctl status fstrim.timersudo fstrim -v /Note: The sudo prefix runs the command with administrator rights, so use it only on a machine you manage.
Try it yourself
You need a Linux machine with an SSD and administrator rights. You will see whether the disk accepts TRIM, and how many bytes the trim job frees.
List the disks with their discard columns. Values above zero mean the disk accepts TRIM.
lsblk --discardWhat you should see: DISC-GRAN and DISC-MAX above zero: the disk accepts TRIM.
Check the status of the trim timer that Linux provides for fstrim.
systemctl status fstrim.timerWhat you should see: The status shows whether the trim timer is active on this machine.
Run the trim job on the root filesystem now. It needs administrator rights.
sudo fstrim -v /What you should see: It reports how many bytes it trimmed on the root filesystem.
Terms
- Flash memory
- A chip that keeps data without power, by holding electric charge in tiny cells.
- Controller
- The small processor inside an SSD that decides where each write goes.
- Garbage collection
- The work of moving the valid pages out of a block, so the block can be erased and reused.
- Wear levelling
- Spreading writes across all the chips, so that no single spot wears out first.
- TRIM
- A message that tells an SSD which blocks are free, so it can erase them ahead of time.
Check yourself
Why can a small change to a file cost extra work on an SSD?
The new data goes to a free page and the old page becomes stale. Later, the controller must move the valid pages out before it can erase the block.
What does wear levelling do?
It spreads writes across all the chips, so no single spot uses up its erase cycles first.
What does TRIM tell the drive?
Which blocks are free, so it can erase them ahead of time and keep writes fast.
Previous: How a hard disk works. Next: SATA, SAS and NVMe compared.