Hello everyone, I’m Fei Ge!
I recently published a very popular new book – ” Understanding Linux Networking “. In this book we discuss a lot of issues related to the kernel networking module in depth. It discusses how a network packet reaches the user process from the network card, talks about synchronous blocking and multiplexing epoll, and also discusses in detail how data is sent from the process, and a series of in-depth network working principles.
The book became the champion of JD.com’s technology sales day on the day it was first released, and it has already started its third printing just three weeks after its launch. It can be said to be very popular. If you can read this book, you will be just like Paoding. From today onwards, we will no longer see the whole Linux (the whole cow), but the internal modules (muscles) of the kernel. .
So with a deep understanding of the network, what optimization methods are available for us in terms of performance? Here I give some performance optimization suggestions in development or operation and maintenance. These suggestions are all excerpted from the book. However, it should be noted that each performance optimization method has application scenarios where it is suitable or not. You should be flexible in choosing to use it or not, depending on your current project status.
Note : There are reader benefits at the end of today’s article. Brother Fei asked you for 5 new books , “In-depth Understanding of Java Core Technology”, which will be given to you by lottery!
Recommendation 1: Minimize unnecessary network IO
The first piece of advice I would give is to use network IO as much as possible.
Yes, the Internet plays a very important role in the modern Internet world. Users request online services through the network, the server reads the data in the database through the network, and builds an extremely powerful distributed system through the network. The network is very good, which can reduce the difficulty of module development, and can also use it to build a more powerful system. But that’s not a reason for you to abuse it!
The reason is that even the native network IO overhead is still significant. First of all, to send a network packet, you must first switch from user mode to kernel mode, which costs the overhead of a system call. After entering the kernel, it has to go through a lengthy protocol stack, which will take a lot of CPU cycles, and finally enter the “driver” of the loopback device. On the receiving end, the soft interrupt takes a lot of CPU cycles and has to be processed by the receiving protocol stack, and finally wake up or notify the user process to process. When the server has finished processing, it has to send the result again. You have to do this again, and finally your process can receive the result. You say trouble is not trouble. Another problem is that multiple processes cooperate to complete a job, which will inevitably introduce more process context switching overhead. From the perspective of development, these overheads are actually useless.
What we also analyzed above is only the local network IO. If it is cross-machine, there must be a DMA copy process of the two network cards, as well as the network RTT time-consuming delay between the two ends. Therefore, although the network is good, it cannot be abused at will!
Recommendation 2: Try to merge network requests
If possible, combine multiple network requests into one, which not only saves the CPU overhead of both ends, but also reduces the time consumption caused by multiple RTTs.
We may better understand with an example in practice. If there is a…
The post 15 Optimization Tips for Linux Network Performance! first appeared on Lenix Blog .
This article is reprinted from https://blog.p2hp.com/archives/8888
This site is for inclusion only, and the copyright belongs to the original author.