What mac donald Can Teach You About Concurrency and Parallelism

Alok
5 min readDec 5, 2020
Image source — Daily Mail. Fast food lovers queue at McDonalds before UK shutdown

If you feel like impressing or scaring the candidate in a technical interview (NEVER DO IT) then this question may top the lists.

“What is the difference between concurrent execution and parallel execution”.

There is something about concurrency and parallelism concept. It just doesn’t lands in the initial learning. Every revisit of the topic gives you a false feeling of clarity and with a new twisted scenario, the confusion surges its ugly head back.

I don’t belong to that elite developer group who wrote games at age of 10 or wrote a new operating system before they hit 15. Hence, i used to blame myself for struggling to understand the concurrency and parallelism. Its like maths, if you struggle with calculus then you don’t blame the literature but yourself. I eventually got the idea/concept and interestingly it came via stories and metaphors. I recently had to explain the concept to a college fresher and the metaphors made the concepts feel like a breeze. Seeing the happiness that comes with clarity, propelled me to write about it. If you already know the metaphors or stories then may be this post would work as a nudge to remember the powerful Einstein quote

If you can’t explain it simply you don’t understand it well enough.

So, here goes the metaphors for concurrency and parallelism that i have copied shamelessly from FastAPI (FastAPI is a modern, fast (high-performance), web framework for building APIs with Python).

Concurrent Burgers

  1. You go with your crush 😍 to get fast food 🍔, you stand in line while the cashier 💁 takes the orders from the people in front of you.
  2. Then it’s your turn, you place your order of 2 very fancy burgers 🍔 for your crush 😍 and you.
  3. You pay 💸.
  4. The cashier 💁 says something to the guy in the kitchen 👨‍🍳 so he knows he has to prepare your burgers 🍔 (even though he is currently preparing the ones for the previous clients).
  5. The cashier 💁 gives you the number of your turn.
  6. While you are waiting, you go with your crush 😍 and pick a table, you sit and talk with your crush 😍 for a long time (as your burgers are very fancy and take some time to prepare ✨🍔✨).
  7. As you are sitting on the table with your crush 😍, while you wait for the burgers 🍔, you can spend that time admiring how awesome, cute and smart your crush is ✨😍✨.
  8. While waiting and talking to your crush 😍, from time to time, you check the number displayed on the counter to see if it’s your turn already.
  9. Then at some point, it finally is your turn. You go to the counter, get your burgers 🍔 and come back to the table.
  10. You and your crush 😍 eat the burgers 🍔 and have a nice time ✨.

Now replace yourself with a computer program and treat your crush as another program. Since you are waiting for your resource request to complete, you get off the line giving way to others to get the processor time (or attention). This also reminds of the famous hollywood principle — “Don’t call us, we’ll call you” that you would have seen in object oriented design patterns literature.

Parallel Burgers

You go with your crush 😍 to get parallel fast food 🍔.

  1. You stand in line while several (let’s say 8) cashiers that at the same time are cooks 👨‍🍳👨‍🍳👨‍🍳👨‍🍳👨‍🍳👨‍🍳👨‍🍳👨‍🍳 take the orders from the people in front of you.
  2. Everyone before you is waiting 🕙 for their burgers 🍔 to be ready before leaving the counter because each of the 8 cashiers goes himself and prepares the burger right away before getting the next order.
  3. Then it’s finally your turn, you place your order of 2 very fancy burgers 🍔 for your crush 😍 and you.
  4. You pay 💸.
  5. The cashier goes to the kitchen 👨‍🍳.
  6. You wait, standing in front of the counter 🕙, so that no one else takes your burgers 🍔 before you do, as there are no numbers for turns.
  7. As you and your crush 😍 are busy not letting anyone get in front of you and take your burgers whenever they arrive 🕙, you cannot pay attention to your crush 😞.
  8. This is “synchronous” work, you are “synchronized” with the cashier/cook 👨‍🍳. You have to wait 🕙 and be there at the exact moment that the cashier/cook 👨‍🍳 finishes the burgers 🍔 and gives them to you, or otherwise, someone else might take them.
  9. Then your cashier/cook 👨‍🍳 finally comes back with your burgers 🍔, after a long time waiting 🕙 there in front of the counter.
  10. You take your burgers 🍔 and go to the table with your crush 😍.
  11. You just eat them, and you are done 🍔 ⏹.There was not much talk or flirting as most of the time was spent waiting 🕙 in front of the counter 😞.

Now imagine, you are a computer / program 🤖 with two processors (you and your crush 😍), both waiting 🕙 and dedicating their attention ⏯ to be “waiting on the counter” 🕙 for a long time.

So, is concurrency better than parallelism (Another question to show off your superior interviewer IQ )? No, they are just different!! Whether good or bad, will depend on the context. The two scenarios also highlight an interesting pattern — you can combine them. You start with few queues and all of them working asynchronously. Btw, as no surprise, this is how we buy burgers — search the shortest queue and wait for your turn. Though somehow the other queue always seems to move faster. Sorry, i digressed.

The current form of teaching code and coding is too focussed on technicality. I wonder, how many really understood the value or real world applications of derivatives(calculus) or algebra during school time. Now, ask the same questions to a ML practitioner and they can talk all day about the value of it.

To get good at algorithms requires getting good at thinking

Conclusion

In order to learn and apply computational paradigm effectively, It is essential to build a clear mental model. This process can often be facilitated effectively by using suitable metaphors. I know, i started with concurrency/parallelism and ending with extolling the values of metaphors but i guess you got the point — they are inseparable.

--

--

Alok

Certified Solution architect | Experience technologist | Speaker | Developer. Views and opinions are personal