At present, the servers of the storage system developed and maintained by the author all run on SSD, and the largest SSD capacity of a single server is as much as 4TB. (The company is so rich that only mechanical hard disks were used in the lab before.) However, SSD itself has a big gap with mechanical hard disks. Although SSD has many advantages in performance, if it is used in the wrong way, it will get half the result with twice the effort. So I took the time to investigate solid-state drive architecture and features, and summarized some rules to avoid SSD write magnifying performance degradation

###1.SSD write magnification

First of all, let’s take a look at what is Write amplification. Write amplification was first proposed by Intel and SiliconSystems in 2008: it shows that the data actually written on SSD is far more than that written by users.

Why is this happening? We need to go back to the nature of solid-state drives. First, SSDS differ from traditional mechanical hard drives in that they cannot overwrite. Therefore, for existing SSDS, a data write is divided into two actions:

·1. Erase the existing data on SSD.

·2. Write new data.

This is where the problem of write magnification comes in, because the minimum size of each SSD write is 4KB per Page, while the size of each erase is in blocks, which are typically 64 or 128 pages. That is, ** is precisely the unit size mismatch between SSD writes and erasures that causes write magnification. If you write only to a single Page and there are no free pages in a single Block, you need to erase one Block and then write another Block. So, with only a 4KB write on its own, the “magnification” is 64 times or even 128 times! Since the lifetime of an SSD depends on the number of erasures, write-down greatly affects the lifetime of the SSD


OK, now that we know about write magnification, what can we do next to cause write magnification?

Write quantity,

This should make a lot of sense, as you can see from the above, if each write to the SSD is a small amount, then typical write magnification will occur.

· Remaining capacity

Typically, SSDS have reserved space (OP) for the SSD master chip to perform some optimizations. Among them, the two core tasks of reserving space are garbage recovery and loss balance. Here THE author gives a brief introduction to garbage recovery and loss balance.

Garbage collection:

The dominant garbage collection algorithm in SSDS is similar to the markup clean garbage collection algorithm in Java:

  


As shown in the figure above, THE SSD first writes a-D pages in Block X, then continues to write to H and updates A ‘-d’ so that the original A-D pages become garbage that needs to be recycled. So the master chip will move the available data to the free Block Y next to it and erase the entire Block X. The smaller the remaining capacity, the more frequent the garbage collection, and the more severe the SSD write magnification. That’s why so many mobile phones are so smooth when you first buy them, but then you start using them more and more. It’s because the capacity is getting smaller and smaller. (This is part of the reason why the author’s 64GB Mi 5 has only 4 gigabytes of spare capacity, so it’s like a dog in daily use.) Frequent garbage collection scenarios make the problem of write magnification even worse.

Loss equalization:

As we know, the service life of SSD is equal to the number of erasures of Block. Currently, the programming/erasures of MLC particles used by the mainstream are generally around 3000 ~ 5000 times. If you program and erase a Block repeatedly, that Block will break before other BLCoKs, resulting in a large number of bad blocks. For this reason, the SSD master chip tries to erase each Block as evenly as possible. So a loss-balancing operation that requires moving blocks where no new data is being written also results in an increase in write magnification.

###2. Write some solutions to the magnification problem

After understanding the cause of SSD write magnification, we can “take the right medicine” to try to give some solutions to reduce the write magnification problem to our online service.

Batch write,

This is an almost universal solution to the disk IO problem, as well as traditional mechanical hard disks and SSDS. Try to reduce the number of random writes in the code logic to avoid the write magnification problem caused by a small number of write operations, and consider block alignment to further reduce the write magnification problem. (Of course, the so-called block alignment idea here is tightly coupled with the program running environment, the portability of the program will be greatly reduced.)

· Reserved space, capacity alarm

This is also the common thinking of our o&M machines. There should be a threshold for SSD usage. When our preset capacity is exceeded, the online application needs to alert the o&M and developer.

Write compression,

Write compression depends on SSD master chips. Some SSD master chips support write compression. That is to say, accept to the operating system to send to write 20m data, the main control chip can pass some stream compression or block compression algorithm compression data, when reading data, and then decompress again. This approach relies heavily on hardware, and the new bottleneck may be the compression and decompression speed of the main control chip.

TRIM command,

TRIM is an operating system-level command. ** The operating system uses the TRIM command to mark a Page on the SSD as recyclable. ** Once a Page is marked by an SSD as recyclable, the SSD master chip collects the marked Page data into the same Block when the SSD is idle, and then erases it together. Each time data needs to be written, new data can be written to a Page that is already free enough.

In practice, the TRIM command needs to be enabled through Linux Settings. Here is how to enable the TRIM command in Linux:

1. Check whether the Linux kernel is larger than 2.6.28.

  


2. Run the hdparm -i /dev/sda1 command to check whether THE SSD device supports TRIM.

  


3. Modify the /etc/fstab file and add discard to mount options. After the restart, TRIM is enabled

  


# # # 3. Summary

So far, the author has talked about the causes of SSD write enlargement, and put forward some ideas and methods to solve the causes of SSD write enlargement, hoping that you can learn from the production environment can better “tune” SSD~~~