July 10, 2017
Concurrency and Parallelism are two of the most talked-about topics in computer science. The reason why is because these are very important concepts to build a scalable, reliable and fault-tolerant service. Also, there are a huge number of discussions/debates as to what the differences are!
Most people thought in different ways which has spread several beliefs which has obviously tattered the meanings of both terms. In fact, some people would say Concurrent = Parallel, and another group would think otherwise.
Here’s what Rob Pike (co-inventor of the Go language) had to say about this:
Concurrency is about dealing with lots of things at once. Parallelism is about doing lots of things at once.
Let me give you a real-world example.
Let’s just assume that I’m an owner of a small-sized restaurant here in the Philippines.
Bob and John (customers) had ordered 1x spaghetti each. Once I received this order, I’ll prepare this by:
This is how the default setup in a language is like. It goes through the steps serially by waiting for a process to finish before proceeding to a new one. So if there’s a long-running operation that is running, the next process should wait for it to finish before it runs.
It sounds bad but it works most of the time.
While Bob and John are eating their delicious spaghettis made by yours truly; Lisa, Rey, and Jack each ordered a watermelon shake. So to do this concurrently:
Parallel is doing several tasks simultaneously so to take the above example and make it work in a parallelized way, I would hire two more chefs; Billy and Tophs.
The good thing about this is it happens at the same time so it is obviously faster than the previous approaches.
In computer sense, a code running concurrently means that it is executing a number of different tasks at the same time by switching contexts regardless of how many threads/workers/cores are available. With parallel, each tasks are mostly ran at the same time, using a separate worker or a CPU core.
Depending on your programming language, the capability to do it successfully can
be easy or difficult. With a mutable language like Ruby, you have to deal with
shared states and
thread safetiness to prevent corrupt data.