Search code examples
javanettynio

Multi-reactor Design - am I trying to reinvent Netty?


I am trying to migrate an existing NIO Server code-base with following characteristics - Single thread which performs accepts of connections under control of a Selector - Multiple threads which performs Socket READ/WRITE (one Thread: one Selector: one SocketChannel of accepted connection)

I want to migrate to a model wherein Single READ/WRITE thread can have one Selector and multiple accepted SocketChannels could register and unregister. This way, a single thread can multiplex multiple SocketChannels for IO.

I understand that this model would straight-away map to a case of Netty where there is one boss thread and configurable no of workers and multiple connections per worker. ExecutionHandler is orthogonal so I am not bringing that into picture for now.

Specific Question: Am I reinventing "Netty" wheel or is there any difference in above approach for a server which is expected to be latency-sensitive where connections could go upto 5000 and binary protocol message-exchange rates are expected to reach 60-70K msgs/sec ?

I understand that numbers mentioned about would not be influenced by the said design choice alone but other factors too. But a larger influence could be made by design choice - at least that's what I believe

Thanks in advance


Solution

  • I think you are re-inventing. Netty does exactly this and more. But since netty is now well modularized, you could even pick up individual maven sub-modules of netty instead of the whole thing if you dont want the rest of the functionality or "baggage".
    The basic problem with writing from scratch is that you will probably run into a ton of issues that netty has already resolved, platform issues, jdk work arounds and so on.
    For the record, I was able to easily get the message rate mentioned above(actually more) for a server I have written with netty, but for fewer number of clients(100) when testing on my 4 year old centrino processor laptop.
    Probably you should take an example from netty examples module, write your 5000 connection client and test out if latency works out for you before you go down this path. Should take you just couple of hours to do that.