VIEW ARTICLE

Why should we prefer to write Async Code always

Author: Anant Kumar
Category: Programming
Submitted On: 9/21/2017
Published On: 9/21/2017
ABSTRACT: This article mentions major advantages of async programming and its need in modern day applications development.

What is Async Programming and why should we prefer to write Async code always

Modern day applications are generally data intensive and therefore they need to make extensive use of data lying in files, database and devices across networks. If we start performing such read-write operations one-after another in serial manner, the Input/Output operations, since they operate via mechanical process underneath, start blocking further operations. The speed with which these operations are required to be executed, generally fails to match the demands. Because of this, more and more operations would start queuing up for execution during application run-time. This would ultimately result in a bad user experiences. This can lead to the failure of entire application particularly when more and more users are using the application at any point of time. There may be situations when application may crash as well.

Another disadvantage is that serial execution of programming logics (involving disk I/O, Network Calls etc) also leads to poor usage of hardware execution capacity. This is because the operations in queue, are waiting to execute, which otherwise would have started there execution simultaneously and took advantage of advanced execution capacity of modern hardware design.


Now this has got a drastic change with the introduction of Async programming patterns. This was actually a core requirement because users wanted responsive UI experience, and the organizations want their server(s) / infrastructure to scale with ever increasing business demands.
Applications are expected to handle millions of users' requests using the application concurrently. And application servers are expected to service increased or decreased traffic gracefully and use the resources optimally.

Here comes the role of Async programming. It makes it easier for the programmers to go ahead with simultaneous calls to methods involving tasks of fetching /reading /writing information from devices and across networks. Each task is handed over to a method call and an event is raised to the caller when that particular task is finished. This way multiple methods can be invoked concurrently.

In other words, Asynchronous programming is a means of parallel programming in which a unit of work/task runs separately from the main application thread and notifies the calling/main thread of its completion, failure or progress when it finishes.
Async Method Call

We should always write code to invoke methods in Async manner unless we have a reason for not doing so.

How easy is it write Async code

Modern programming languages provide the built-in construct to perform actions in Async manner. Task-based async APIs and language-level asynchronous programming model makes it an easy task. For instance in .NET programming construct in ASP.NET MVC model, asynchronous method call can be implemented by writing few keywords (async and await) as in following code snippet: -

[HttpPost]
[ValidateAntiForgeryToken]
public async Task Create([Bind(Include = "RecordID,RecordUserNumber,RecordedOn,RecordDetail,RecordedByName")] UserRecord userRecord)
  {
       if (ModelState.IsValid)
       {
           try
           {
             db.UserRecords.Add(userRecord);
             await db.SaveChangesAsync();
           }
          catch (Exception e)
          {
             ViewBag.ErrorMsg = "Error Occured While Saving the Record! - " + e.Message;
             goto BACK;
           }
          return RedirectToAction("Index");
          }
           
        }
BACK:
      return PartialView("_Create", userRecord);
  }

Notice the usage of "async" and "await" keywords. It is assumed here that database operation may take some time to complete and therefore the application UI or other operations, should not wait for this.

Go to Top
Why should we prefer to write Async Code always

Related Questions...(0) Ask A Question

Ask a Question

Go to Top