Synchronous vs. Asynchronous Communications

COMP 310  Java Resources  Eclipse Resources

When communicating between two separated entities, e.g. between machines across a network, there are often two choices on how that communication can be accomplished: 

 

 

Pros

Cons

Synchronous:

  • Simple, intuitive behavior
  • Easy technique for retrieving values from a remote entity.
    • Well-defined behavior: either the value is returned or the system throws an error from excessive waiting.
  • Easy to get transmission status information via the returned value.
  • Relatively easy to maintain state while processing multiple communications operations.
  • Sender and receiver are fundamentally coupled to each other.
    • This coupling reduces the scalability of the system.
    • This reduces the robustness of the system because the performance of one part of the system depends on the performance of another part.
  • Requires blocking of sender's execution thread waiting for remote operation to complete.
    • Can result in degraded system performance on the sender side if the transmission speeds are slow, inconsistent or unreliable.
      • Favors high-speed, low-latency communications.
    • Can effectively be turned into an asynchronous operation by dispatching to another thread to wait for and then finally process the returned value.

Asynchronous:

  • Decouples the sender from the receiver.
    • Communications can be set up as a simple "fire-and-forget" processing.
    • Minimized coupling enables easier scaling of the system.
    • Systems are more robust because the system's separated parts are less dependent on each other.
    • Enables asychronous updating of data at any time without any action from the receiving side, e.g. polling.
  • The sender side is unaffected by transmission speed issues.
  • No threading issues on the sender side.
  • Enables asychronous alerting, notifications and messaging without any additional infrastructure.
  • Retrieving values from a remote entity requires sending a request message and then setting up a receiving process that the remote entity can use to asynchronously send the requested value back.
    • No guarantee that the requested value will ever be sent.
    • More difficult to get transmission status information because it must be returned via another asynchronous operation and the original message must be referenceable in some manner.
  • Data caching of some sort required to hold state across multiple communications operations.
    • Favors stateless systems.
     

The choice of communications technique depends on the needs of the system.  For example:

  Typically characterized by: Common communications solution choice:
Small systems
  • High speed, low latency communications
  • Lower complexity levels - can tolerate some level of system coupling.
Synchronous
Large systems
  • Low speed communications (relative to small systems)
  • High complexity levels - system decoupling is a high priority.
  • Stateless components for greater scalability.
  • Greater flexibility and evolvability needs.
Asynchronous

 

Asynchronous Messaging != async response handling

A very common misconception is to confuse an asynchronous messaging architecture with a multi-threaded "asynchronous" response handling of a request.

For instance, many libraries for HTTP request processing, e.g. JavaScript, enable the handling of the response to an HTTP request (e.g. GET, POST, etc) using a lambda function or similar that runs on a separate thread from the request itself. This technique is very useful for preventing the locking of the calling thread waiting for the response from a remote system. Because the "asynchronous" descriptor is typically applied to this technique, people often confuse it with real asynchronous messaging architecture when it is actually quite different.

Async response handling:

A request-response processing coding technique where the response to a request runs on a "different" thread (conceptually in JavaScript since it is single threaded).

Asynchronous messaging:

A communications architecture where data is transmitted and received independently from requests.

 

The Myth of "Asynch is Too Hard"

Unfortunately, a very common reason for not using an asynchronous architecture is simply misconceptions about the difficulties of creating such a system.

The biggest hurdle for most people is changing the way in which they see how information is passed in the system.

In particular, the key notion is to realize that the processes of requesting data (the code to make the function call to retrieve the data) is fundamentally decoupled from the processing the received data (the code that processes the returned result).

Also, in any system where the response time of a synchronous call is potentially signficant, the synchronous operation must be structured using some sort of asynchronous callback (e.g. a JavaScript async construct or a callback lambda) so as not to lock the calling threads and to reduce the impact on system resources. For these reasons, most libraries for handling synchronous network calls such as REST endpoints are fundamentally set up this way. That is,

in properly constructed synchronous operations, the processing is ALREADY configured as an asynchronous architecture!

In the end, the major difference between synchronous and asynchronous architectures is WHERE the calling and receiving code is located. In an asynchronous system, the calling code ("requests") is simply physically separated from the receiving code.

The physical separation of the requests for data and the processing of received data has the added benefit of clarifying the multi-threaded (possibly multi-process) nature of the operations. The same issues are present in synchronous systems but the tighter coupling makes it much easier for developers to ignore or forget to properly deal with critical shared-data issues.

Asynchronous processing has a host of advantages and benefits for larger systems or systems that intend to grow. Switching from synchronous to asynchronous processing in an established system can be difficult and costly due to tight coupling that synchronous architectures naturally incur. But designing the system from the get go for asynchronous processing is not at all difficult and well worth any efforts needed to change the way one sees how information is being processed.

Don't fear the async!

 

 

 

© 2024 by Stephen Wong