Tags

, , , ,

                     instancingintro

The following is in this article:

  • What Concurrency is
  • The difference between instancing and concurrency
  • Instancing with Concurrency
  • Attachment
  • Summary
Concurrency

“ Several computations are executing simultaneously.”

I will just try to explain concurrency with simple day-to-day activity. Assume that you are going for a shopping to get some provisional goods for your home, if you find only one shopkeeper in that shop then obviously you must be in a queue to get your requirements done. Yes, the only shopkeeper serves the request one after the other. If the same shop has multiple shopkeepers then they will manage multiple buyers at the same time, the result will be that the requests are processed very quickly. If we relate the same activity with WCF then I could say that, by forming multiple threads in the server instance, more requests from the client proxy would be addressed in a concurrent fashion. We can use the concurrency feature in the following three ways:

  • Single
  • Multiple
  • Reentrant

Single

A single request will be processed by a single thread on a server at any point of time. The client proxy sends the request to the server, it process the request and takes another request. One request will be processed on the server side at a time. Other requests need to wait until the request processed by the service is completed. The following is the declaration of Concurrency.Single:

                      WCF2

Multiple

Multiple requests will be processed by multiple threads on the server at any point of time. The client proxy sends multiple requests to the server. Requests are processed by the server by spawning multiple threads on the server object. Doing this you will get more throughput here. The following is the declaration of Concurrency.Multiple.

Reentrant

The reentrant concurrency mode is nearly like the single concurrency mode. It is a single-threaded service instance that receives requests from the client proxy and it unlocks the thread only after the reentrant service object calls the other service or can also call a WCF client through call back. A reentrant service calls the other service or call back so other client requests would be allowed. The following is the declaration of Concurrency.Reentrant:

                    WCF3

Difference between Instancing and Concurrency

  • Instancing is about how the objects are created on the server, such as single or multiple objects are created on the server side
  • Concurrency is about how the requests are handled by the WCF object, such as single or multiple threads are created on the server side to process the client requests

Instancing with the Concurrency

Though WCF provides many facilities to build a distributed system, to write efficient services the developer must think about the answer for the following questions:

  • What is the frequency of the service instances that are to be created?
  • How long should the created instance exist?
  • How about the service going to manage the incoming client requests? What level of concurrency (Single or Multiple) is needed?
  • Does your service require to maintain the state between calls?

Ok, let’s discuss 9 combinations of instancing and concurrency.

Instance Mode -> Per Call and Concurrency -> Single

“Single thread for every client”

With the instance mode percall, new instances will be created for every service method call, with the concurrency mode Single, one thread will be created in the server to manage all the requests from one client.

                      WCF5

In the preceding diagram you can find that a server instance will be created on every method call that the client does. The client will call a method by creating a new service instance, execute the service method and it releases the memory. One thread sitting on the server for each client manages the request in one after the other. Let’s discuss the same concept with source code.

                   WCF6

One is above the exposed service method that actually maintains the number of coffees selling the total sale made on coffee. For our demonstration purposes just the Thread Id is included to see how many threads are actually serving the client’s request. Here is the client portion where it triggers the server method SellCoffee.

                 WCF7

In the client I am just trying to sell 100 coffees at a moment. With this example we will see how the instances are created and how it manages the client requests.

               WCF8

 

If you see in the above snapshot, you can find that two client applications were launched. As I said earlier, the instance will be created on each and every server method call invocation. In our case the Instance number will always display the value 1 across the clients, because of the existing information will be lost on new instance creation. Since the concurrency mode is single, you can see in the above picture that each client request will be managed by a single thread (Thread Id 5 for Client 1) and (Thread Id 6 for Client 2). For further details please refer to the source code attached.

Instance Mode -> Per Call and Concurrency -> Multiple

“Multiple Threads for every client”

With the instance mode percall, new instances will be created for every service method call. When the concurrency mode is Multiple, Multiple threads will be created on the server to manage all the requests from each client.

               WCF9

In the above diagram you can find that a server instance will be created on every method call that the client does. The client will call a method by creating a new service instance, execute the service method and release the memory. Multiple threads sitting in the server for each client to manage the request concurrently. Let’s discuss the same concept with the source code. For further details please refer to the attached source code.

Instance Mode -> Per Session and Concurrency -> Single

“Single Thread for Every Client”

When the Instance Mode is PerSession, an instance will be created for every client session. When the concurrency mode is Single, one thread will be created in the server to manage all the requests from one client.

                 WCF10

                WCF11

As you can see in the snapshot above, you can find that one server instance will be created for every client session. The Instance number is incremented and maintained for one client. If you look at the instance number in the another client again incremented starting from the 0. Threads are created in the server for every client session. Thread number 4 is created for the client 1 and the thread number 8 is created for the client 2. For further details please refer to the source code attached.

Instance Mode -> Per Session and Concurrency -> Multiple

“Multiple threads for every request”

When the Instance Mode is PerSession, an instance will be created for every client session. When the concurrency mode is multiple, multiple threads will be created on the server to manage all the requests from one client call.

                WCF12

               WCF13

In the above snapshot you can find that multiple threads are created for every client session.On every client session the server spawns multiple threads to process each request of the specific client. In our case Thread numbers 5, 10 and 15 are created in the server side. For further details please refer to the source code attached.

Instance Mode -> Single and Concurrency -> Single

“Single Thread for all Clients”

When the instance mode is single, the single server instance will be created on the server side, using the same and the only instance, all the clients will be communicated. Here the concurrency mode is also single, so the single thread in the server manages all the requests from all the clients. Comparing all other combinations, this one might be relatively slow, because the only thread available to process the requests are from all the clients.

                 WCF14

                WCF15

Since the same server instance is used across clients you can see the information (Instance number) are maintained in the server. Also a single thread is dedicated to each client. Thread number 17 is managing the client 1 requests, whereas the Thread 18 is managing the client 2 requests and Thread 8 is managing the client 3 requests. For further details please refer to the source code attached.

Instance Mode -> Single and Concurrency -> Multiple

“Multiple Threads for all the clients”

When the instance mode is single, the single server instance, will be created on the server side, using the same and the only instance, all the clients will be communicated. Here the concurrency mode is Multiple, so the Multiple threads in the server manages all the requests.

               WCF16

               WCF17

Since the same server instance is used across clients you can see the information (Instance number) maintained in the server. Also Multiple threads are created for each client request. In the snapshot above you can see that different threads are created for each request. For further details please refer to the source code attached.

Reentrant

There is only one thread in the server to serve all requests. If your WCF service makes an outbound call to another WCF service, or makes a client call, it releases the thread lock. In other words, until the outbound call is completed, other WCF clients cannot make calls.

Attachment

Refer the below attached source code for debugging purposes

SourceCode


Summary

  Concurrency Mode
Instance ContextMode Single Multiple Reentrant
Single Single Thread for all the client Multiple threads for all the clients Single Single Thread for all clients. Lock will be released when the outbound call to other services are completed
PerSession Single Thread for every client Multiple threads for every request Single Thread for all clients. Lock will be released when the outbound call to other services are completed
PerCall Single Thread for every client Multiple threads for each request Single Thread for all clients. Lock will be released when the outbound call to other services are completed
Advertisements