Sunday, August 29, 2010

WCF 6(Service Behavior: ConcurrencyMode)

In this post i will try to explain concurrency mode available to configure the WCF service. Concurrency is different from Instance. WCF Instance dictates how objects are created whereas concurrency talks about requests handled by WCF object. Roughly you can say the concurrency is actualy threads available on service side to do your task.
There are three different type of concurrency mode available to configure the service.
1) Single:
A single request has access to the WCF service object and only that request will be processed at a given moment of time. Other requests hav to wait until the request processed by WCF service is not completed.
2) Multiple:
Multiple requests can be handled by the WCF service object by multiple threads at any given moment of time.It gives you a great throughput. But concurrency issue will be there. Its your responsibility to to synchronize your methods.
3) Reentrant:
A single thread has access to WCF service object, but the thread can exit the WCF service to call another WCF service or can also call WCF client through callback. It is almost like Single but a small difference.
In case of single till the callback finishes the service won't allow any other request to get process, but in case of Reentrant it will release the lock and other request will start processing and when callback request comes back it will processed like a new request.
So whenever a call back from other service or from client is required then use reentrant else you may face a deadlock.

Service behavior is combination of instance context mode and concurrency mode. The following table summarizes the service behavior for different combinations:

Instance Context Mode
Concurrency Mode
(Single (Default))
Concurrency Mode
(Multiple)
Concurrency Mode
(Reentrant)
Single
(Single instance for all client)
Single thread for all clients
Multiple threads for all clients
Single threads for all clients, locks are released when calls diverted to other WCF services.
PerSession
(Multiple instance per client)
Single thread for every client.
Multiple threads for every request.
Single threads for all clients, locks are released when calls diverted to other WCF services.
PerCall (Default)
(Multiple instance for every method call)
Single thread for every client
Multiple thread for every client
Single threads for all clients, locks are released when calls diverted to other WCF services.

Instance mode = per Call and Concurrency = Multiple
In this combination multiple instances are created for every call but multiple threads serve every method call to WCF service instance.
Instance mode = per session and Concurrency = single
In this combination one WCF service instance is created for every WCF client session because the WCF instance mode is set to per session. All the method calls are executed in a sequential manner one by one. In other words only one thread is available for all method calls for a particular service instance.

Instance mode = per session and Concurrency = Multiple
In this combination one WCF instance is created for every WCF client session and every method call is run over multiple threads. Below is the pictorial representation of the same.

Instance mode = Single and Concurrency = Single
In this combination only one instance of WCF service instance is created which serves all requests which are sent from all WCF clients. These entire requests are served using only one thread.

Instance mode = Single and Concurrency = Multiple
In this combination one WCF service instance is created for serve all WCF clients. All request are served using multiple / different threads.

Throttling behavior:
WCF throttling settings helps you to put an upper limit on number of concurrent calls, WCF instances and concurrent session. WCF provides 3 ways by which you can define upper limits MaxConcurrentCalls, MaxConcurrentInstances and MaxConcurrentSessions.

MaxConcurrentCalls: - Limits the number of concurrent requests that can be processed by WCF service instances.
MaxConcurrentInstances: - Limits the number of service instances that can be allocated at a given time. When it’s a PerCall services, this value matches the number of concurrent calls. For PerSession services, this value equals the number of active session instances. This setting doesn’t matter for Single instancing mode, because only one instance is ever created.
MaxConcurrentSessions: - Limits the number of active sessions allowed for the service.

No comments:

Post a Comment