Skip to content

Musings on Messaging

Date: 2011-04-14

Summary: Object interaction is fundamental irrespective of programming paradigm. Some reflections on styles.

Introduction

Interaction is a central tenet of the programming. It's perhaps most obviously associated with object orientation, but it goes much wider: it's equally applicable in functional and procedural styles too. It's also central to concurrency approaches such as the Actor model and Communicating Sequential Processes (CSP). Tasks are accomplished by communication, with objects conceptually sending messages to each other.

There are (at least) two interesting but separable aspects to object interaction:

  • The message passing mechanism, and
  • The semantics of the messages themselves.

Message Passing Mechanisms

In all traditional OO languages (Java, Python, C++, Smalltalk, C#, ...) "message passing" is implemented in-process by synchonously invoking a method call. That mechanism served its purpose relatively well in the single processor era. But as we embrace the multi-core future it looks increasingly strained.

There are alternatives. Several implementations of the Actor model exist, notably in Erlang and more recently Scala. The Actor model provides a different approach. Actors are independent processes that communicate by asynchronous message passing. The Actor model can scale more easily on multi-core systems; Actor instances can be partitioned across cores, with intra- and inter-core messaging as required.

In some ways the Actor model is a more accurate realisation of the OO conceptual model than the synchronous incumbent. The OO model talks of objects as independent, active entities communicating by messages. Conventional OO languages break that independence since threads of execution run across objects. In doing so they break another fundamental OO tenet: encapsulation. That's part of the reason why multi-threaded programming in traditional languages is extremely hard to get right. With multiple threads, the developer has to build a mental picture of parallel threads criss-crossing the object structure, often with more than one thread passing through the same object (instance) at the same time. Hence the need for locking to prevent contention and the attendant problems that brings (deadlock, synchronisation).

In contrast, the Actor model aligns execution threads with the Actors (objects) not the processes. Each Actor has its own input queue. Any other Actor can deposit a message on the Actor's input queue (provided the sender knows how to find the receiver of course). It's then up to the receiver to pick messages off the queue and react as appropriate.

The Actor model is conceptually much easier to deal with than a conventional, multi-threaded approach. It also has the advantage of solid theoretical foundations - both of the model itself (notably by its originator Carl Hewitt and Gul Agha who did much to formalise it). Djikstra's closely related Communicating Sequential Processes takes a slightly different approach but shares the concept of insulated processes, running concurrently, and interating via message passing. Finally, in Erlang, it has the advantage of being tested and found effective in real, live, large-scale deployments.

Kinds of Message - Requests and Notifications

There are two types of messages that can pass between objects. Requests are the most familiar - when one object asks another to do something on its behalf. This is supported in pretty much all languages, whether traditional or Actor based. Sending requests is the primary way we accomplish interaction.

However, there is another, less popular kind of message: the notification. Variously also known as Events, Indications, Signals (and probably a few others), Notifications simply highlight that something has happened.

Though less popular - at least at the language level - Notifications are an extremely effective mechanism for enabling good encapsulation. They have found some traction in GUI programming where they enable a clean way to link together stock components when constructing a GUI from pre-supplied parts.

Take an example, where a TextInput component raises a TextChanged() event each time its text is edited. The TextInput component has no idea which other components listen for that event, far less what they might do on receiving it. Put another way, all the application-level semantics are encapsulated in the wiring of event publishers to event subscribers.

Of course we still need Requests. The scenario whereby one object explicitly sends a request to another still applies. But adding first class support for Notifications provides a richer - yet simpler and better encapsulated - model.

A Messaging Manifesto

It would be great to see a language that provided first class support for both Requests and Notifications, layered on top of an asynchronous, Actor/CSP-like threading model. There are many more challenges and opportunities arising from the multi-core future. However basing a language around these principles would provide a substrate that was simultaneously easy to use, conceptually clean and simple, and not disruptive to the programming mass market.