Parallelism and concurrency are related but different concepts. Parallelism is a very generic concept in computing and it can applied in a number of scenarios to solve completely different problems. For example, in the web UI (JavaScript world) parallelism is used to improve the responsiveness of the web app. In the web server (server side languages & technologies), parallelism is used to improve the efficiency of the server. Before digging deeper, let's understand the basics.
Layman's Explanation
Let’s take your daily commute to work. You are driving your car while simultaneously listening to music. You are performing 2 tasks in parallel. You know exactly which street are you on & at the same time, you know which song is playing at that moment. No prizes for guessing that this is an example of parallelism. Concurrency is slightly more tricky.
Once you reach the office, how does your typical day look? Let’s say that you execute a query in a database server. Let’s assume that it’s a long running query & will take 20 minutes to complete. You won’t sit idle for that duration. You’ll then pick up other tasks, like say you need to send an email to a colleague to discuss about a work item. You send that mail and you need your colleague’s response to continue. But you don’t wait for it. You start working on something else. But when the query completes its execution, you go back to that and start looking at the result. Again, when you receive the email response, you’ll look at that.
So, you are never really doing more than 1 task at a time, but you are continually monitoring, handling or progressing in multiple tasks. You are essentially juggling between tasks such that you don’t sit idle as you wait for an external dependency to complete their work. This is essentially concurrency. Doing one task at a time but progressing in multiple tasks. The key factor is re-using the idle time.
In Computing World
There are 3 graphs shown below. Can you guess which one represents parallelism and which one represents concurrency?
The second graph represents parallelism while the third one represents concurrency. That was easy! Let’s look at how a web server handles incoming HTTP requests. The tasks here represent incoming requests. Let’s assume that each task takes 2 seconds to process. This means that, once the web server receives the request, it’ll take 2 seconds to process the request & respond to the client (typically, a browser).
Most web servers utilize threads to do the work. If we assume that our server has only 1 thread, and it receives 4 requests, then by our logic (2 second per request), it’ll take 8 seconds for all the requests to get processed. This is graph 1.
Graph 2 represents parallelism. Most web servers automatically assign every request to a thread (except for servers like Nginx & GoLang based systems). So, when we write a server-side code, for example a web service method, the web server will automatically create multiple instances of that method to serve each HTTP request. This is parallelism built into web servers. We don’t need to write any code to do this.
Graph 3 represents concurrency. Again, we are assuming that there is only 1 thread, but the tasks are not processed one by one. The thread starts processing task 1, then abruptly it starts processing task 2 & then jumps to task 3 and comes back to task 1 and so on. Magically, the whole thing ends in 4 seconds, even though basic calculation suggests that it should take 8 seconds. How is this possible? To explain this, we'll need to take a closer look at what constitutes a task. We'll explore this in Part 2 of the article.

Comments
Post a Comment