Egor Lakomkin
Egor Lakomkin

Reputation: 1434

How to read efficiently from socket using Java NIO

I am working on task involving reading from the socket trading quotes and I need to achieve minimum latency and high throughput.

I started with the simpliest possible java nio prototype like this

ByteBuffer buf = ByteBuffer.allocateDirect(BUFFER_SIZE);
try {
       buf.clear();
       int numBytesRead = socketChannel.read(buf);

       if (numBytesRead == -1) {  
           socketChannel.close();
       } else {
           buf.flip();
           byte[] byteArrived = new byte[buf.remaining];
           buf.get(byteArrived,0,byteArrived.length);
           // here we send byteArrived to the parser
       }
   } catch (IOException e) {    
}

I guess it is lame to create byte[] array every time, but due to the lack of knowledge I dont know how to parse ByteBuffer ( because I need to unmarshall byte protocol into messages and pass them into business logic). Can you recommend how to avoid mass garbage creation?

Also I would like to ask about best practices how to organize socket reading with low latency and high throughput? I read about LMAX and disruptor framework and they achieved 6M transactions on the single thread.

Upvotes: 2

Views: 5174

Answers (2)

user207421
user207421

Reputation: 310850

Assuming you can adapt your parser API to accept (byte[] buffer, int offset, int length) as arguments, you can just pass (bb.array(), 0, bb.limit()) as parameters and not have to create the new byte[] at all per read. However this isn't likely to be the rate-determining step.

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533442

You can achieve higher than that with Disruptor and other methods. A lot depends on the size and complexity of message (as well as what you do with the message !!)

If you want to serialize/deserialze with ByteBuffer, use the putXxxx and getXxxx methods. To make this process easier, I suggest putting the length of each message first so you can check you have a full message before attempting to parse it.

You might find this presentation interesting http://vanillajava.blogspot.com/2011/11/low-latency-slides.html

Upvotes: 4

Related Questions