[JAVA] A guide to "Something is slowing down ..." at the TCP layer

Solve something slow

It is a story that falls into the case of "something slower ..." when executed in a specific different environment compared to the development environment and other environments.

1. Turn off the Nagle algorithm

Roughly speaking, it is an optional function of TCP that sends fine-grained write data with some delay (buffer). https://ja.wikipedia.org/wiki/Nagle%E3%82%A2%E3%83%AB%E3%82%B4%E3%83%AA%E3%82%BA%E3%83%A0 `It addresses the issue of applications repeatedly sending at small particles such as 1 byte. There are 20 bytes for IPv4 and 20 bytes for TCP itself, which makes a total of 40 bytes, so a total of 41 bytes must be sent to send 1 byte, which is a large overhead. `` As described in Wikipedia, this option is often enabled by default because it may be more efficient to send fine particles in bulk to some extent due to the large overhead. The Nagle algorithm may cause unnecessary delay, such as when the application has a meaningful buffer, so disabling the option may improve it [^ 1].

If you want to disable java.net.Socket, this is → Socket.setTcpNoDelay (boolean on)

2. Do not reverse DNS lookup

When you want the connection source information on the server side, for example, when you want to output the connection source information to the log If you call java.net.InetSocketAddress.getHostName () for the connected Socket, the host name will be reverse-looked up from the IP address. If the reverse lookup of the host name from the IP address fails, it will take a few milliseconds to a few seconds for the DNS response, which will be slower. Since it can only occur in an environment where reverse lookup fails, it is difficult to notice the performance degradation in this case, but if there is an implementation that does something with the connection source information, or by grep with getHostName () etc. You may be able to identify the cause.


I think there are a few more things, but there were only two in the range I could think of ...

[^ 1]: Disabling Nagle reduces latency but can also reduce throughput, so you should consult with the characteristics of your application.

Recommended Posts

A guide to "Something is slowing down ..." at the TCP layer
A solution to the problem of blank spaces at the beginning of textarea
How to identify the path that is easy to make a mistake
A program that determines whether the entered integer is close to an integer
[Java small story] Monitor when a value is added to the List
How to change the value of a variable at a breakpoint in intelliJ