TNonblockingServer, TThreadedServer and TThreadPoolServer, which one fits best for my case?

Alex Dong picture Alex Dong · Aug 23, 2010 · Viewed 11.7k times · Source

Our analytic server is written in c++. It basically queries underlying storage engine and returns a fairly big structured data via thrift. A typical requests will take about 0.05 to 0.6 seconds to finish depends on the request size.

I noticed that there are a few options in terms of which Thrift server we can use in the c++ code, specifically TNonblockingServer, TThreadedServer, and TThreadPoolServer. It seems like TNonblockingServer is the way to go since it can support much more concurrent requests and still using a thread pool behind the scene to crunch through the tasks. It also avoids the cost of constructing/destructing the threads.

Facebook's update on thrift: http://www.facebook.com/note.php?note_id=16787213919

Here at Facebook, we're working on a fully asynchronous client and server for C++. This server uses event-driven I/O like the current TNonblockingServer, but its interface to the application code is all based on asynchronous callbacks. This will allow us to write servers that can service thousands of simultaneous requests (each of which requires making calls to other Thrift or Memcache servers) with only a few threads.

Related posts on stackover: Large number of simulteneous connections in thrift

That being said, you won't necessarily be able to actually do work faster (handlers still execute in a thread pool), but more clients will be able to connect to you at once.

Just wondering are there any other factors I'm missing here? How shall I decide which one fits my needs the best?

Answer

Marian Klühspies picture Marian Klühspies · Feb 16, 2015

One guy on Github has made a nice comparison

TThreadedServer

TThreadedServer spawns a new thread for each client connection, and each thread remains alive until the client connection is closed. This means that if there are 1000 concurrent client connections, TThreadedServer needs to run 1000 threads simultaneously.

TNonblockingServer

TNonblockingServer has one thread dedicated for network I/O. The same thread can also process requests, or you can create a separate pool of worker threads for request processing. The server can handle many concurrent connections with a small number of threads since it doesn’t need to spawn a new thread for each connection.

TThreadPoolServer (not benchmarked here)

TThreadPoolServer is similar to TThreadedServer; each client connection gets its own dedicated server thread. It’s different from TThreadedServer in 2 ways:

Server thread goes back to the thread pool after client closes the connection for reuse. There is a limit on the number of threads. The thread pool won’t grow beyond the limit. Client hangs if there is no more thread available in the thread pool. It’s much more difficult to use compared to the other 2 servers.