How to curb write amplification in SSD?

I once wrote that LSI, in addition to SAS controllers, produces chips for modern hard drives and SSDs, as well as the fact that flash memory requires a more complex approach to I / O operations. What this complexity consists of, and what kind of problem our chips solve, I want to tell in this article. It's about Write amplification. Of course, professionals are already familiar with WA, so the article is designed more for beginners.
Modern flash memory microcircuits are designed so that to achieve increased performance, they need to read, write and erase data in large blocks. Moreover, the recording unit in size is not less than the reading unit, and the erasing unit is always larger than the recording unit. This necessitates combining memory cells in a hierarchical structure, usually: blocks - sectors - pages.
When we have a virgin-clean new SSD in our hands, there are no problems. All its cells are filled with zeros and are ready to receive our data. Therefore, the first record on the SSD does not present any problems at all. Typically, data is written in blocks of 4-8 KB, and this happens really quickly.
Now let's say we need to change a couple of bytes (for drama, you can even assume that one single bit, although this is rarely possible in life) in an already recorded file. Unlike traditional hard drives, flash memory does not just allow you to take and overwrite a data block; you must erase it first. And since the size erasure unit is much larger than the recording unit, this operation will be very inefficient. Thank you, Wikipedia, for a graphic illustration:

To do this, we need to read the contents of the entire erase block somewhere, then modify the data in it, then erase the block and write the data to it again. This leads to the appearance of such a negative phenomenon as recording amplification. In fact, much more data is written to the disk than the computer really “wanted” to write.
A very simple formula allows you to calculate the recording gain.
Record Gain Factor = Data recorded on flash memory / Data sent for recording by the host
This phenomenon has been known since the first chips with NAND memory appeared, but the term Write Amplification was coined in 2008 by Intel and SiliconeSystems.
Recording amplification has two distinct disadvantages. The first of them is a drop in speed, since reading-erasing-writing large blocks is clearly not as effective as direct writing, but this drawback is simply bypassed.
The flash controller takes on the job of translating the logical addresses that the computer operates into the physical addresses of the data on the media, in our case, SSD, this is called Logical Block Addressing (the familiar abbreviation LBA).
Modern controllers optimize the rewrite operation as follows. If he needs to change one of the file blocks, then instead of completely overwriting it with reading, deleting and writing, the SSD controller saves the modified block in another place that was previously erased, marks the old block “available but not ready” and changes the information to the metadata that it uses for LBA.

At the first stage, this seems like a reasonable idea, but as the disk becomes full, we get more and more blocks in the “wrong” state, that is, marked as free, but not erased. Therefore, modern hard drives use different garbage collection algorithms that erase such blocks and return them to the “available and ready” lists. Of course, the less free disk space, the more intensively you have to use these algorithms, which in turn determines the dependence of the SSD performance on filling.
The second drawback of Write Amplification is the acceleration of SSD wear. We all know that memory cells have a limit on the number of valid rewrite operations. For SLC disks, it is higher, for MLC lower, but it is. To combat this, data redundancy is used and it is checked using different checksums (I wrote about LDPC in the previous article ), but the free space redundancy of memory chips is a limited resource, and recording amplification contributes to its faster exhaustion.
What are the factors and how do they affect Write Amplification?
Garbage collection
The fact that the wear of SSD cells will depend on the performance of GC algorithms is undeniable. Garbage collection on SSDs can occur both background and explicit.
In the case of explicit (foreground) garbage collection, cleaning and optimization of freed blocks occurs when recording new ones. In the case of a background build, the controller uses idle periods to optimize free space and clean blocks.
Background cleaning allows you to speed up the work due to the fact that the blocks are freed up by the time they are already needed, but this leads to the fact that often the controller has to optimize really unnecessary data, which probably can even be deleted in the future.
The presence of both advantages and disadvantages leads to the fact that developers are trying to combine these methods, achieving better performance. For example, in an OCZ SSD, the background garbage collector clears a small number of blocks and stops, which minimizes the number of unnecessary operations, while still providing the disk with online access to free blocks.

However, the most promising area now is garbage collection at the same time as host-initiated write operations. This allows you to achieve high performance in environments with a large number of write operations when SSDs are not idle. Among the controllers with this capability are SandForce controllers from LSI.

Of the interesting exotic, it is worth noting the attempts of some manufacturers, primarily Samsung, to develop a garbage collection system that would use the information of the file system located on the disk in the work. This would allow the controller to efficiently use information about recently deleted files and unallocated space. According to the developers, this approach allowed working effectively in those systems that do not support the TRIM team. Controllers with this feature required that the disk must be partitioned in NTFS and contained MBR. This technology was very unreliable, and often led to data loss, especially when using other file systems. Another area in which SSD developers are moving is Application Hinting. The easiest way to explain this is with the example of databases. Data in the DBMS is also placed in pages for their simplified search. Therefore, the DBMS also has the concept of garbage, or Dirty Page. If the DBMS could send lists of dirty pages to solid state devices in advance, this would greatly help in the garbage collection process. There are optimization examples for other applications.
# end of the first part #