Concurrency vs Multi-threading vs Asynchronous Programming : Explained

Recently, I was speaking in an event and I asked a question to the audience in between my talk about Asynchronous programming , I found that some of the people were confused between multi-threading and asynchronous programming and some were saying that both are same. So I thought of explaining these terms and add one more term Concurrency. Here there are two concepts involved and both are completely different, First – Synchronous and  Asynchronous programming and second – Single threaded and multi-threaded applications. Each programming model (Synchronous and Asynchronous ) can run in single threaded and multi-threaded environment. Let’s discuss these in detail.

Synchronous Programming model – In this programming model, when a thread assigned to one task and start executing. Then completes the executing task then able to take up another task. In this model, it can not leave the executing task in between to take up some another task. Lte’s discuss how this model works in single and multi-threaded scenario.

Single Threaded – If we have couple of tasks to be worked on and the current system provides a single thread which can work all those, then it takes one by one and process as

singlethreadedHere we can see that we have a thread (Thread 1 ) and four tasks to be completed. Thread starts processing one by one and completes all. (The order in which tasks will be taken up, does not affect the execution, we can have different algorithm which can define the priorities of tasks)

Multi-Threaded – In this scenario, we used to have multiple threads which can take up the tasks and start working on that. It mans we have pools of threads (new threads also created based on the requirement and available resources) and bunch of tasks. So these thread can work on it as

multithreadedHere we can see that we have four threads and same number of tasks to complete and each thread start working on that This is ideal scenario but in normal cases, we used to have more number of tasks then number of available threads so whichever thread gets free takes up another task. As already said that spawning new thread is not an option every time because it requires system resources like CPU, memory and based on that number of threads should be decided.

Now Let’s talk about Asynchronous model and how does it behave in single and multi-threaded environment.

Asynchronous Programming Model – In contrary to Synchronous programming model, here a thread once start executing a task it can hold it in between and save the current state and start executing another task.

async-singleHere we can see that a single thread is responsible to complete all the tasks and tasks are interleaved to each other.

If our system is capable of having multiple threads then all the threads can work in asynchronous model as well a below

Async-mutlithreadedHere we can see that same task say T4, T5, T6.. are handled by multiple thread. This is the beauty of this scenario. As you can see that T4 was started first Thread 1 and completed by Thread 2. Similarly T6 is completed by Thread 2, Thread 3 and Thread 4.It shows the maximum utilization of the threads.

So till now we have discussed four scenarios –

  1. Synchronous Single Threaded
  2. Synchronous Multi-Threaded
  3. Asynchronous Single Threaded
  4. Asynchronous Multi-Threaded

Let’s discuss one more term – Concurrency.

Concurrency

In simple terms, concurrency means processing multiple requests at a time. As we have discussed two scenarios where multiple requests were getting processed, Multi-threaded programming and asynchronous model ( single and multi-threaded both). In case of asynchronous model whether single or multi-threaded at a time multiple tasks are in progress, some are in hold state and some is getting executed.There are many flavors of it bt that is beyond the scope of this post.

As discussed earlier, the new age is of asynchronous programming. Why is it so important?

Benefits of Asynchronous Programming

There are two things are very important for any application – Usability and Performance. Usability because say an user clicked on button to save some data as it requires multiple tasks like reading and populating data in internal object, establishing connection with SQL and saving it there etc. As SQL runs on another machine in network and runs under different process, it could be time consuming may take bit long. So If the request is handled by single thread then the screen will be in hanged state till the process completes. That’s why now a days many application and new framework completely rely asynchronous model.

Performance of application and system is also very important. It has been seen that while executing a request, around 70-80% of the the gets wasted in while waiting the dependent tasks. So this can be maximum utilized by asynchronous programming where once once the task is passed to another process (say SQL) current thread save the state and available to take another process and when the sql task completes any thread which is free can take up the task.

Asynchrony in ASP.NET

Async in ASP.NET can be a major boost in the throughput of your application. Lets how IIS processes a request.

CLRWhen a request is received by IIS, it takes a thread from CLR thread pool (IIS does not own any thread-pool itself instead uses CLR thread pool) and assigns to it which further processes the request. As number of threads are limited and new ones can be created at a certain limit then if the thread wastes most of the time in waiting state then it is going to hit hard your server or you can assume it is reality. But if you write asynchronous code (which now become very easy and can be written almost similar to synchronous using the new keywords async/await) then it will way faster for you and throughput of your server will increase significantly because instead of waiting something to complete, it will be available to thread pool to take new request. If the application has a lots of dependency and long running process then for those application, async programming will be bot less than a boon

So now we have understood the difference in multi-threading, asynchronous programming and the kind f benefit we can get using asynchronous programming model.

Cheers
Brij

IIS7 and Higher : system.webServer element ApplicationHost.config vs Web.config

Hi All,

This is another post on Authentication for ASP.NET applications. In one of my last posts, I talked about setting up authentication mode as Windows in web.config and Enabling/Disabling windows authentication at IIS. You can access that post from the below link.

Looking into Windows authentication at Web.config and at IIS
Continue reading

Looking into Windows authentication at Web.config and at IIS

Hello All,

I have seen many confusion around setting authentication mode as windows in web.config and enabling  Windows authentication at IIS.

First thing that there is no relation between setting authentication mode as windows at web.config and enabling/disabling (Integrated)Windows Authentication at IIS.

Continue reading

The requested content appears to be script and will not be served by the static file handler. : A Solution

Today, I faced one issue while working on one sample . And could not find solution in one go and error shown was not intuitive enough.

First let me share my environment. Windows 8 (64 bit), Visual Studio 2012, IIS8

I created an ASP.NET 2.0 web service using Visual Studio 2012 directly at IIS8. But as soon as, I tried to access my web service via browser I found the following error

ErrorAs you can see from the error, it does not point to any cause.

For this error there could be one issue that IIS is not registered with ASP.NET. So to register it, I opened the command prompt with run as administrator and navigated to the folder C:\Windows\Microsoft.NET\Framework\v2.0.50727. I navigated to framework version 2.0 because I was accessing the ASP.NET 2.0 webservice. Now I required to run the following command

aspnet_regiis -i

But as soon as I run the above command, I faced another error

errorcmd

Later, I found that my Operating System (Windows 8) that I am running is of 64 bit version. So again navigated 64bit version executable and it ran successfully

successfullyRanSo you can see above the message “Finished installing ASP.NET <2.0.50727>”.  Here in the above pic, if you see the red encircled area, I have ran here 64 bit version executable.

Also note, this issue arises only if you installed the IIS after installing Visual Studio.

Hope this would save lot of time of yours.

Thanks,
Brij

How to enable HTTPCompression at IIS6

Last week, I was working on performance improvement of one of my projects. So I read several Articles on Internet and found that we can configure HTTPCompression on IIS. It compress the response sent to the browser and the size of the response get reduced dramatically i e major improvement in performance. So I wanted to share you all.

I’ll discuss it point wise.

1: Why do we need HTTP compression?

Nowadays, we are building Rich Internet Applications, which is increasing the size of  Pages heavily. Means the more page size, more time it’ll take to load. But, IIS provides a feature to compress the responses and most common browsers support the HTTP Compression.

Means you can configure, HTTP compression at your web server, and browsers will understand it.

2: How much page size will be reduced.

Normally there are two algorithm supported. One is gzip and other one is deflate. I used Gzip in my website and found that the Page size was reduced by 60 to 70%.

3: When we don’t need compression.

If your page size are very less by default less than 60 to 70k. Then I think you don’t need this. Also if your users are having very high speed Internet then also you can ignore it. Because obviously the compression/decompression is a overhead if you are not gaining much.

4: How HTTPCompression works:

When a browser send a request to IIS, it also the send the information that what all kind of encoding supports. You can see the request header by many tools (One is Firebug that is available as a plugin for Firefox). You will be able to see following line in the request header.

Accept-Encoding: gzip,deflate

It says that this browser accepts gzip and deflate encoding

Now when IIS receives the request and find that that the requester can understand the given encoding. Based on the configuration, it encodes the response.

Now you can see the Response header. There it is mentioned, in what encoding is done on response as you can see it in header as

Content-Encoding: gzip

It means the response was encoded with gzip.

Here I am showing you all an example of Gmail.

Here I am showing the request header sent by the Firefox 3.5

It says it can accept encoding : gzip,deflate

Now let’s see the  response header

As you can see, the response is sent is gzipped format

5: What will happen if Browser does not understand any encoding?

Actually when browser sends a request to the server it tells the server what all encoding it supports and that is available in Request Header. If it does not support,  it may not send the Accept-Encoding tag.

Now when IIS receives the request and if it does not find any encoding mechanism supported by Requester, It does not apply any compression/encoding mechanism and the response is not encoded and sent it in normal format.

So you don’t need to worry about, it any browser does not understand gzip or deflate, then what will happen. IIS takes care, IT only encodes when it is supported by the requester.

6: How to configure HTTPCompression at IIS,

There are two types of Compression that can be configured.

First one: Static ( for static files like some css, javascript file etc.)

Other one: Dynamic (means for for dynamic generated page/response) .

There is no console available to configure to Configure HTTPCompression.

So there are two ways to configure HTTP compression at IIS.

First: Update the IIS metabase files directly.

Second: Use some commands to update it.

Here I’ll discuss the second one and will discuss the commands that can be used to configure it.

Here you need to do two things.

First: Configure the IIS for HTTPCompression

Second: Configure what all types/extension will be encoded

So for that you need to run the following commands at your web server:

Configure the IIS6 for HTTP Compression-

First Open command prompt and go to your IIS root folder, normally it would be “c:\inetpub\adminscripts\” then follow the below steps.

Static Compression:

To see whether Compression is enabled or not:

cscript adsutil.vbs get w3svc/filters/compression/parameters/HcDoStaticCompression

Enable/Disable Static Compression

adsutil.vbs set w3svc/filters/compression/parameters/HcDoStaticCompression true/false

To view what all files will be encoded:

cscript adsutil.vbs get W3SVC/Filters/Compression/gzip/HcFileExtensions (for gzip)

cscript adsutil.vbs get W3SVC/Filters/Compression/deflate/HcFileExtensions (for deflate)

To add more files for Compression

cscript adsutil.vbs set W3SVC/Filters/Compression/gzip/HcFileExtensions “js” “css” “png” “bmp” “swf” “doc” “docx” (for gzip)

cscript adsutil.vbs set W3SVC/Filters/Compression/deflate/HcFileExtensions “js” “css” “png” “bmp” “swf” “doc” “docx” (for deflate)

Dynamic Compression:

To see whether Compression is enabled or not:

cscript adsutil.vbs get w3svc/filters/compression/parameters/HcDoDynamicCompression

Enable/Disable Dynamic  Compression

cscript adsutil.vbs set w3svc/filters/compression/parameters/HcDoDynamicCompression true/false

To view what all files will be encoded:

cscript adsutil.vbs get W3SVC/Filters/Compression/gzip/HcScriptFileExtensions (for gzip)

cscript adsutil.vbs get W3SVC/Filters/Compression/deflate/HcScriptFileExtensions (for deflate)

To add more file extension

cscript adsutil.vbs set W3SVC/Filters/Compression/deflate/HcScriptFileExtensions “asp” “exe” “dll” “aspx” “asmx” (for gzip)

cscript.exe adsutil.vbs set W3Svc/Filters/Compression/GZIP/HcScriptFileExtensions “asp” “exe” “dll” “aspx” “asmx” (for deflate)

Now reset the IIS. Now you web server is ready to compress the responses based on incoming Requests.

I’ll suggest to all configure it at IIS6 and see/analyze the performance.

Note: I have explained above the settings for IIS6.

Please share you feedback:

Thanks,

Brij

Failed to access IIS metabase

Few days back, I formatted my system due to some virus.. Then again installed OS and VS2008 for my learning.I am in habit of using integrated webserver with Visual studio.

But later,I thought of deploying one WCF service. So when I deployed my service and tried to access, It started barfing me.This was running smoothly prior to formatting my system.

I kept trying to resolve this issue but it took a lot of time.Finally it started running as earlier.

Root Cause: Acually after installation of my OS Windows XP, I installed .net framework and Visual studio.Then when I needed IIS, I installed it.

Cause was, ASPNET user does not had sufficient rights over IIS

Solution:Install aspnet using command aspnet_regiis -i as

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>aspnet_regiis -i

then reset iis using iisreset command in command propmpt..

it should work now..

Happy .neting