2

I am new to Java NIO.I have a small doubt. If I use the NIO instead of a socket client, should the server also be using NIO or does it not matter?

I am concerned about application scalability. I am looking at around 500-1000 client requests per seconds per server. Since I would send my data to atleast three different servers, I am ideally looking at around 1500 client requests per second. For this, I already have a socket pool implementation in place which does a fairly decent job.

What I have is a pool of socket connections for each server.Each thread picks up an available socket connection from the pool and sends it to the server

I am trying to find out if NIO can help or is better in any way than a socket client. How about blocking ? Normal client would either block or timeout.

3
  • 1
    Isn't NIO more beneficial in a server than in a client? I don't see why it's a requirement in the client and not in the server? Commented Nov 24, 2011 at 12:14
  • 1
    @jgauffin I am only bothered about the client.The server is a third party application to which I send data through TCP. I only intend to maximise the performance of my client. I am considering my options. So is it a good idea to use NIO as client vs. a normal socket client ? Commented Nov 24, 2011 at 12:18
  • 2
    imho you won't get any more performance out of your client by using NIO. The bottleneck will be either the server or the network. Also, NIO is more complex and it's likely that you'll get something wrong that will hurt performance. Keep it simple and get your application done. Commented Nov 24, 2011 at 12:26

2 Answers 2

4

It doesn't matter. The TCP stream will be unaffected by your choice of NIO.

Sign up to request clarification or add additional context in comments.

1 Comment

+1: In fact your client or server can be C or PHP or some platform you have never heard of. The same applies for NIO2 in Java 7.
2

imho you won't get any more performance out of your client by using NIO. The bottleneck will be either the server or the network. Also, NIO is more complex and it's likely that you'll get something wrong that will hurt performance.

I would start by doing a socket implementation to get it up and running more quickly. If you follow some common patterns/principles like Single Responsibility Principle it would be easy to switch implementation later on (if you or your users have proved that your applications performance is the bottleneck which I find highly unlikely).

Update

What NIO or any other asynchronous framework does it to let multiple operations share the same threads. Having one thread per connection when dealing with a lot of connections is a waste of resources since all threads will not be active all the time.

Using NIO for a client will not give you any benefits if you have just a few connections. Having a thread+socket per connection doesn't consume that much resources and it will be easier to handle the connection.

5 Comments

When you say Using NIO for a client will not give you any benefits if you have just a few connections. Then under what scenario will NIO be useful ? In my implementation,I would have, say 50 socket connections in a pool, used/reused by 500 threads every second. SO I do not have a connection per thread model.
Having 500 threads? What are those threads doing? Are you using linux/osx or windows?
I use Windows server 2008 64 bit. To explain in simple terms, my application acts like a bridge. It gets 500 web service requests per second. A thread is created for each web service request with the task of transporting the request to a destination through tcp. If I have to send it to 3 destinations, then 3 threads will be created per request.
I would not use 500 threads in a windows machine. Thread context switching is not as cheap in windows as in linux. Using NIO to receive the web requests would be a lot more efficient.
@jgauffin That's a dubious assertion. NIO moves the scheduling into your code, specifically into the select() call and the ensuing loop over the selected-key set. Your application becomes the scheduler. And with NIO you have zero possibility of concurrency; with threads you get it free.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.