Java – Is it better to listen on multiple ports than listening on single port

javamultithreadingsockets

In my server, I have 1000 threads that are listening to 1000 different UDP sockets. When a thread receives a packet it puts the UDP packets on a thread-safe queue. The code is like this.

   while(true)
    { 
        ......
        socket.receive(packet); 
        packetQueue.add(packet);
        ..... 

    }

In a second on average a thread (UDP socket) receives 50 packets that are on average the server receives

1000 * 50 = 50K

UDP packets. The server is working more or less fine.

But now I want to change the design a little now. Now I am planning to use 1 thread that will listen on 1 socket and after receiving those packets that thread will put that packet in the queue like before.

So now on average 50K UDP packets will come to the server on that port.

Will this new architecture be able to handle this load?
From Java networking perspective does it make any difference if all the packets are coming to one port or different ports?

Best Answer

It should handle it, depends of your implementation.

One thread is enough to push it to the queue. The problem is the size of the queue and how fast you are able to take data from it. Maximum size of the UDP package in IPv4 is 65,507 bytes, multiply it by 50 000 and you get more than 3gb per second. You need to be sure that packages are not lost if queue is full, unless you don't mind

Use Netty it will help you out with this. There is a similar question on stackoverflow

Related Topic