A place to keep my thoughts on programming

January 23, 2011 .net, mindtouch, mono , , , ,

Type-safe actor messaging approaches

For I hand-rolled a simple actor system to handle all Xmpp traffic. Every user in the system has its own actor that maintains their xmpp state, tracking online status, resources, resource capability, notification queues and command capabilities. When a message comes in either via our internal notification queues or from the user, a simple dispatcher sends the message on to the actor which handles the message and responds via a message that the dispatcher either hands off to the Xmpp bot for formatting and delivery to the client or sends it to our internal queues for propagation to other parts of the system.

This has worked flawlessly for over 2 years now, but its ad-hoc nature means it's a fairly high touch system in terms of extensibility. This has led me down building a more general actor system. Originally Xmpp was our backbone transport among actors in the system, but at this point, I would like to use Xmpp only as an edge transport, and otherwise use in-process mailboxes and serialize via protobuf for remote actors. I still love the Xmpp model for distributing work, since nodes can just come up anywhere, sign into a chatroom and report for work. You get broadcast, online monitoring, point-to-point messaging, etc. all for free. But it means all messages go across the xmpp backbone, which has a bit of overhead and with thousands of actors, i'd rather stay in process when possible. No point going out to the xmpp server and back just to talk to the actor next to you. I will likely still use Xmpp for Actor Host nodes to discover each other, but the actual inter-node communication will be direct Http-RPC (no, it's not RESTful, if it's just messaging).

Definining the messaging contract as an Interface

One design approach I'm currently playing with is using actors that expose their contract via an interface. Keeping the share-nothing philosophy of traditional actors, you still won't have a reference to an actor, but since you know its type, you know exactly what capabilities it has. That means rather than having a single receive point on the actor and making it responsible for routing the message internally based on message type (a capability that lends itself better to composition), messages can arrive directly at their endpoints by signature. Another benefit is that testing the actor behavior is separate from its routing rules.

public interface IXmppAgent {
    void Notify(string subject, string body);
    OnlineStatus QueryStatus();

Given this contract we could just proxy the calls. So our mailbox could have a proxy factory like this:

public interface IMailbox {
    TRecipient For<TRecipient>(string id);

allowing us to send messages like this:

var proxy = _mailbox.For<IXmppAgent>("");
proxy.Notify("hey", "how'd you like that?");
var status = proxy.QueryStatus();

But messaging is supposed to be asynchronous

While this is simple and decoupled, it is implictly synchronous. Sure .Notify could be considered a fire-and-forget message, .QueryStatus definitely blocks. And if we wanted to communicate an error condition like not finding the recipient, we'd have to do it as an exception, moving errors into the synchronous pipeline as well. In order to retain the flexibility of a pure message architecture, we need a result handle that let's us handle results and/or errors via continuation.

My first pass at an API for this resulted in this calling convention:

public interface IMailbox {
    void Send<TRecipient>(string id, Expression<Action<TRecipient>> message);
    Result SendAndReceive<TRecipient>(string id, Expression<Action<TRecipient>>  message);
    Result<TResponse> SendAndReceive<TRecipient, TResponse>(
        string id,
        Expression<Func<TRecipient, TResponse>>  message

transforming the messaging code to this:

_mailbox.Send<IXmppAgent>("",a => a.Notify("hey", "how'd you like that?"));
var result = _mailbox.SendAndReceive<IXmppAgent, OnlineStatus>(
    a => a.QueryStatus()

I'm using MindTouch Dream's Result<T> class here, instead of Task<T>, primarily because it's battle tested and I have not properly tested Task under mono yet, which is where this code has to run. In this API, .Send is meant for fire-and-forget style messaging while .SendAndReceive provides a result handle — and if Void were an actual Type, we could have dispensed with the overload. The result handle has the benefit of letting us choose how we want to deal with the asynchronous response. We could simply block:

var status = _mailbox.SendAndReceive<IXmppAgent, OnlineStatus>(
        a => a.QueryStatus())
Console.WriteLine(" status:", status);

or we could attach a continuation to handle it out of band of the current execution flow:

_mailbox.SendAndReceive<IXmppAgent, OnlineStatus>(
        a => a.QueryStatus()
    .WhenDone(r => {
        var status = r.Value;
        Console.WriteLine(" status:", status);

or we could simply suspend our current execution flow, by invoking it from a coroutine:

var status = OnlineStatus.Offline;
yield return _mailbox.SendAndReceive<IXmppAgent, OnlineStatus>(
        a => a.QueryStatus()
    .Set(x => status = x);
Console.WriteLine(" status:", status);

Regardless of completion strategy, we have decoupled the handling of the result and error conditions from the message recipient's behavior, which is the true goal of the message passing decoupling of the actor system.

Improving usability

Looking at the signatures there are two things we can still improve:

  1. If we send a lot of messages to the same recipient, the syntax is a bit repetitive and verbose
  2. Because we need to specify the recipient type, we also have to specify the return value type, even though it should be inferable

We can address both of these, by providing a factory method for a typed mailbox:

public interface IMailbox {
    IMailbox<TRecipient> To<TRecipient>(string id);

public interface IMailbox<TRecipient> {
    void Send(Expression<Action<TRecipient>> message);
    Result SendAndReceive<TResponse>(Expression<Action<TRecipient>>  message);
    Result<TResponse> SendAndReceive<TResponse>(
        Expression<Func<TRecipient, TResponse>>  message

which let's us change our messaging to:

var actorMailbox = _mailbox.To<IXmppAgent>("");
actorMailbox.Send(a => a.Notify("hey", "how'd you like that?"));
var result2 = actorMailbox.SendAndReceive(a => a.QueryStatus());

// or inline
    .Send(a => a.Notify("hey", "how'd you like that?"));
var result3 = _mailbox.To<IXmppAgent>("")
    .SendAndReceive(a => a.QueryStatus());

I've included the inline version because it is still more compact than the explicit version, since it can infer the result type.

Supporting Remote Actors

The reason the mailbox uses Expression instead of raw Action and Func is that at any point an actor we're sending a message to could be remote. The moment we cross process boundaries, we need to serialize the message. That means we need to be able to programatically inspect the inspection, and build a serializable AST as well as serialize the captured data members used in the expression.

Since we're talking serializing, inspecting the expression also allows us to verify that all members are immutable. For value types, this is easy enough, but DTOs would need be prevented from changing so that local vs. remote invocation won't end up with different result just because the sender changed it's copy. We could handle this via serialization at message send time, although this looks like a perfect place to see how well the Freezable pattern works.

3 to “Type-safe actor messaging approaches”


  1. […] Type-safe actor messaging approaches […]

  1. bjorg says...

    It looks like you're effectively turning method definitions into message definitions.  That's a brilliant approach.  I could see this work without the actor ever implementing the actual interface, but instead by listening on the mailbox with compatible-type callback.  Somewhat similar in how Mock works.  However, there are some details missing that need clarification:

    Does the response of a SendAndReceive() operation get dispatched on the same mailbox?  If so, how is that mailbox discovered?  If not, how do you prevent a message from the mailbox being processed while the continuation of a SendAndReceive fires?
    Similarly, if an actor is asynchronously processing message A and is suspended by doing an async operation, can the actor read another message from the inbox or does the actor need to wait until processing of message A is completed?

    Concerning the double duty pulled by the void type–in one case, it's a signal without data and in another it indicates the lack of a continuation–I would recommend settling on one purpose and introducing a special type for the other.  For example, void means signal w/o data (isomorphic with other built-in types) and Unobservable for something that well, can't be observed when it completes. 🙂

  2. arne says...


    My primary reason for using interfaces rather than just dispatching against a classes methods in Moq style is that for remote actors i could share an interface assembly without ever having to share the implementation assembly. That said, this messaging pipeline should work just as well with concrete types as recipients.

    I'm still working out the mailbox semantics. My current plan is that every actors gets its own mailbox associated at spawn and can get it injected to send messages. A separate hierarchy of dispatchers/routers is responsible for taking messages from an actors mailbox and delivering it to the destination mailbox. Regarding concurrency, each mailbox will determine how many messages it is willing to process in parallel and anytime an actor yields control its mailbox can use that "thread" to process another message. This should avoid deadlocks unless an actor uses blocking semantics to SendAndReceive and then they are just getting what they deserve 🙂

  3. Calculon: Building an actor framework says...

    […] Type-safe actor messaging approaches […]

Leave a comment