If you are following this blog, you might have observed that I am currently exploring asynchronous programming. Being web developers, it is important for us to understand how asynchrony will help the web applications we develop.
Please note that this set of operations is performed in response to a single request. Technically, we can say that these four operations include the following set of heavy tasks:
As it is a banking application which is usually accessed by a large number of viewers, it is very important to serve them quickly. If there are hundreds of such requests, the server may respond very slowly to most of the users.
So, the request is processed in a single-threaded model. If any task of the thread is halted for an I/O operation or a network call or any such request to complete, the corresponding thread is remains idle till the task gets required input. Waiting time of thread may be some milliseconds. Imagine a case when all threads are engaged and some of them are waiting because of heavy operations. If a new request hits the server now, it will have to wait until a thread is freed to process its request.
This can be achieved using asynchronous programming on the server side. We can use the asynchronous language features of C# or VB.NET to enhance such applications. You might have questions in your mind about the implementation of asynchrony in ASP.NET. We will discuss about them in subsequent posts.
Happy coding!
Let’s take an example
Say, we have a banking web application where the user wants to get the details of all the transactions he made during last two months in the form of an excel sheet. If the user would have performed some hundreds of operations during this period, the server operation will take considerable amount of time as it has to perform the following operations:- Fetch details of all transactions into memory
- Create an excel file with appropriate columns
- Write data into the excel file
- Send it to the user across the network
Please note that this set of operations is performed in response to a single request. Technically, we can say that these four operations include the following set of heavy tasks:
- Querying database to fetch details of hundreds of transactions
- Creating a file on the disk
- Writing data from an object into file
- Sending file to the user’s system
As it is a banking application which is usually accessed by a large number of viewers, it is very important to serve them quickly. If there are hundreds of such requests, the server may respond very slowly to most of the users.
How does the server respond to a request?
In a typical ASP.NET web application, the server performs the following tasks when it gets a request:- Picks up a thread from CLR managed thread pool on and assigns the new request to it
- The assigned thread performs all the tasks one by one. It works for the same request till all tasks of the request are completed
- The thread is returned to thread pool as soon as it finishes the assigned tasks
So, the request is processed in a single-threaded model. If any task of the thread is halted for an I/O operation or a network call or any such request to complete, the corresponding thread is remains idle till the task gets required input. Waiting time of thread may be some milliseconds. Imagine a case when all threads are engaged and some of them are waiting because of heavy operations. If a new request hits the server now, it will have to wait until a thread is freed to process its request.
How can it be improved?
Performance of the web application described above will improve if the thread attached to a waiting request is used process some other request. Once the request receives response from the operation it was waiting for, processing of the request is resumed with help of a free thread from the CLR thread pool.This can be achieved using asynchronous programming on the server side. We can use the asynchronous language features of C# or VB.NET to enhance such applications. You might have questions in your mind about the implementation of asynchrony in ASP.NET. We will discuss about them in subsequent posts.
Happy coding!
No comments:
Post a Comment
Note: only a member of this blog may post a comment.