Cook Concurrent, Parallel, Async, and Non-blocking Together (1)
Last updated
Was this helpful?
Last updated
Was this helpful?
"Let's change our web service to an asynchronous model!" Tommy announced excitedly.
"How does that improve our service?" I asked.
"We can process many more requests in parallel using the asynchronous programming model!" Tommy answered.
"We're already using a multi-threaded service model, processing one request per thread at a given time. Are you suggesting that we can process more requests concurrently using the asynchronous model?" I questioned.
"'Concurrently'? How is that different from parallel processing?" Tommy was confused.
"They're related concepts, but there is a subtle difference," I said. "Concurrency is a programming model that deals with multiple tasks by executing them independently, potentially overlapping in time. Generally, there are two ways to achieve concurrency. The first is parallel programming, where tasks are divided into smaller, independent subtasks and executed on multiple cores or processors. We often use multi-threaded or multi-process models to achieve parallel programming. For example, if you and I need to cook a dinner with four dishes, we can divide the tasks so that you cook two dishes and I cook two dishes in parallel. The second way is the asynchronous programming model you mentioned. It allows a program to start a task and move on to another task without waiting for the previous task to complete. This is particularly useful for non-blocking I/O-bound tasks."
"Non-blocking I/O tasks?" Tommy was still confused.
"I/O tasks, or Input/Output tasks, are operations in a computer system where data is transferred between the system's memory and external devices, such as storage drives, network interfaces, or peripheral devices. There are two main types of I/O tasks: reading and writing. Reading retrieves data from an external source and stores it in the system's memory, while writing sends data from the system's memory to an external device for storage or processing. I/O tasks can be blocking or non-blocking. In blocking I/O, the executing thread waits for the I/O operation to complete before continuing. In blocking I/O, the executing thread initiates the I/O operation and continues processing other tasks without waiting for the operation to complete," I explained.
"Asynchronous programming requires nonblocking I/O. Using the dinner cooking analogy, if I have a gas range with four burners, I could theoretically cook the four dishes simultaneously. I just need to orchestrate my operations across the four burners so that it seems like I'm cooking four dishes at once."
"I get it. At any given moment, you attend to one burner, but you cook the four dishes concurrently. That's an asynchronous cooking model!" Tommy laughed. "Have you ever cooked four dishes concurrently in reality?"
"Nope. I'd probably burn the house down first," I answered honestly.