Recently I’ve been looking into message passing as a model for concurrency. It turns out that there are two warring camps in the message passing community. One believes that synchronous message passing is more fundamental, the other believes that asynchronous message passing is more basic. You might think that the discussion is moot, since one can be emulated by the other, but it’s not as simple as that.
Let me first explain the concepts. In message passing you have a sender and a receiver–they usually run in separate threads. The sender sends a message and the receiver receives it. All issues of memory sharing and concurrent access are hidden inside the communication channel. The client does no low level synchronization actions like locking. Which is a good thing–no low level races or deadlocks!
The receiver usually has a choice of synchronous or asynchronous access. It can block until a message is available, or it can peek to see if there is a message waiting. Usually both interfaces are available.
The major choice of paradigms is on the sender’s side.
- In the synchronous model, the sender blocks until the receiver picks up the message. The two have to rendezvous.
- In the asynchronous model, the sender drops the message and continues with its own business.
The synchronous model is used, among others, in CSP (Communicating Sequential Processes) and CML (Concurrent ML). The asynchronous one is used in Concurrent Haskell and in actor-based languages like Erlang or Scala.
Here’s the main argument of the synchronous camp: After calling send the sender has the guarantee that the message has been received by the receiver. The code after send may safely make this assumption. If you’re a believer in RPC (Remote Procedure Call) you’ll love this. Sending a message is just like making a function call, only the work is done in a separate thread.
To which the asynchronous camp answers: Yeah, but what about distribution? In a distributed system, the receiver may be in a different process on a different machine. There may be many reasons why the message doesn’t arrive at the recipient (the recipient is dead? a bulldozer cut the network cable?). In that case the sender will hang forever. The code after send may still assume safe delivery, but it will never be executed.
There is another, more subtle problem with synchronous message passing between machines–a network protocol might deliver messages in different order than they were sent. If the receiver’s code expects a certain sequence of messages, it might block forever if the messages are permuted.
All these problems may be overcome by queuing messages, building sophisticated protocols, and spawning helper threads. But is it really worth the trouble?
Yes, say the synchronous camp, if you want to formally prove the correctness of your programs. With synchronous message passing you can use the theoretical machinery of C.A.R Hoare’s CSP. That’s an important advantage if you are writing your Ph.D. thesis, but maybe less so if you’re maintaining millions of lines of Erlang code.
For my next installment I’m already working on translating Haskell’s MVar’s to Java. This will be fun!
You may vote on this article on reddit.
February 10, 2009 at 11:54 pm
Seriously why does this matter? Sync and async are well known prototypical mechanisms.
What do erlang and even messaging have to do with it?
We have an entire industry which has no way to effectively code a future 50-80 core processor with our existing programming languages and you are hung up on sync v async messaging?
Pls. finish your thesis.
February 11, 2009 at 3:25 am
IMO, your argument is rather biased. First of all, CML provides both synchronous and asynchronous message passing primitives. Asynchronous message passing often requires sending additional ACK messages and opens the room for issues such as filling buffers when the producer is faster than the consumer. Solving these and other problems tends to make programs using asynchronous message passing more complicated. Also, CML does not intend or pretend to solve distributed computing. Like you say, and is briefly discussed in the CML book, there are many more points of failure when computation is distributed. Effective distribution requires much more than just asynchronous message passing. For programming multi-core machines, synchronous message passing should be just fine, tends to lead to simpler programs and is easier to debug and reason about. Finally, CML does support timeouts.
February 11, 2009 at 4:02 am
Sync vs. Async message passing is not really a problem. CML contains a pretty nice primitive to avoid the sender hanging (see channels) but granted, you have to know where to use it and where you can make a synchronous rendezvous.
obo: We do not have that much of a problem up to some 32 cores or so. After that it gets a bit more murky, but we probably wont have many problems for a large set of software. There are some cases in which it will be hard to utilize all cores, but these problems are not that prevalent as one might think.
You may see a split in those programmers attacking the hard-to-parallelize problems and the easier problems, but that is arguably nothing new. You already split programmers into specializations anyway.
February 11, 2009 at 9:49 am
A great tool built in java for simulating all the schemes is http://ptolemy.eecs.berkeley.edu/
February 11, 2009 at 9:51 am
Last version of Ptolemy: http://ptolemy.eecs.berkeley.edu/ptolemyII/ptII7.0/index.htm
February 12, 2009 at 5:03 pm
[…] Message Passing–Sync or Async? Recently I’ve been looking into message passing as a model for concurrency. It turns out that there are two […] […]
February 14, 2009 at 12:14 pm
[…] level primitive that can be used to build a concurrent message passing system? As I discussed in my previous post, there are two message passing paradigms, synchronous and […]
March 3, 2009 at 9:15 pm
“What do erlang and even messaging have to do with it?”
Well, the post is about messaging, right? And Erlang uses async message passing, not sure why that isn’t clear. And further, with an Erlang-based web server having considerably more scalability that, say, an Apache (sync) web server, I think, yes, this is an important argument.
And some of the newer technologies, like WebSphere XD, use queuing systems to support SLAs, for example, so yes, worth the trouble it seems.
Or think of products like MQSeries, which support async messaging, and, at least to me, without the fanfare of web services, or CORBA, helped many companies toward interoperability – and still do. Because of the exact reasons explained here in this post – what if your web service provider goes down?
Now, web services do support other transports besides http, for example MQ, but this is not the normal implementation of course, amazingly. Though there was talk of HTTP-R for a while..
Anyway, thanks for the post, look forward to the Haskell post..
March 5, 2009 at 4:09 pm
This article might be of interest. Gives an intro to Erlang, discusses how the messaging works, some examples of it’s stunning performance, and it’s use in some new apps, such as Facebook Chat.
“The prospect of multicore systems of this magnitude threatens the viability of the shared-state concurrency model. This article introduces you to a robust alternative — the actor concurrency model — and explains how it is implemented in a 20+-year-old yet increasingly relevant functional language: Erlang.”
http://www.javaworld.com/javaworld/jw-02-2009/jw-02-actor-concurrency1.html?page=4
March 5, 2009 at 4:32 pm
I was going to blog about actor systems: Erlang and Scala in particular. It’s in my queue.
July 7, 2009 at 6:47 pm
Is Message passing a synchrnization tool???
July 7, 2009 at 6:50 pm
Yes, message passing is a synchronization tool.
February 1, 2010 at 4:36 am
A great explanation. useful for students.
thx mate
March 21, 2010 at 5:04 am
Nice post. Thanks for it.
I’m trying to compare using Sync and Async message passing in Web services.
Is sync message-passing used with Web services?
Any pointers, will be very appreciated.
Marica.
March 21, 2010 at 10:58 am
I’m not very familiar with Web services. My understanding is that they are built on top of the HTTP protocol. The basic communication pattern is: the client sends a request to the server and waits for a response. So this is a synchronous protocol.
In the asynchronous protocol the client would send a request and immediately continue with other chores. It would eventually be notified when the response arrives and have a special handler to process it.
May 22, 2011 at 7:35 am
[…] · “Message Passing–Sync or Async?”, Bartosz milewski, https://bartoszmilewski.wordpress.com/2009/02/10/message-passing-sync-or-async/ […]