Table of Contents
Technical vocabulary in IT industry is sometimes very confusing and “Concurrency” and “Parallelism” are some of them. Many developers think “Concurrency and parallelism means executing at the same time” which is right 50%, but with one big difference:
- Concurrency gives you a feel of parallelism and parallelism as the name implies is actual parallelism.
Feel of parallelism means you execute multiple tasks on the same core and the core switches context between tasks and serves them. You can also term this has time slicing / overlapping time period because your single core is just dedicating some time to one task and then some time to other.
Actual parallelism means you execute multiple tasks on multiple cores parallely.
Note: “Concurrency is a broader term and Parallelism is a subset of it”.
Mapping to the real world, the left image depicts parallelism the right image depicts concurrency.
In order to achieve actual parallelism, we need dedicated cores, separate memory and so on.
We need MORE RESOURCES.
Let’s say we want to show a progress bar for some task completed. Now we really do not want to have separate cores allocated to display the progress.
|
|
We do not want PERFORMANCE here, we want that physiologically the end user feels both tasks are happening simultaneously.
We want to just beat the human eye capability of 100 FPS and give an illusion of parallelism without stressing our computer resources. But let’s say we want to process big Excel files with a million records, then yes we would love to have actual parallelism to achieve performance.
In order to achieve concurrency, we need to compose our application logic independently. For instance, let’s say you want to process employee data where you want to increment the salary by x% and bonus by x%.
So you can decompose the application into logical units by following different designs:
Design 1
- Divide data into 50% size each.
- Process each 50% as separate unit.
Design 2
- Process bonus calculation as separate unit.
- Process salary calculation as separate unit.
Design 3
- Divide data into 50% size each.
- For every 50% data process bonus calculation separately and salary calculation separately.
There can be many such designs and combinations. So when you say your application is supporting concurrency, your application should be composed into small independent units.
Now you take these units and run on one core (Concurrency) or you run on multiple cores (Parallelism). So concurrency is about design while on parallelism, we talk more from the hardware perspective, 2 core, 3 cores and so on.
If you try to run every concurrent code as parallel, you have resource starvation unnecessarily. So ask yourself if you want an illusion (concurrent) or if you want performance (parallel).
| Concurrency | Parallelism |
Basic definition | Executing multiple tasks on the same core using overlapping or time slicing. | Executing multiple tasks on different core. |
Goal | Feeling of parallelism without stressing out resources. | Actual parallelism for performance. |
Perspective | Software design: Composition of independently executing computations in a co-operative fashion. | Hardware: Executing computation parallel. |
Resource utilization | Light | Heavy |
- Parallelism is a subset of concurrency.
- Concurrency enables parallelism.
- Concurrency is more about software design while parallelism is more about hardware.
- Concurrency gives an illusion of parallelism while parallelism is about performance.
- Concurrency just needs one core while parallelism needs at least 2 cores.
Here we have something more in video based learning on Concurrency and Parallelism topic do view following 20+ minutes sit back, relax and enjoy full video: -
For Further reading do watch the below interview preparation videos and step by step video series.