Synchronous and Asynchronous I/O for Dummies

I have been digging into concurrency for a while now, and I noticed there was a little confusion around people understanding what Synchronous and Asynchronous I/O is. This article tries to use real life example to explain what I/O, Synchronous I/O and Asynchronous I/O is.

TL;DR

I/O Explained

According to Wikipedia I/O i.e input/output is the communication between an information processing system, such as a computer, and the outside world, possibly a human or another information processing system. Inputs can be seen as data sent by the human to the information system while output is the result the information system sends back. I would be using a real life object as an example in this article so we fully understand how synchronous I/O differs from asynchronous I/O.

Real life Illustration of I/O

Our day to day activities are really based on this simple word I/O (input/output); how is that? We would see shortly. Let me introduce you to John and Coldstone. John is a fresh graduate looking for great opportunities to make some cash for his masters program, Coldstone is well known for quality and creamery Ice cream and are currently looking for a sales guy in a new location close to John’s house. Now, this was the deal, John gets 10% for each Ice cream he sales. John was excited about the offer and decided to start ASAP. On the first day of sales, John had customers queue up on a single file. The sales framework was simple; customer tell John which flavor he/she wanted, John gives customer the requested flavor, John collects his money and move to the next customer. What you see happening between John and the customer is I/O operation, customers performs I (input) by asking for a flavor of Ice cream while john performs O (output) by giving customers the Ice cream flavor requested.

What is Synchronous I/O

The first 10 customers knew what they wanted, so John didn’t spend so much time answering them. But, unfortunately for John the 11th customer was a woman whose size was almost equal her thinking. She wasn’t sure of what flavor she wanted, so John spent absolutely the whole day attending to the woman and wasn’t able to attend to other customers. The scenario given above  is known as synchronous I/O, because John have to finish with one customer before moving to the next, and while he his answering one customer other are waiting doing absolutely nothing.
John left that day making 110% off his total sales because he was only able to attend to 11 customers. Was there a possibility of John making more than that? Was there a better sales framework that would have yielded more profit for John? We would find out shortly.

What is Asynchronous I/O

 On the second day of sales, John decided to take a new approach similar to the first but with a little tweak. John put a big board that showcases all Ice cream flavor available with pictures, description’s and there respective prices and told customers’ to choose from what is on the board before or while on the queue. While on the queue John assumes that you already have a flavor in mind, so John just ask for the flavor you want, give the flavor to you and collect his money. But, peradventure our confused woman from the illustration above was the first on the queue, instead of trying to help her figure out what flavor to buy, John ask her to step out of the queue so she can go back to the board to figure out what to buy. While she is at the board still trying to figure out what buy, John doesn’t wait for her to finish thinking of what to buy instead he moves to the next person on the queue. John attends to our confused customer whenever she is ready as long as he his not attending to another person. John was able to attend to a total of 2345 customers, this did not only make the client richer, it also added 23450% of the total sales to Johns pocket. The second approach is simply what is known as asynchronous I/O.

I/O in summary

In summary asynchronous I/O, or non-blocking I/O is a form of input/output processing that permits other processing to continue before the transmission has finished while synchronous I/O or blocking I/O would block the progress of a program while the communication is in progress, leaving system resources idle. Although there are cases were synchronous operation is often preferred and there are cases where it is not needed. Most applications built today are data-intensive real-time applications that run across distributed devices, synchronous operation is not necessary in this case; as we should be able to do other stuff while fetching data from the database or while performing other I/O related operations like HTTP requests, disk reads and writes e.t.c.
Asynchronous I/O, or non-blocking I/O is one reason why Javascript is the most popular programming language in 2015 as analyzed by RedMonk. To understand how Javascript handle concurrency read about the Javascript Event Loop.