Home » Java » We need advice for a server software implementation with Java NIO

We need advice for a server software implementation with Java NIO

Posted by: admin December 28, 2021 Leave a comment

Questions:

I’m trying to calculate the load on a server I have to build.

I need to create a server witch have one million users registered in an SQL database. During a week each user will approximately connect 3-4 times. Each time a user will up and download 1-30 MB data, and it will take maybe 1-2 minutes.

When an upload is complete it will be deleted within minutes.
(Update text removed error in calculations)

I know how to make and query an SQL database but what to consider in this situation?

Answers:

What you want exactly is Netty. It’s an API written in NIO and provides another event driven model instead of the classic thread model.
It doesn’t use a thread per request, but it put the requests in a queue. With this tool you can make up to 250,000 requests per second.

###

I am using Netty for a similar scenario. It is just working!

Here is a starting point for using netty:

public class TCPListener {
    private static ServerBootstrap bootstrap;

    public static void run(){
        bootstrap = new ServerBootstrap(
                new NioServerSocketChannelFactory(
                        Executors.newCachedThreadPool(),
                        Executors.newCachedThreadPool()));

        bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
            public ChannelPipeline getPipeline() throws Exception {
                TCPListnerHandler handler = new MyHandler();
                ChannelPipeline pipeline = Channels.pipeline();
                pipeline.addLast("handler", handler);

                return pipeline;
            }
        });

        bootstrap.bind(new InetSocketAddress(9999));  //port number is 9999
    }

    public static void main(String[] args) throws Exception {
        run();
    }
}

and MyHandler class:

public class MyHandler extends SimpleChannelUpstreamHandler {
    @Override
    public void messageReceived(
        ChannelHandlerContext ctx, MessageEvent e) {


        try {
            String remoteAddress = e.getRemoteAddress().toString();
            ChannelBuffer buffer= (ChannelBuffer) e.getMessage();
            //Now the buffer contains byte stream from client.

        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }

        byte[] output; //suppose output is a filled byte array
        ChannelBuffer writebuffer = ChannelBuffers.buffer(output.length);
        for (int i = 0; i < output.length; i++) {
            writebuffer.writeByte(output[i]);
        }


        e.getChannel().write(writebuffer);
    }

    @Override
    public void exceptionCaught(
            ChannelHandlerContext ctx, ExceptionEvent e) {
        // Close the connection when an exception is raised.
        e.getChannel().close();
    }
}

###

At first I was thinking this many
users would require a non-blocking
solution but my calculations show that
I dont, [am I] right?

On modern operating systems and hardware, thread-per-connection is faster than non-blocking I/O, at least unless the number of connections reaches truely extreme levels. However, for writing the data to disk, NIO (channels and buffers) may help, because it can use DMA and avoid copy operations.

But overall, I also think network bandwidth and storage are your main concerns in this application.

###

The important thing to remember is that most users do not access a system evenly in every hour of every day of the week. Your system need to perform correctly during the busiest hour of the week.

Say the busiest hour of the week, 1/50 of all uploads are made. In the busiest hour each upload could be 30 MB, a total of 1.8 TB. This means you need to have an Internet upload bandwidth to support this. 1.8 TB/hour * 8 bits/byte / 60 min/hour / 60 sec/min = 4 Gbit/s Internet connection.

If for example, you have only a 1 Gbit/s connection, this will limit access to your server.

The other thing to consider is your retention time for these uploads. If each upload is 15 MB on average, you will be getting 157 TB per week or 8.2 PB (8200 TB) per year. You may need a significant amount of storage to retain this.

Once you have spend a significant amount of money on Internet connectivity and disk, the cost of buying a couple of servers is minor. You could use Apache MIMA, however a single server with a 10 Gbit/s connection can support 1 GB easily using any software you care to chose.

A single PC/server/labtop can handle 1,000 I/O threads so 300-600 is not a lot.

The problem will not be in the software but in the network/hardware you chose.