Yan Cui
I help clients go faster for less using serverless technologies.
This article is brought to you by
Don’t reinvent the patterns. Catalyst gives you consistent APIs for messaging, data, and workflow with key microservice patterns like circuit-breakers and retries for free.
ServiceThrottlingAttribute
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] public sealed class ServiceThrottlingAttribute : Attribute, IServiceBehavior { public int MaxConcurrentCalls { get; set; } public int MaxConcurrentInstances { get; set; } public int MaxConcurrentSessions { get; set; } public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) { } public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) { } public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) { var currentThrottle = serviceDescription.Behaviors.Find<ServiceThrottlingBehavior>(); if (currentThrottle == null) { serviceDescription.Behaviors.Add(GetConfiguredServiceThrottlingBehaviour()); } } private ServiceThrottlingBehavior GetConfiguredServiceThrottlingBehaviour() { var behaviour = new ServiceThrottlingBehavior(); if (MaxConcurrentCalls > 0) { behaviour.MaxConcurrentCalls = MaxConcurrentCalls; } if (MaxConcurrentInstances > 0) { behaviour.MaxConcurrentInstances = MaxConcurrentInstances; } if (MaxConcurrentSessions > 0) { behaviour.MaxConcurrentSessions = MaxConcurrentSessions; } return behaviour; } }
Performance
Before you go ahead and create yourself an ultra-scalable service by setting the max concurrent calls/sessions/instances values to be very high you need to be aware what the performance implications are.
First, you need to know that WCF is configured to be safe from DOS attacks out of the box and the default configurations for service throttling behaviour is very conservative:
- MaxConcurrentCalls – default is 16, represents the max number of messages that can actively be processed.
- MaxConcurrentInstances – default is 26. For a “PerSession” service this represents the max number of session; for a “PerCall” service this represents the max number of concurrent calls; for a “Singleton” service this is meaningless.
- MaxConcurrentSessions – default is 10, represents the max number of sessions a service can accept at one time and only affects session-enabled channels. Increase this to allow more than 10 concurrent sessions.
If your InstanceContext is set to “PerCall” you should set maxConcurrentSessions and maxConcurrentCalls to the same value since each call is its own session. You should target this value to be at least 25-30. However you shouldn’t need to go higher than 64.
If your InstanceContext is set to “PerSession” you should set maxConcurrentCalls to be at least 25-30. Your maxConcurrentSessions should be the number of users you want to have concurrently connected.
If your InstanceContext is set to “Single” you should use the same settings as “PerSession”, but the maxConcurrentSessions will only apply if session support is active (this is set with the SessionMode attribute on the ServiceContractAttribute).
References
Whenever you’re ready, here are 3 ways I can help you:
- Production-Ready Serverless: Join 20+ AWS Heroes & Community Builders and 1000+ other students in levelling up your serverless game. This is your one-stop shop for quickly levelling up your serverless skills.
- I help clients launch product ideas, improve their development processes and upskill their teams. If you’d like to work together, then let’s get in touch.
- Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.