Sync VS Async

Hankyu KimHankyu Kim
3 min read

Synchronous

The term "synchronous" means things happening at the same time.

Imagine a scenario where you send a message to a server saying, "I just got connected." The server takes a moment and then responds, "Oh, really? You just got connected! I'll let everyone know."

In this case, your client (you) has to wait for the server's response before doing anything else. This is what we call synchronous communication — you're waiting for both the request and the response to happen one after the other, almost simultaneously.

Let's look at another example: You’re calling your team leader. If they don’t pick up immediately, you hold the phone until they answer. During this wait, you can’t do anything else because you're focused on that call.

In programming terms, this means that the client (your program) is effectively "paused" until the server responds. The client request and server response are tightly linked in time.

Asynchronous

On the other hand, "asynchronous" means things do not occur at the same time.

Let's say you call your team leader again, but this time, instead of waiting, you switch your phone to speaker mode and start playing a game while waiting for them to pick up. Here, you're not just waiting; you’re doing something else while the phone call is pending.

In an asynchronous program, after the client sends a request to the server, it doesn't wait for the response before doing other tasks. The client can continue with its other work and handle the server's response whenever it comes. This way, the client isn’t "paused" and can multitask effectively.

One might think, “Isn’t asynchronous always better than synchronous?” Not necessarily. Each approach has its own use cases and benefits.

Blocking

"Blocking" means halting a program’s execution until an operation is completed.

In synchronous programming, blocking occurs because the program waits for a response before moving on. Using the phone example, you can’t do anything else until your team leader picks up the phone.

When we use socket libraries in programming, the default behavior is blocking. For example, when you use send or recv methods, the program waits until it gets a response before continuing.

Non-blocking

"Non-blocking" means not halting the program's execution even if an operation isn’t completed yet.

In non-blocking scenarios, the program can continue to run other tasks while it waits for a response. Going back to our example, you switch your phone to speaker mode and play a game while waiting for your team leader to answer. You’re not stopped by the wait.

In programming, making sockets non-blocking (using methods like fcntl) allows the send and recv operations to return immediately, regardless of whether they have data to process. If there's no immediate data, the status is recorded, and the program can decide what to do next based on this status.

Understanding these concepts helps you decide when to use each method based on your application's needs.

0
Subscribe to my newsletter

Read articles from Hankyu Kim directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Hankyu Kim
Hankyu Kim

𝘐 𝘩𝘢𝘷𝘦 𝘢 𝘬𝘦𝘦𝘯 𝘪𝘯𝘵𝘦𝘳𝘦𝘴𝘵 𝘢𝘯𝘥 𝘱𝘢𝘴𝘴𝘪𝘰𝘯 𝘧𝘰𝘳 𝘢𝘶𝘵𝘰𝘯𝘰𝘮𝘰𝘶𝘴 𝘥𝘳𝘪𝘷𝘪𝘯𝘨 𝘵𝘦𝘤𝘩𝘯𝘰𝘭𝘰𝘨𝘺. 𝘈𝘶𝘵𝘰𝘯𝘰𝘮𝘰𝘶𝘴 𝘤𝘢𝘳𝘴 𝘰𝘧𝘧𝘦𝘳 𝘭𝘪𝘮𝘪𝘵𝘭𝘦𝘴𝘴 𝘴𝘤𝘦𝘯𝘢𝘳𝘪𝘰𝘴 𝘧𝘰𝘳 𝘤𝘰𝘯𝘷𝘦𝘯𝘪𝘦𝘯𝘤𝘦 𝘢𝘯𝘥 𝘲𝘶𝘢𝘭𝘪𝘵𝘺-𝘰𝘧-𝘭𝘪𝘧𝘦 𝘪𝘮𝘱𝘳𝘰𝘷𝘦𝘮𝘦𝘯𝘵𝘴. 𝘛𝘩𝘦𝘺 𝘤𝘢𝘯 𝘱𝘳𝘰𝘷𝘪𝘥𝘦 𝘪𝘯𝘥𝘦𝘱𝘦𝘯𝘥𝘦𝘯𝘤𝘦 𝘵𝘰 𝘵𝘩𝘦 𝘦𝘭𝘥𝘦𝘳𝘭𝘺 𝘢𝘯𝘥 𝘱𝘩𝘺𝘴𝘪𝘤𝘢𝘭𝘭𝘺 𝘥𝘪𝘴𝘢𝘣𝘭𝘦𝘥 𝘪𝘯𝘥𝘪𝘷𝘪𝘥𝘶𝘢𝘭𝘴. 𝘍𝘰𝘳 𝘪𝘯𝘴𝘵𝘢𝘯𝘤𝘦, 𝘪𝘧 𝘺𝘰𝘶𝘳 𝘤𝘩𝘪𝘭𝘥𝘳𝘦𝘯 𝘧𝘰𝘳𝘨𝘦𝘵 𝘵𝘩𝘦𝘪𝘳 𝘣𝘢𝘵𝘩𝘪𝘯𝘨 𝘴𝘶𝘪𝘵𝘴 𝘢𝘯𝘥 𝘵𝘰𝘰𝘵𝘩𝘣𝘳𝘶𝘴𝘩𝘦𝘴 𝘢𝘵 𝘴𝘶𝘮𝘮𝘦𝘳 𝘤𝘢𝘮𝘱, 𝘵𝘩𝘦 𝘤𝘢𝘳 𝘤𝘰𝘶𝘭𝘥 𝘣𝘳𝘪𝘯𝘨 𝘵𝘩𝘦𝘮 𝘵𝘩𝘦 𝘮𝘪𝘴𝘴𝘪𝘯𝘨 𝘪𝘵𝘦𝘮𝘴. 𝘠𝘰𝘶 𝘤𝘢𝘯 𝘦𝘷𝘦𝘯 𝘴𝘦𝘯𝘥 𝘺𝘰𝘶𝘳 𝘥𝘰𝘨 𝘵𝘰 𝘢 𝘷𝘦𝘵𝘦𝘳𝘪𝘯𝘢𝘳𝘺 𝘢𝘱𝘱𝘰𝘪𝘯𝘵𝘮𝘦𝘯𝘵 𝘶𝘴𝘪𝘯𝘨 𝘢𝘶𝘵𝘰𝘯𝘰𝘮𝘰𝘶𝘴 𝘤𝘢𝘳𝘴. 𝘐 𝘢𝘮 𝘱𝘢𝘴𝘴𝘪𝘰𝘯𝘢𝘵𝘦 𝘢𝘣𝘰𝘶𝘵 𝘤𝘰𝘯𝘵𝘳𝘪𝘣𝘶𝘵𝘪𝘯𝘨 𝘵𝘰 𝘵𝘩𝘦 𝘳𝘦𝘴𝘦𝘢𝘳𝘤𝘩 𝘢𝘯𝘥 𝘥𝘦𝘷𝘦𝘭𝘰𝘱𝘮𝘦𝘯𝘵 𝘰𝘧 𝘢𝘶𝘵𝘰𝘯𝘰𝘮𝘰𝘶𝘴 𝘥𝘳𝘪𝘷𝘪𝘯𝘨 𝘵𝘦𝘤𝘩𝘯𝘰𝘭𝘰𝘨𝘺, 𝘬𝘦𝘦𝘱𝘪𝘯𝘨 𝘪𝘯 𝘮𝘪𝘯𝘥 𝘵𝘩𝘦 𝘱𝘰𝘴𝘴𝘪𝘣𝘪𝘭𝘪𝘵𝘪𝘦𝘴 𝘪𝘵 𝘩𝘰𝘭𝘥𝘴. 𝘞𝘪𝘵𝘩 𝘮𝘺 𝘴𝘬𝘪𝘭𝘭𝘴 𝘢𝘯𝘥 𝘬𝘯𝘰𝘸𝘭𝘦𝘥𝘨𝘦, 𝘐 𝘢𝘪𝘮 𝘵𝘰 𝘤𝘰𝘯𝘵𝘳𝘪𝘣𝘶𝘵𝘦 𝘵𝘰 𝘢 𝘴𝘶𝘴𝘵𝘢𝘪𝘯𝘢𝘣𝘭𝘦 𝘧𝘶𝘵𝘶𝘳𝘦 𝘵𝘳𝘢𝘯𝘴𝘱𝘰𝘳𝘵𝘢𝘵𝘪𝘰𝘯 𝘴𝘺𝘴𝘵𝘦𝘮. 𝘐 𝘢𝘮 𝘤𝘶𝘳𝘳𝘦𝘯𝘵𝘭𝘺 𝘸𝘰𝘳𝘬𝘪𝘯𝘨 𝘪𝘯 𝘢𝘶𝘵𝘰𝘯𝘰𝘮𝘰𝘶𝘴 𝘥𝘳𝘪𝘷𝘪𝘯𝘨 𝘴𝘪𝘮𝘶𝘭𝘢𝘵𝘰𝘳 𝘤𝘰𝘮𝘱𝘢𝘯𝘺 𝑴𝑶𝑹𝑨𝑰. 𝘗𝘭𝘦𝘢𝘴𝘦 𝘧𝘦𝘦𝘭 𝘧𝘳𝘦𝘦 𝘵𝘰 𝘮𝘦𝘴𝘴𝘢𝘨𝘦 𝘮𝘦. 𝘐'𝘮 𝘭𝘰𝘰𝘬𝘪𝘯𝘨 𝘧𝘰𝘳𝘸𝘢𝘳𝘥 𝘵𝘰 𝘴𝘩𝘢𝘳𝘦 𝘢𝘯𝘥 𝘤𝘰𝘯𝘯𝘦𝘤𝘵. 𝘛𝘩𝘢𝘯𝘬 𝘺𝘰𝘶! 𝘏𝘢𝘯𝘬𝘺𝘶 𝘒𝘪𝘮