multithreading - How many threads is too many? -


I am writing a server, and when the request is coming, I share each task in thread. I do this because almost every request creates a database query. I am using a Threadpool library to reduce the construction / destruction of the thread.

My question is - what are such good cutoff points for I / O sources? I know that this would be just a rough guess, but are we talking hundreds? Thousands?


Edit:

Thank you for all your responsibilities, it seems that I'm just going to test it for finding our thread count. The question is: How do I know that I have hit that roof? Should I really measure?

Some people will say that two threads a lot - I I'm not: -)

This is my advice: Measure, do not guess. A suggestion is configurable and it is initially set to 100, then release your software in the wild and monitor what happens.

If your thread is in use 3 then 100 is too much if it stays at 100 for most days of the day, then bump it up to 200 and see what happens.

You can be Maybe actually monitor your code to use and adjust the configuration for it next time it starts but it is probably overkill.


For clarification and expansion:

I am not advocating rolling my own thread pooling subsystem, by all means Use one close but, since you were asking about a good cut-off point for thread, I think your thread has the ability to limit the maximum number of threads created in the implementation of the pool (which is good thing is).

They have written thread and database connection pooling code and they have the following features (which I believe are necessary for performance):

  • Minimum number of active threads .
  • The number of threads maximum.
  • Close the thread not used for a short time.

The first thread sets a baseline for the minimum performance in the context of the client (this number of threads is always available for use). The second prohibits resource usage by the active thread. The third lets you return to the baseline in a quiet time so that resource usage can be reduced.

The resources needed to balance the resources need to be balanced (A) work to not do enough thread to use the resource (B).

(A) Normally memory usage (stack and so on) because no thread that does not work will use much of CPU (B) Generally the processing of requests will be delayed as they arrive , Because you have to wait for the thread to be available.

This is the reason why you take measures as you say, the vast majority of your thread is waiting for a response from the database so that they will not run. There are two factors that affect how much yarn you should allow.

The number of available DB connections is first of all, it can be a difficult limit as long as you can increase it in DBMS - I am assuming that your DBMS can take unlimited connection in this case ( Although you should ideally measure it too).

Then, the number of your threads should have been dependent on your historical use. You must have the minimum running minimum number you have ever run + A%, with a minimum (for example, And it is configurable like A) 5.

Maximum number of threads should be your historical maximum of + B%.

You should also monitor for behavioral changes if, for some reason, your usage becomes 100% available for a significant time (so that it affects the performance of the customer) Maximum acceptance should be given again till B% high. In response to "What exactly should I measure"

? Question:

The maximum thread should be measured exclusively in contemporary usage under load (e.g., waiting for returns from DB calls). Add a security factor of <10> for example (emphasis is given because other posters take my recommendations as prescribed recommendations).

In addition, it should be done in production, it is okay to get estimation from the environment already for tuning, but you never know what type of production will throw your way (this is the reason that at the runtime All things should be configurable). This is to catch a situation such as the unexpected doubling of incoming client calls.


Comments