views:

87

answers:

3

I am writing an application server (again, non-related with a question I already posted here) and I am wondering what are the strategies to use when creating worker threads that work on the database. Some preliminary dates: the server receives xml and sends back xml, all the requests query a database - each request could take a few milliseconds to a few seconds.

Say for example that your server services a small to medium number of clients which in turn send a small number of requests per connection. Is it safe to have one worker thread per connection or should it be per request? Also should a thread pool be used to limit the resources used by the server or a worker should be added each time a new connection/request is made?

Should the server limit the number of threads it creates to an upper limit?

Hope I am not too vague ... I can hardly keep my eyes open.

A: 

The way I write apps like this is to make the number of threads configurable via the command line and/or a configuration file. I then do some load testing with different numbers of threads - there is always an optimal number beyond which performance begins to degrade.

anon
+1  A: 

If you don't have extensive experience writing application servers is a daunting task. It can be eased by using frameworks like ACE that allow you to build different configurations of your app serving infrastructure like thread per connection, thread pools, leader follower and then load the appropriate configuration with an extensible service framework.

I would recommend to read these books on ACE to get

to get an idea about what the framework can do for you.

lothar
A: 

If you follow the model adopted by Java EE app server developers, there's a queue for incoming requests and a pool of worker threads to service them. It's one thread per request. When a worker thread fulfills a request it goes back into the pool. If the incoming requests show up faster than the worker thread pool can service them, the queue allows them to stack up until a worker thread is released. Both the queue size and the thread pool can be tuned to match for your situation.

I'd wonder why anyone would feel the need to write their own server from scratch, especially when the scenario you describe is solved so well by others. If your wish is education, good luck. If you think you're going to improve on what's been done in the past, I'd re-examine that assumption.

duffymo
Not all of us think that what has been done in the past is the acme of human achievement.
anon
Obviously, but it has been done. Sometimes the fastest code to write is the stuff that you just use from someone else. It's not clear at all to me how a green field solution will improve on what's been done in the past, especially if the level of understanding of past practice is so low as to require a question like this.
duffymo
its not about you - its about the user. If you can write a server that performs better, your users benefit even if it takes you longer to write it. Sometimes a server optimised for your particular scenario is better than the generic versions someone else wrote.
gbjbaanb
Fair enough, gbjbaanb. Perhaps the questioner will benchmark their server against others and report back to see how well they did.
duffymo