Avoid NIO, Get Better Throughput

The Java NIO (new/non-blocking I/O) API introduced in Java 1.4 is arguably the most arcane part of the standard library.  With channels, selectors, byte buffers and all the associated flipping, marking, compacting, event-handling and registering/de-registering of read/write interest, it’s an entirely different level of complexity to the old-fashioned, straightforward blocking I/O.  And if you want to use SSL with NIO then it’s a whole new world of pain.

Few have mastered NIO.  For most it provides an opportunity to really get to know your debugger.  “Should this buffer be flipped before I pass it to this method, or should the method flip it?”.  BufferOverflowExceptions and memory leaks abound.

So, in the spirit of doing the simplest thing that could possibly work, writing your own NIO code is usually best avoided unless you have a compelling reason.  Fortunately, some masochistic individuals have done a lot of the hard work so that we don’t have to.  Projects such as Grizzly and QuickServer provide proven, reusable non-blocking server components.

However, in most instances, maybe non-blocking I/O is not necessary at all? In fact, maybe it is detrimental to performance?

That’s the point that Paul Tyma makes.  He attacks some of the received wisdom about the relative merits of blocking and non-blocking servers in Java.  The characteristics of JVMs and threading libraries change as new advances are made.  Good advice often becomes bad advice over time, demonstrating the importance of making your own measurements rather than falling back on superstitions.

Paul’s experiments show that higher throughput is achieved with blocking I/O, that thread-per-socket designs actually scale well, and that the costs of context-switching and synchronisation aren’t always significant.  Paul’s slides form his talk “Thousands of Threads and Blocking I/O: The Old Way to Write Java Servers Is New Again (and Way Better)” are well worth a look.

If you are writing your own multi-threaded servers in Java, Esmond Pitt’s Fundamental Java Networking and Java Concurrency in Practice by Brian Goetz et al. are essential reading.

Originally posted on New Adventures in Software

Article Type: 
Opinion/Editorial
0
Average: 1 (1 vote)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Horia Muntean replied on Thu, 2008/09/04 - 4:44am

Hello,

Don't be so eager to dismiss the advantages of a NIO server. When using blocking sockets if the clients are slow or have intermitent connections you can end up really quickly in a thread starvation situation.

Just try to close a blocking socket opened on a 'dead' tcp/ip connection. The calling thread can wait for hours before returning the control back.

Regards,

Horia

jordanz replied on Thu, 2008/09/04 - 1:16pm

I saw Tyma's talk and it was excellent. To summarize, trying to write an NIO app you end up duplicating what the kernel's thread scheduler does. More than likely (!) you won't be able to beat it on performance and scalability. Besides, the NIP socket channel APIs are terrible and difficult to use.

Milind Rao replied on Mon, 2008/09/08 - 9:44am

How about NIO memory mapped files?

Zombi G replied on Fri, 2008/09/12 - 7:00pm

Blocking IO is simpler, but simply doesn't scale. And no matter, how advanced the scheduler in your OS, I bet, it can better schedule 10 threads, instead of 1000. Once I've to rewrote a server from B-IO, to NB-IO, and after it can served twice as much connection concurrently (the actual byte/sec througput is increased to 3times than before)

jordanz replied on Sat, 2008/09/13 - 2:13am

Everyone's mileage will vary, but Tyma's point was the blocking I/O scales very well to 1000s of threads. In particular, he proved that the NIO library is 30% slower even when used for _blocking_ sockets. It's apparently poorly implemented internally.

Gramesmith1 replied on Tue, 2009/04/28 - 8:23am

So, in the spirit of doing the simplest thing that could possibly work, writing your own NIO code is usually best avoided unless you have a compelling reason. website design |design for print |best logo designs

John22 replied on Thu, 2009/05/07 - 8:18am

The characteristics of JVMs and threading libraries change as new advances are made. Good advice often becomes bad advice over time, demonstrating the importance of making your own measurements rather than falling back on superstitions. My profiles can be found here: Free dating site | russian girls dating | single russian brides

ranaldinio replied on Sat, 2009/05/16 - 2:03pm

thank you for the information and for sharing

<a href="http://www.telavenir.com">voyance telephone</a>||<a href="http://www.telavenir.com">medium</a> 

 

emad964 replied on Mon, 2009/06/29 - 4:33pm

تحميل برامج برامج جوالات العاب بنات برامج تكنولوجيا كتب تعليم UltraSurf العاب برامج نت Internet Download Manager ProgDVB برامج مجانية أفضل المواقع العربية دليل مواقع مشاهدة محطات مشفرة Online TV Player 3.0.0.940 Internet Download Manager 5.17 Build 4 رقص شرقي anyTV Pro 4.32 OnLineLive 7.1.1 هزي يانواعم ProgDVB 6.06.2 SopCast 3.0.3 منتدى برامج نت Falco Image Studio 3.6 لعبة تزلج على الجليد UltraSurf 9.4 كاثرين هيغل Katherine Heigl محطة غنوة FreeZ Online TV 1.0 Free Video to Mp3 Converter 3.1.3.51 Advanced MP3 Converter 2.10 Xilisoft Video to Audio Converter 5.1.23.0515 Blaze Media Pro 8.02 AKRAM Media Creator 1.11 DVD Audio Extractor 4.5.4 Free WMA to MP3 Converter 1.16 لعبة نينجا المتقدم لعبة قذف كرة لعبة دراجات البهلوانية لعبة اعداء الغابة تحميل برامج Download DivX Subtitles 2.0 BullGuard 8.5 Google Chrome 2.0.181.1 Dev Dell Studio XPS Desktop 435T Intel Matrix Storage Manager A00 Gigabyte GA-EP45-UD3P Bios F9 Ambush HDConvertToX 1.1.229.1764 MSI Wind Nettop CS 120 Realtek Audio Driver 5.10.0.5618 Biostar T41-A7 6.x Realtek On-Board Audio Driver 5.10.0.5735 for 2000/2003/XP TweakNow RegCleaner 4.1.1 SpeedItup Free 4.97 برامج العاب - Internet Download Manager - برامج جوالات - العاب - محطة غنوة - قنوات فضائية - بنات - تكنولوجيا - كتب تعليم - UltraSurf - ق ذ -0

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.