Throttling in WCF

When your server is hosted out on the “big bad internet”, you need a way to make sure that you don’t get flooded with client requests. In WCF, our services support throttling as a way of mitigating potential DoS (denial of service) attacks. These throttles can also help you smooth load on your server and help enforce resource allocations. There are three service-level throttles that are controlled by ServiceThrottlingBehavior. These are in addition to any transport-specific throttles imposed by your binding. To fully understand the impact of these throttles you should also understand the threading/instancing characteristics of your service.

  1. MaxConcurrentCalls bounds the total number of simultaneous calls that we will process (default == 16). This is the only normalized throttle we have across all of the outstanding reads that the ServiceModel Dispatcher will perform on any channels it accepts. Each call corresponds to a Message received from the top of the server-side channel stack. If you set this high then you are saying that you have the resources to handle that many calls simultaneously. In practice how many calls will come in also depends on your ConcurrencyMode and InstancingMode.
  2. MaxConcurrentSessions bounds the total number of sessionful channels that we will accept (default == 10). When we hit this throttle then new channels will not be accepted/opened. Note that this throttle is effectively disabled for non-sessionful channels (such as default BasicHttpBinding).

    With TCP and Pipes, we don’t ack the preamble until channel.Open() time. So if you see clients timing out waiting for a “preamble response”, then it’s possible that the target server has reached this throttle. By default your clients will wait a full minute (our default SendTimeout), and then time out with a busy server. Your stack will look something like:

    TestFailed System.TimeoutException: The open operation did not complete within the allotted timeout of 00:01:00. The time allotted to this operation may have been a portion of a longer timeout.
    […]
    at System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.SendPreamble(IConnection connection, ArraySegment`1 preamble, TimeoutHelper& timeoutHelper)

    If instead you are timing out under channel.Send (rather than channel.Open), then it’s possible that you are hitting the MaxConcurrentCalls throttle (which kicks in per-message, not per-channel).

  3. MaxConcurrentInstances bounds the total number of instances created. This throttle provides added protection in the case that you have an instance lifetime that is not tied to a call or a session (in which case it would already be bounded by the other two throttles). Orcas durable services are one such scenario.

Net-net: if you are testing your services under load, and your clients start timing out, take a look at your throttling and instancing values. On the flip side, do not just blindly set these to int.MaxValue without fully understanding the potential DoS consequences.

17 thoughts on “Throttling in WCF

  1. Pingback: De standaard waardes van ServiceHost zijn laag! - Jowen

  2. Alex Black

    Hi Kenny, is there a way to set a timeout on an operation?

    I’ve got a WCF service, and I’ve set MaxConcurrentCalls to 4. If any individual call takes mroe than 15 seconds, I’d like WCF to kill it. The time I am interested in is once the call has started, and its processing on the server, how long it takes. I’m not interested in how long the client waited for the connection, or how long transmitting the datat took.

    Is there a feature in WCF like that?

    thanks!

    – ALex

    Reply
  3. Scott

    Kenny,

    I am seeing a weird behavior related to throttling and sessions that I was hoping you could explain to me. I am using the netTcpBinding, with the InstanceContextMode=Single and the ConcurrencyMode=Multiple. I hit the service with 20 simultaneous threads expecting to hit the default MaxConcurrentCalls throttle of 16, but instead I am hitting the MaxConcurrentSessions throttle of 10. Why is that when I am not using PerSession for my InstanceContextMode?

    Thanks for the help,
    -Scott

    Reply
  4. Kenny

    Alex, we don’t have a built-in feature to WCF like what you are asking for. You could start a timer within your service method though.

    Scott, the reason you are hitting MaxConcurrentSessions is because each TCP channel is a session, and the MaxConcurrentSessions throttle applies to the channels being accepted. It’s independent of InstanceContextMode.

    Reply
  5. Pingback: Streaming large content with WCF and deferred execution - Pablo M. Cibraro (aka Cibrax)

  6. Victor

    Hi,
    We are developping a service based application with WCF and experiencing the “MaxConcurrentSessions” issue
    The problem is that in our case calls are asynchronous and the code is generated by the WCF Proxy Generator, any idea about how to safely close the proxy?
    Thanks

    Victor

    Reply
  7. Pingback: WCF, IIS, C# | hilpers

  8. Pingback: Streaming large content from a WCF RESTFul service - Pablo M. Cibraro (aka Cibrax)

  9. Frank Xu Lei

    Hi?
    I have one question for WCF ?Does WCF use or Supports IOCP in Data Communication?as we know,IOCP supplies a solution to improve pefermance of the Data communicatioin for a lot of cocurrent clients.There is the same problem
    for WCF like too many cocurrent clients.So I like to know more detail about IOCP and WCF.Does WCF use or Supports IOCP?Is there any offical docs or aticles for this question?
    I have posted this questioin on WCF forum at MSDN.http://social.microsoft.com/Forums/zh-CN/wcf/thread/3f45500d-6de0-4c90-8a62-f08e1ede053e;
    But I still have some questions need to confirm,So do me a favour when you are free.

    Thanks a lot.

    Reply
  10. Kenny

    Yes, WCF uses completion ports under the hood for all of our channel implementations. As it’s an internal implementation detail, you won’t find much in the core product docs. There are some blog posts, forum posts, and possibly perf whitepapers that touch on this though.

    Reply
  11. Pingback: De standaard waardes van ServiceHost zijn laag! « Verbal Kempo .Net

  12. Venkat

    Hi,

    I have wcf service library. for debug i am using a console application to host the service. and in real time i am hosting it as windows service. The issue is that when i run as console application the service accepts more concurrent request (in the order of 100’s) and when i run the same code as windows service, the no of concurrent calls that is accepted is only handful some time less than 4.

    i found some article about similar issue at http://bytes.com/topic/net/answers/264495-tcp-ip-server-windows-service-vs-application but i am still investigating this. any help or direction ???

    Thanks

    Regards
    Venkat

    Reply
  13. Nick

    Perfect explanation… exactly what I was looking for when I ran into the above mentioned System.TimeoutException. Thanks!

    Reply
  14. Judi

    Hi Kenny,

    We have just started working in WCF and are currently having concurrency and timeout issues. We have session failure errors, transaction errors and inability to execute certain operations. All these errors are experienced randomly through the application. We have used some binding principles but not load balancing in the application. There are currently about 10 users of the application and there is a minimum of 4 logged in users at any given time. What would you suggest will be an amicable solution to the problem? Thanks in advance.

    Judi

    Reply
  15. Pingback: WCF proxy connection open causes errors | PHP Developer Resource

Leave a Reply

Your email address will not be published. Required fields are marked *