简体   繁体   中英

Java TCP/IP Socket write performance optimization

Server Environment

Linux/RedHat
6 cores
Java 7/8

About application :

We are working on developing a low latency (7-8 ms) high speed trading platform using Java. Multi-leg orders are sent after algo conditions are met

Problem

The orders to the exchange using TCP/IP java.net.Socket APIs (using java.io.OutputStream.write(bytes[] arg0) ). Profiler measurement is records as 5-7 microsec which is very high as per our low latency reqs. We are not made use of setPerformancePreferences() api as suggested in one of the questions posted in stacktrace.

Question

  1. Any alternatives to java.net.Socket to reduce the socket transmission time?
  2. Any optimization techniques to improve performance
  3. Is setPerformancePreferences() is of any use?

We are not made use of setPerformancePreferences() api

It doesn't do anything and never has. I wouldn't worry about it.

Any alternatives to java.net.Socket to reduce the socket transmission time?

The problem is most certainly not a software one. You can get <8 micro-seconds from Java to Java on different machines, but you need low latency network cards like Solarflare or Mellanox.

If you want fast processing you should consider either a high GHz haswell processor, possibly over clocked to 4.2 or 4.5 GHz or a dual socket Haswell Xeon. The cost of these compared to the cost of trading is not high.

Any optimization techniques to improve performance

Using non-blocking NIO ie ByteBuffers and busy waiting on the socket connections. (I wouldn't use Selectors as they add quite a bit of overhead) I would turn off nagle.

For some micro-tuning, use an affinity bound thread on an isolated cpu.

Is setPerformancePreferences() is of any use?

Looking at the source .. I will let you be the judge.

public void setPerformancePreferences(int connectionTime,
                                      int latency,
                                      int bandwidth)
{
    /* Not implemented yet */
}

Java 7/8

In term of which version to use, I would start with Java 8 as it has much improved escape analysis which can reduce garbage of short lived objects and thus help reduce latency between GCs and jitter from GCs.

A couple of things come to mind:

JNI : JNI lets you write C code that is ran from your Java code. Critical parts of your Java code that are running to slow can be migrated to C/C++ for improved performance. Work would be needed to first identify what those critical points are and if its worth the effort to move it to C/C++.

Java Unsafe : Wanna get dangerous? Use Java Unsafe to bypass that pesky GC. Here is more info on it. On Github you may find some cool wrapper code to more-safely use Java Unsafe. Here is one. More info.

LMAX Disruptor : Read more about it here . This company is also building a fast trading system in Java. Disruptor allows for faster inter-thread communication.

Bytecode scrutinization: Review your code by looking at the byte code. I have done this for a video game I made and was able to streamline the code. You'll need a good tool for turning your class files into readable bytecode. THis might be the tool i used.

Improved garbage collection: Have you tried using the G1 garbage collector ? Or messing around with the older GC's ?

Highscalability : This site is full of good info on making code fast. Here is an example that might help.

New API I dont know exactly how to use New API, but it has come up in articles I have read. Here is another article on it. You might need to use it via JNI.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM