Improve response times and handle more users with parallel processing
Building a Web application using non blocking calls to the data layer is a great it to increase the scalability of your s Ystem. Performing a task asynchronously frees up the worker thread to accept another request and work are being done Ground. Until recently, designing your system this is significantly more complicated. With the. NET Framework 4.5 and Entity Framework 6, the task has become trivial.
Six TED Talks that can change your career
These talks would help you reshape how do you approach work and see your career in a new light.
READ Now
If you ' re starting out building a new ASP. NET MVC 5 Application, the new best practice are to use Async tasks for basic Ally everything, unless you has a good reason to do otherwise. Your view controllers can be implemented as async methods, Your generic collections has async functions, and with EF6, yo ur data layer can take advantage of the technique as well. This functionality have existed in different forms in the past (PLINQ, Task Parallel Library, etc.) but the coding overhead and added thread management caused many to opt out from implementing it. Today, The system.threading.tasks library greatly simplifies the process.
In a. NET Web application running on a IIS server, the application pool for the app is only have so many threads it can sp In-to-use for request handling. When a user makes a request for a relatively long running task, the synchronous thread would begin to execute and would be B Locked while it's waiting for data to return, let's say from an IO operation. If a new request comes in during that time, a new thread would be is required to fulfill that request as the existing thread I s waiting on IO. Spinning up these new threads requires additional memory and processing, and if your application is heavily used the app P Ool can quickly run out of available threads and result in an unresponsive application or even a timeout error R.
With asynchronous tasks, once the thread reaches the long running task it you ' ve called asynchronously, the thread WI ll be released and become available to process further requests while the task is being executed. This would keep your application responsive to the user and it'll be able to accommodate significantly more simultaneous Requests. In terms of benchmarked performance, I ' ve seen load tests show 300% improvement in response times and concurrent Connections boost almost 8x [I can ' t find the link, but I promise I saw it] over the synchronous count Erparts.
It ' s not a silver bullet however. There is a time and place for the async tasks, knowing when the them is just as important as knowing when you should. For a low-level guide to how it works, there is a look at this MSDN article detailing the process. Basically, the rule of thumb is to use an async task when your operation are accessing a slow medium like the network, disk , or database. If your operation is using the data that you know are already in memory (memory stream, cached object, etc.), you ' re better off Using a synchronous task. another vital consideration is the async tasks is not thread safe and they be not compa Tible with the lazy loading. to Keep your code from imploding, you need to ensure so you never execute both async tasks In parallel using the same data context. You also need to make sure you eager load/include any relational data your object might has to fetch from your async CA LL.
There is some caveats, and even though it ' s suggested that's use async tasks as the de facto going forward, you need to Consider your application thoroughly before diving in. The performance benefits is too great to ignore, and so while it could add a bit of complexity to your software, it should pay Off in spades if a few thousand people decide to use it at the same time. Pay close attention to the functions your use of it on, structure your data layer in a by that promotes compartmentalization Of the context, and know when to use a synchronous call instead. With those things in mind you should is on your it to providing a snappy user experience and software your server Adminis Trators would love.
PREVIOUS POST
3 Reasons to use code first design with Entity Framework
NEXT POST
Does relying on the IDE for development make a bad programmer?
Matthew Mombrea
Matthew Mombrea is a software engineer, founder Ofcypress North, and a technology enthusiast.
The opinions expressed in this blog is those of the author and does not necessarily represent those of ITWorld, its parent, Subsidiary or affiliated companies.
Why you should the use of async tasks in. NET 4.5 and Entity Framework 6