473,721 Members | 4,027 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

The .NET blocking problem, thread pools, and other interesting stuff

Hi,

I'm developing an application that will support several thousand
simultaneous connections on the server-side. I'm trying to maximize
throughput. The client (WinForms) and server communicate via a socket
connection (no remoting, no ASP.NET). The client sends a message to the
server that contains some instructions and the server responds in an
asynchronous fashion. In other words, the client doesn't block while waiting
for the request. The request might be processed on the server in a split
second or it might take a few minutes. In any case, the client doesn't wait
around for the response. It happily goes about servicing the user and when
the server gets around to responding, the client makes that response data
available to the user.

Now let's look at what's happening on the server. The server basically
receives a message from the client on an I/O thread-pool thread and does as
much as it can to process that request. Sometimes the server logic realizes
that it must talk to another server to process the request. In that case,
the thread-pool thread on the server does NOT block. It simply sends it's
request asynchronously to another server and then the thread returns to the
pool. When the secondary server does its thing, it'll notify the main server
via an existing socket connection, an I/O thread from the .NET thread pool
is assigned the task of reading this information, and this thread eventually
sends the results back to the client. Again, my desire is to eliminate
thread-blocking on the server whenever possible since thread-pool threads
are a limited resource (25/CPU, I believe). Make sense so far? ok...

...but now I'm in a bit of a pickle [not literally, that's just an
expression]. I'm now required to have the server respond to a certain type
of client request that requires that a SQL Server database be contacted. The
request might call for a SELECT statement or INSERT/UPDATE/DELETE or for a
stored proc to be executed. So I thought "no problem" -- I'll just use
ADO.NET. The problem though is that ADO.NET is synchronous and "blocking" by
nature. There is no "BeginFill( )" for example to asynchronously fill a
DataSet. What are the implications of this? Well, for starters, this means
that whenever my server is waiting for a reply from ADO.NET, the thread-pool
thread that is making the call just blocks. This might not sound like such a
big deal but on a two CPU machine that has 50 threads in the pool, I could
easily encounter a situation where all 50 threads are blocked waiting for
ADO.NET responses -- which means that there are no threads left to process
incoming requests. Even *simple* requests that don't require database access
must sit grudgingly in the queue because all of the thread-pool threads are
sitting idly in a blocked state waiting for ADO.NET calls to return! Not
good!

Now you might say, "Listen, brother...Just use delegates and
BeginInvoke() to simulate asynchronous behavior for your database calls."
This won't work because of course BeginInvoke() simply will place the call
in the thread pool queue and then make the call synchronously. You might
say, "My friend... Use the .NET API command that increases the number of
threads in the pool." This doesn't solve the problem though and it's hackish
and it implies that I know better than MSFT how many threads should be in
the pool [which I don't].

What other approach might I take? Perhaps I should create a bunch of
threads manually and use these for database access? It's ugly, but at least
I'm not wasting my valuable thread pool threads which are used for
processing of incoming requests.

I'm going to give MSFT the benefit of the doubt that they made ADO.NET a
synchronous blocking animal for good reason... and they're right, I suppose,
because 99% of the time it makes sense to make ADO.NET calls in a
synchronous fashion. But in my case, hopefully you'll see my plight and have
a clever workaround for me!

Peace be with you!

David



Nov 15 '05 #1
7 2866
David Sworder <ds******@cts.c om> wrote:

<snip>
What other approach might I take? Perhaps I should create a bunch of
threads manually and use these for database access? It's ugly, but at least
I'm not wasting my valuable thread pool threads which are used for
processing of incoming requests.


I think this is probably exactly the approach to take. The threadpool
job items are meant to be quick things, not long-running work. If you
create a second threadpool, only serviced by a few threads, you should
be able to load work to be done onto that pool, and it will get
processed "sooner or later" - and in the meantime, you've got your
*normal* threadpool ready to receive requests and perform any quick
operations.

Just MHO, of course.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
> I think this is probably exactly the approach to take. The threadpool
job items are meant to be quick things, not long-running work. If you
create a second threadpool, only serviced by a few threads, you should
be able to load work to be done onto that pool, and it will get
processed "sooner or later" - and in the meantime, you've got your
*normal* threadpool ready to receive requests and perform any quick
operations.


Thanks, Jon... and I'm sure you can anticipate my next question: How
many threads in this mini-pool?

I'm thinking that since .NET seems to set the default maximum SQL
connection pool size to 50, it probably wouldn't make sense to exceed 50
threads. The idea of having 50 extra threads hanging around kind of bums me
out. Maybe I should take the same approach that the thread pool takes: If
one of my DB threads isn't used for say 2 minutes, let that thread die a
painless death. If my server process goes for several minutes without
processing any requests that require database access, these threads will all
die. If I need more threads later, I can create them -- up to a max of 50.
What do you think?

David
Nov 15 '05 #3
David Sworder <ds******@cts.c om> wrote:
I think this is probably exactly the approach to take. The threadpool
job items are meant to be quick things, not long-running work. If you
create a second threadpool, only serviced by a few threads, you should
be able to load work to be done onto that pool, and it will get
processed "sooner or later" - and in the meantime, you've got your
*normal* threadpool ready to receive requests and perform any quick
operations.

Thanks, Jon... and I'm sure you can anticipate my next question: How
many threads in this mini-pool?


I would stick with "not many at all" - like maybe 5.
I'm thinking that since .NET seems to set the default maximum SQL
connection pool size to 50, it probably wouldn't make sense to exceed 50
threads. The idea of having 50 extra threads hanging around kind of bums me
out. Maybe I should take the same approach that the thread pool takes: If
one of my DB threads isn't used for say 2 minutes, let that thread die a
painless death. If my server process goes for several minutes without
processing any requests that require database access, these threads will all
die. If I need more threads later, I can create them -- up to a max of 50.
What do you think?


Yes, your threadpool should definitely have more configuration etc
available than the built-in one. I'd suggest ramping up threads slowly
(so that if there's a long stream of fairly short-lived jobs, they all
end up being done by the same single thread) and ramping them down
slowly too (having one die per minute, say). I wouldn't go up as far as
50 threads though - if you've got 50 concurrent database requests going
at the same time, that's unlikely to help throughput in the long run.

Don't forget that you have much more knowledge of how your threadpool
is going to be used than .NET has of its own one - because you're the
only one who's going to be putting jobs on it. I suspect the thread
limit in the .NET threadpool is significantly higher than it needs to
be for most uses, but it's high for robustness. Just a guess though!

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
> Yes, your threadpool should definitely have more configuration etc
available than the built-in one. I'd suggest ramping up threads slowly
(so that if there's a long stream of fairly short-lived jobs, they all
end up being done by the same single thread) and ramping them down
slowly too (having one die per minute, say). I wouldn't go up as far as
50 threads though - if you've got 50 concurrent database requests going
at the same time, that's unlikely to help throughput in the long run.


Thanks for the comments, Jon.

On a somewhat related issue, what do you recommend as an optimum size
for a *connection* pool? In my last message, I stated that the default max
size of a connection pool in .NET is 50. I was wrong. It is actually 100!
What is Microsoft trying to tell us by setting this value so high, and how
does this reconcile with your suggestion of having no more than 5 threads in
my personal thread-pool for database access?

Regarding MSFT's magic number of 100, are they saying:
a) SQL Server can't effectively handle more than 100 simultaneous
connections.

or...

b) A *client* of SQL Server shouldn't have more than 100
simultaneous connections open because assuming that each connection runs on
it's own thread, the context-switching penalty outweighs the benefits of
concurrency.

or...

c) If a middle tier machine needs to make more than 100 simultaneous
connections to SQL Server, it's time to scale out and get a second middle
tier machine to handle the load.

or

d) something else entirely?

Don't get me wrong -- I don't foresee a situation where 100+
simultaneous database connections would be necessary. I'm just trying to
understand the logic MSFT is using. If I understand their way of thinking, I
can better decide for myself how many simultaneous DB connections I should
be allowing.

David



Nov 15 '05 #5
David Sworder <ds******@cts.c om> wrote:
On a somewhat related issue, what do you recommend as an optimum size
for a *connection* pool? In my last message, I stated that the default max
size of a connection pool in .NET is 50. I was wrong. It is actually 100!
What is Microsoft trying to tell us by setting this value so high, and how
does this reconcile with your suggestion of having no more than 5 threads in
my personal thread-pool for database access?
Hmm... no idea! I suppose it means you can keep connections open to
multiple databases, even if you're not going to use them all at once.
Regarding MSFT's magic number of 100, are they saying:


<snip>

I really wouldn't like to second-guess them very much on this. I don't
have a lot of experience in this area, and it's the kind of topic where
you really need people who know what they're talking about.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6

Hi David,

Thanks for posting in this group.
Yet no single approach performs better in all scenarios, there is no
substitute for performance testing using realistic scenarios
There is no determine number for optimum of connections. It depends on many
factors, the best way is testing the suitable number in your environment.

Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "David Sworder" <ds******@cts.c om>
| References: <Oo************ **@TK2MSFTNGP09 .phx.gbl>
<MP************ ************@ms news.microsoft. com>
<#P************ **@TK2MSFTNGP12 .phx.gbl>
<MP************ ************@ms news.microsoft. com>
| Subject: Re: The .NET blocking problem, thread pools, and other
interesting stuff
| Date: Tue, 21 Oct 2003 14:54:48 -0700
| Lines: 55
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <en************ *@TK2MSFTNGP11. phx.gbl>
| Newsgroups:
microsoft.publi c.dotnet.framew ork.adonet,micr osoft.public.do tnet.languages. c
sharp
| NNTP-Posting-Host: rrcs-west-66-27-51-213.biz.rr.com 66.27.51.213
| Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP11.phx.g bl
| Xref: cpmsftngxa06.ph x.gbl
microsoft.publi c.dotnet.langua ges.csharp:1930 10
microsoft.publi c.dotnet.framew ork.adonet:6421 5
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
|
| > Yes, your threadpool should definitely have more configuration etc
| > available than the built-in one. I'd suggest ramping up threads slowly
| > (so that if there's a long stream of fairly short-lived jobs, they all
| > end up being done by the same single thread) and ramping them down
| > slowly too (having one die per minute, say). I wouldn't go up as far as
| > 50 threads though - if you've got 50 concurrent database requests going
| > at the same time, that's unlikely to help throughput in the long run.
| >
|
| Thanks for the comments, Jon.
|
| On a somewhat related issue, what do you recommend as an optimum size
| for a *connection* pool? In my last message, I stated that the default max
| size of a connection pool in .NET is 50. I was wrong. It is actually 100!
| What is Microsoft trying to tell us by setting this value so high, and how
| does this reconcile with your suggestion of having no more than 5 threads
in
| my personal thread-pool for database access?
|
| Regarding MSFT's magic number of 100, are they saying:
| a) SQL Server can't effectively handle more than 100 simultaneous
| connections.
|
| or...
|
| b) A *client* of SQL Server shouldn't have more than 100
| simultaneous connections open because assuming that each connection runs
on
| it's own thread, the context-switching penalty outweighs the benefits of
| concurrency.
|
| or...
|
| c) If a middle tier machine needs to make more than 100
simultaneous
| connections to SQL Server, it's time to scale out and get a second middle
| tier machine to handle the load.
|
| or
|
| d) something else entirely?
|
| Don't get me wrong -- I don't foresee a situation where 100+
| simultaneous database connections would be necessary. I'm just trying to
| understand the logic MSFT is using. If I understand their way of
thinking, I
| can better decide for myself how many simultaneous DB connections I should
| be allowing.
|
| David
|
|
|
|
|
|
|
|
|
|

Nov 15 '05 #7

There are many articles in MSDN talk about the performance issue of Sql
server, you can get more information from them.
The article below talks about the performance compasion of different Data
Assess techniques.
http://msdn.microsoft.com/library/de...us/dnbda/html/
bdadotnetarch03 1.asp

Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| X-Tomcat-ID: 284955570
| References: <Oo************ **@TK2MSFTNGP09 .phx.gbl>
<MP************ ************@ms news.microsoft. com>
<#P************ **@TK2MSFTNGP12 .phx.gbl>
<MP************ ************@ms news.microsoft. com>
<en************ *@TK2MSFTNGP11. phx.gbl>
| MIME-Version: 1.0
| Content-Type: text/plain
| Content-Transfer-Encoding: 7bit
| From: v-*****@online.mi crosoft.com ("Jeffrey Tan[MSFT]")
| Organization: Microsoft
| Date: Wed, 22 Oct 2003 10:24:03 GMT
| Subject: Re: The .NET blocking problem, thread pools, and other
interesting stuff
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
| Message-ID: <EW************ **@cpmsftngxa06 .phx.gbl>
| Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| Lines: 93
| Path: cpmsftngxa06.ph x.gbl
| Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1931 26
| NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122
|
|
| Hi David,
|
| Thanks for posting in this group.
| Yet no single approach performs better in all scenarios, there is no
| substitute for performance testing using realistic scenarios
| There is no determine number for optimum of connections. It depends on
many
| factors, the best way is testing the suitable number in your environment.
|
| Hope this helps,
|
| Best regards,
| Jeffrey Tan
| Microsoft Online Partner Support
| Get Secure! - www.microsoft.com/security
| This posting is provided "as is" with no warranties and confers no rights.
|
| --------------------
| | From: "David Sworder" <ds******@cts.c om>
| | References: <Oo************ **@TK2MSFTNGP09 .phx.gbl>
| <MP************ ************@ms news.microsoft. com>
| <#P************ **@TK2MSFTNGP12 .phx.gbl>
| <MP************ ************@ms news.microsoft. com>
| | Subject: Re: The .NET blocking problem, thread pools, and other
| interesting stuff
| | Date: Tue, 21 Oct 2003 14:54:48 -0700
| | Lines: 55
| | X-Priority: 3
| | X-MSMail-Priority: Normal
| | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| | Message-ID: <en************ *@TK2MSFTNGP11. phx.gbl>
| | Newsgroups:
|
microsoft.publi c.dotnet.framew ork.adonet,micr osoft.public.do tnet.languages. c
| sharp
| | NNTP-Posting-Host: rrcs-west-66-27-51-213.biz.rr.com 66.27.51.213
| | Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP11.phx.g bl
| | Xref: cpmsftngxa06.ph x.gbl
| microsoft.publi c.dotnet.langua ges.csharp:1930 10
| microsoft.publi c.dotnet.framew ork.adonet:6421 5
| | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
| |
| | > Yes, your threadpool should definitely have more configuration etc
| | > available than the built-in one. I'd suggest ramping up threads slowly
| | > (so that if there's a long stream of fairly short-lived jobs, they all
| | > end up being done by the same single thread) and ramping them down
| | > slowly too (having one die per minute, say). I wouldn't go up as far
as
| | > 50 threads though - if you've got 50 concurrent database requests
going
| | > at the same time, that's unlikely to help throughput in the long run.
| | >
| |
| | Thanks for the comments, Jon.
| |
| | On a somewhat related issue, what do you recommend as an optimum
size
| | for a *connection* pool? In my last message, I stated that the default
max
| | size of a connection pool in .NET is 50. I was wrong. It is actually
100!
| | What is Microsoft trying to tell us by setting this value so high, and
how
| | does this reconcile with your suggestion of having no more than 5
threads
| in
| | my personal thread-pool for database access?
| |
| | Regarding MSFT's magic number of 100, are they saying:
| | a) SQL Server can't effectively handle more than 100
simultaneous
| | connections.
| |
| | or...
| |
| | b) A *client* of SQL Server shouldn't have more than 100
| | simultaneous connections open because assuming that each connection
runs
| on
| | it's own thread, the context-switching penalty outweighs the benefits of
| | concurrency.
| |
| | or...
| |
| | c) If a middle tier machine needs to make more than 100
| simultaneous
| | connections to SQL Server, it's time to scale out and get a second
middle
| | tier machine to handle the load.
| |
| | or
| |
| | d) something else entirely?
| |
| | Don't get me wrong -- I don't foresee a situation where 100+
| | simultaneous database connections would be necessary. I'm just trying to
| | understand the logic MSFT is using. If I understand their way of
| thinking, I
| | can better decide for myself how many simultaneous DB connections I
should
| | be allowing.
| |
| | David
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|

Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1470
by: Jacob Friis | last post by:
I have built a script inspired by a post on Speno's Pythonic Avocado: http://www.pycs.net/users/0000231/weblog/2004/01/04.html#P10 I'm setting NUM_FEEDERS to 1000. Is that crazy? Are there a better solution? Thanks, Jacob
1
3557
by: Daylor | last post by:
hi. i have main thread , and 2 others thread (t1 ,t2 for example) how can i call from the main thread, to tell t1 to exit his thread ? (calling application.exitthread from main thread, in method of class created on t1 ,will call exitthread to the main thread) what is the simple way to do that ?
1
1505
by: Hal | last post by:
SQL2K, sp3 Active Server Pages front-end I am having a blocking problem with a spid executing a stored procedure that does no updates. When I look at the locks the blocking spid has, some of them have lock mode "IS". Looking in BOL, I see "S" is shared, but I don't see "IS". Can anyone tell me what lock mode "IS" is? And how could a stored procedure that does no updates be guilty of
3
2220
by: David Sworder | last post by:
This message was already cross-posted to C# and ADO.NET, but I forgot to post to this "general" group... sorry about that. It just occured to me after my first post that the "general" group readers might have some thoughts on this perplexing .NET blocking issue. (see below) ===== Hi,
11
1407
by: Ravi Ambros Wallau | last post by:
Dear friends: This is my third question on this news in three days... Sorry for this spam behavior :-) I've a lot of problems on "first page load" after some change on web.config or even in the contents of bin folder... Nathan Sokalski passed me a link to ILMerge library, I've made some tests grouping a set of DLL's that are loaded without reflection, but there were no improvements on load time. I've noted that ASP.NET copies all...
6
5479
by: Joe HM | last post by:
Hello - I have a function that calls Thread.Abort() to stop a thread in a _Closed() Method of a GUI. The thread contains a blocking call on a TCP socket and that is the easiest way to stop that. This thread is also outputting strings in a RichTextBox and in some rare instances I get a System.NullReferenceException when I exit the GUI. It seems like the _Closed() Method calls Thread.Abort() and then continues closing down/disposing...
1
2227
by: richard.hallgren | last post by:
Hi, I have some questions concerning thread pools. I've marked the questions by number in the text. First I'd like to know when which thread pools an application uses. I know ASP.NET uses the CLR common thread pool. (1a) Is there a way around this? (1b) Can ASP.NET application have it's own pool? Is there a setting in IIS for this? (2) If ASP:NET application uses the CLR common pool is this also true for Web Services running on the...
2
2183
by: giddy | last post by:
hi , I'm in a slight dilemma. I have an updater program that checks for pending updates and downloads them if the users chooses to. Since i dont need a windows form to check if there is an update , I dont use a form unless i have two. This , creates either one of two problems. When i'm downloading asynchonously , the main thread exits!
7
1050
by: azrael | last post by:
Hy Guys Did anyone manage to install and use Pivy. I'm trying it and cant come closer to the goal I get the message: Please set the COIN3DDIR environment variable to your Coin root directory! ** Aborting ** Familiar to anyone? And there is anoher question in my mind.
0
8851
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8736
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9373
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9227
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9143
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9077
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6676
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3202
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2588
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.