Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing Java server to handle multiple simultaneous clients

I need some advice in building a Java server that handles multiple clients at the same time. The clients need to remain connected for fairly long periods of time. I'm currently using blocking IO and spawning a thread to read from each client that connects to the server, but this is obviously not scalable.

I've found a few options, including using Selector or Executor with fixed size thread pools. I am not too familiar with either one, so which would be the best solution here? Thanks!

like image 713
meteoritepanama Avatar asked Dec 09 '25 23:12

meteoritepanama


1 Answers

It depends on your definition of scalable. The system you have described with a single thread per connection is scalable up to hundreds may be even a couple of thousand concurrent connections, it will hit a wall at some point.

Your question says that your clients connect and stay connected for an extended period of time, it would be possible to have a single IO thread to handle the reading and writing, but have the processing of the request dispatched to another thread using an Executor.

There are frameworks/servers that are already written to handle this sort of event driven design. Have a look at:

  • Netty recently used by twitter in there query server
  • Jetty (not to be confused with Netty) capable of NIO and very scalable, might be to HTTP focused
  • MINA
  • Grizzly

It's worth noting that the world is full of failed startups & software products that had really scalable architecture. Scaling is a nice problem to have, better to have the problem than not to have it and no customers.

like image 155
Gareth Davis Avatar answered Dec 11 '25 14:12

Gareth Davis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!