Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number of threads for NioEventLoopGroup with persistent connections

I would like to use Java Netty to create a TCP server for a large number of persistent connections from a clients. In other words, imaging that there are 1000 client devices out there, and all of them create and maintain a persistent connection to the TCP server. There will be a reasonable amount of traffic (mostly lines of text) that go back and forth across each of these persistent connections. How can I determine the best number of threads to use in the boss and worker groups for NioEventLoopGroup?

My understanding is that when the connection is created, Netty creates a SimpleChannelInboundHandler<String> object to handle the connection. When the connection is created then the handler channelActive method is called, and every time it gets a new message from the client, the method messageReceived gets called (or channelRead0 method in Netty 4.0.24).

  1. Is my understanding correct?

  2. What happens if I have long running code to run in messageReceived - do I need to launch this code in yet another thread (java.util.Thread)?

  3. What happens if my messageReceived method blocks on something or takes a long time to complete? Does that bring Netty to a grinding halt?

Basically I need to write a TCP socket server that can serve a large number of persistent connections as quickly as possible.

  1. Is there any guidance available on number of threads for NioEventLoopGroup and on how to use any threads inside the handler?

Any help would be greatly appreciated.

like image 979
Marc Avatar asked Nov 22 '14 04:11

Marc


1 Answers

How can I determine the best number of threads to use in the boss and worker groups for NioEventLoopGroup?

  • About Boss Thread,if you are saying that you need persistent connections , there is no sense to use a lot of boss threads, because boss threads only responsible for accepting new connections. So I would use only one boss thread.
  • The number of worker threads should depends on your processor cores.

Don't forget to add -XmsYYYYM and -XmxYYYYM as your VM attributes, because without them you can face the case, when your JVM are not using all cores.

What happens if I have long running code to run in messageReceived - do I need to launch this code in yet another thread (java.util.Thread)?

  • Do you really need to do it? Probably you should think of doing your logic another way, if not then probably you should consider OIO with new thread for each connection.

What happens if my messageReceived method blocks on something or takes a long time to complete?

  • You should avoid using thread blocking actions in your handlers.

Does that bring Netty to a grinding halt?

  • Yep, it does.
like image 175
Maksym Avatar answered Nov 15 '22 07:11

Maksym