Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One thread per client. Doable?

I'm writing a Java server which uses plain sockets to accept connections from clients. I'm using the fairly simple model where each connection has its own thread reading from it in blocking mode. Pseudo code:

handshake();

while(!closed) {
  length = readHeader(); // this usually blocks a few seconds
  readMessage(length);
}

cleanup();

(Threads are created from an Executors.newCachedThreadPool() so there shouldn't be any significant overhead in starting them)

I know this is a bit of a naive setup and it wouldn't really scale well to many connections if the threads were dedicated OS threads. However, I've heard that multiple threads in Java can share one hardware thread. Is that true?

Knowing that I'll be using the Hotspot VM on Linux, on a server with 8 cores and 12GB of RAM, do you think this setup will work well for thousands of connections? If not, what are the alternatives?

like image 451
Bart van Heukelom Avatar asked Oct 05 '10 19:10

Bart van Heukelom


People also ask

How many requests can a thread handle?

No, it can't. One thread will work on one request until it's done. A thread that's working can never be paused and then given another task.

What is thread per request?

Thread-per-request: this model handles each request from a client in a separate thread of control. This model is useful for servers that handle long-duration requests (such as database queries) from multiple clients.

What is thread per request architecture?

Thread-Per-Request: With this architecture, the CORBA server ensures that each incoming message is processed in its own thread. This means that multiple requests will be processed concurrently. There are concurrency issues.

Do threads have their own stack?

Threads are sometimes called lightweight processes because they have their own stack but can access shared data. Because threads share the same address space as the process and other threads within the process, the operational cost of communication between the threads is low, which is an advantage.


2 Answers

This will scale well for up to hundreds of connections, not to thousands. One issue is that a Java thread takes quite a bit of stack as well (e.g. 256K), and the OS will have problems scheduling all your threads.

Look at Java NIO or framworks that will help you get started doing complex stuff more easily (e.g. Apache Mina)

like image 82
ivy Avatar answered Oct 17 '22 06:10

ivy


It is possible this will scale to thousands of clients. But how many thousands is the next question.

A common alternative is to use Selectors and non-blocking I/O found in the java.nio package.

Eventually you get into the question of whether it's useful to set up your server in a clustered configuration, balancing the load over multiple physical machines.

like image 36
Mark Peters Avatar answered Oct 17 '22 07:10

Mark Peters