Tag related to Parallel Computing

Parallel vs Asynchronous: Understanding the Difference in C# (Using Bread Making)

When working with C#, developers often face two important concepts:

  • Asynchronous programming (async/await) – Best for waiting on external tasks (e.g., network requests, file operations).
  • Parallel programming (Parallel.ForEach) – Best for executing multiple tasks simultaneously (e.g., CPU-heavy computations).

To explain this, let’s use the example of baking bread.

Asynchronous Programming (async/await) – A Single Baker Handling Tasks Efficiently

Imagine you are baking bread alone. The process involves several steps:

  1. Mix the ingredients → You wait for the dough to come together.
  2. Proof the dough → You wait for it to rise.
  3. Shape the dough → You actively work on it.
  4. Bake the bread → You wait for it to cook.

Each step has periods of waiting. Instead of just standing around, you do other work—like washing dishes or preparing another meal—while waiting.

One Use Case for TPL

TPL is a set of libraries from Microsoft that can be used for parallel processing, basically it is a set of high level tools that can make your code run in parallel. TPL is not a panacea for parallel processing, mainly because you don’t always need parallel processing, but when you need it, it can be a life-saver.

The system

The system was built with a simple purpose, to consume and process events delivered by Kafka. The events were published, in Avro format, by a separate system, let’s call it producer. Essentially the events represent a course of actions, which are used to produce our model. Eventually that model is publicly exposed through an API (not HTTP). Everything was built using C# on dotnet core and ran on a docker container on linux hosts.