Concurrency vs Parallelism – The Microwave Analogy

Many people confuse the terms concurrent with parallel, often interchanging them with each other to mean the same thing. However, they are in fact not the same as they have a subtle, but very important difference that completely changes the way we should think of these concepts. This difference is especially important in the way we think about computing and processing efficiency.

To better illustrate this, let me provide an analogy. Suppose we have a microwave and 2 containers of food. The microwave can only heat up 1 container at any given time. Each container needs to be microwaved for a total of 1 minute each, but at the 30 second mark will need to be taken out and stirred for at least 15 seconds before being microwaved again.

Solution 1:

Let’s solve this problem in the simplest way first:

  1. Microwave the first container for 30 seconds.
  2. Take out the first container for 15 seconds and stir it.
  3. Microwave the first container for another 30 seconds.
  4. Take out the first container and microwave the second container for 30 seconds.
  5. Take out the second container for 15 seconds and stir it.
  6. Microwave the second container for another 30 seconds.

This took a total of 2 minutes and 30 seconds.

Now, you may notice that there is some inefficiency here. While the containers were being stirred, the microwave was sitting idly and no progress was being made.

In this first solution, no concurrency nor parallelism was being used.

Solution 2, Microwave Concurrency:

Now let’s look at a second solution that is a bit more efficient:

  1. Microwave the first container for 30 seconds.
  2. Take out the first container and stir it for 15 seconds.
  3. While the first container is being stirred, put in the second container for 30 seconds.
  4. Take out the second container and stir it for 15 seconds and microwave the first container for 30 seconds at the same time.
  5. Take out the first container and microwave the second container for the remaining 30 seconds.

The total time here was 2 minutes. We saved 30 seconds from the first solution!

What we see here is that the microwave was fully utilized during this time and no waiting was occurring. Even though the first container was not fully finished being heated, we used the microwave on the second container right away.

This illustrates concurrency – making progress in multiple tasks in a given time interval (but not necessarily at the exact same time).

However, this does not illustrate parallelism, which we’ll talk about below.

Solution 3: Microwave Parallelism

Continuing on with our analogy, suppose we add another microwave. Now we’re able to microwave food containers at the exact same time! Let’s see how this speeds things up.

  1. Microwave 1 heats up container 1 and Microwave 2 heats up container 2 for 30 seconds.
  2. Both containers are taken out to be stirred for 15 seconds.
  3. Both containers are heat up again for another 30 seconds.

This is all done in 1 minute and 15 seconds! We just saved 45 seconds from the concurrency solution.

This is due to the fact that we were able to heat up containers at the exact same time – in other words, we were able to utilize parallelism!

You may notice that this solution’s time is actually Solution 1’s time divided by 2. This is the case because we’re actually just using Solution 1 on two different microwaves at the same time!

So what exactly is parallelism? Parallelism is making progress in multiple tasks at the same exact time.

In this case, we have 2 separate microwaves that are heating up containers at the exact same time, which utilized parallelism!

Bonus: Microwave Parallelism and Concurrency

Suppose now that we still have 2 microwaves but now we have 4 food containers that we have to heat up. Now, we can see how we can utilize both parallelism and concurrency in order to speed up our heating.

  1. Microwave 1 heats up container 1 and Microwave 2 heats up container 2 for 30 seconds.
  2. Both containers are taken out to be stirred for 15 seconds. Microwave 1 starts heating up container 3 and Microwave 2 starts heating up container 4 for 30 seconds.
  3. Containers 3 and 4 are taken out to be stirred and Containers 1 and 2 are put back into Microwave 1 and 2 respectively for 30 seconds.
  4. Containers 1 and 2 are removed from the microwaves and Containers 3 and 4 are put back into the microwaves for another 30 seconds.

In total, it took 2 minutes to heat 4 food containers! This amounts to an average of 30 seconds per food container.

If we look at Solution 3, it took 1 minute and 15 seconds for 2 food containers – an average of 37.5 seconds per food container.

So despite the overall time to heat the containers taking longer, the efficiency per container is better since we were able to utilize both parallelism and concurrency!

Summary

Concurrency is making progress in multiple tasks in a given time interval (but not necessarily at the exact same time).

Parallelism is making progress in multiple tasks at the same exact time.

SolutionConceptNumber of MicrowavesNumber of Food ContainersTotal TimeTime per Food Container
1None12150 seconds75 seconds
2Concurrency12120 seconds60 seconds
3Parallelism2275 seconds37.5 seconds
BonusConcurrency and Parallelism24120 seconds30 seconds

In the analogy, the microwave is a processor (a CPU, central processing unit, for a computer) while the food container is a unit of work.

By thinking with this analogy, we can now understand why a single processor can never execute work in parallel, but it can execute work concurrently. A computer with a single CPU seems like it is doing many things at once such as running multiple programs at the same time. However, this is actually just an illusion as computers can process data at very fast speeds. In fact, there is a name for this – called multiprogramming – where the CPU is used concurrently between multiple programs. Pretty much all computers nowadays have a multitude of CPU cores now though that take advantage of both concurrency and parallelism in ways that are far beyond this post can cover.