Member-only story
Sync vs. Async vs. Concurrent vs. Parallel
A typical interview question about execution
“How do you distinguish between sync vs. async vs. concurrent vs. parallel?”
It’s a question you’ll probably be asked in your first technical interview.
Having witnessed a lot of answers from interviewees, I see that people know the terms, but they rarely understand what they conceptually are.
Knowing use cases are essential. However, just knowing the use cases also limits yourself to only those use cases. That’s why interviewers want to ask you this question — they want to see whether you’re able to introduce solutions for new use cases.
Now, let’s break the code.
Sync vs. Async
Sync and async are two different programming models, which refer to styles of programming, how you should write code, and how your code will run.
In the sync programming model, you write code as steps — your code is executed from top to bottom, step by step, and it only gets to the second step when it has finished the first step.
func step1() { print("1") }
func step2() { print("2") }func main() {
step1()
step2()
}// Result: "12"
Because of its predictable behavior, sync is also called a predictable programming model. Most programming languages use sync as its base programming model.
In an async programming model, you write code as tasks, which are then executed concurrently. Executing concurrently means that all the tasks are likely executed at the same time.
func task1() { print("1") }
func task2() { print("2") }func main() {
task1()
task2()
}// Result: "12" or "21" (not predictable)
As you can see in the result of the example, because tasks are executed at the same time you can not predict the result. And due to that behavior, async is also called an…