Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pub/sub pattern in Azure Service Fabric

I'm working on an Azure Service Fabric application here where I have certain actors that need to receive pings/hooks from other services on demand. The application is a sort of event distribution engine that is intended to work something like this:

  • An event router actor that can take an event in and then take care of distributing that event to all subscribers of that event type.
  • 0..N event subscription actors that somehow need to inform the router what type of events they wish to subscribe to and how they want them delivered (sync or async).
  • When the event router actor receives an event of the type MyEvent, it will identify what subscribers are listening and how they want the event delivered. For asynchronous delivery, a message will be popped in an Azure Service Bus topic. For synchronous delivery though, the router actor will invoke the subscription method on the subscribing actors directly, awaiting their response.

Most of this is fairly straight forward, but I'm not entirely sure how I'm going to pull off synchronous delivery of these events. I don't want the event router actor to in any way be aware of any internals in the actors subscribing to events - yet with the current ActorProxy implementations and similar, having access to the interfaces are required to invoke methods on other actors.

Say I subscribe to an event type, informing the event router that my address is fabric:/MyApp/MyEventSubscriberActor and that I want to subscribe to MyEvent. Is there any sensible way within the Service Fabric APIs I can programatically invoke a method on that actor without (for instance OnEventAsync(MyEvent ev) using the ActorProxy.Create<IMyEventSubscriberActor>() method? The source code of these APIs doesn't seem to be publicly available, so I have no direct way of checking how this is done under the hood.

like image 809
Trond Nordheim Avatar asked Jun 09 '15 13:06

Trond Nordheim


People also ask

What is pub/sub design pattern?

The Publish/Subscribe pattern, also known as pub/sub, is an architectural design pattern that provides a framework for exchanging messages between publishers and subscribers. This pattern involves the publisher and the subscriber relying on a message broker that relays messages from the publisher to the subscribers.

What is pub/sub model in Azure?

The Azure Web PubSub Service helps you build real-time messaging web applications using WebSockets and the publish-subscribe pattern easily. This real-time functionality allows publishing content updates between server and connected clients (for example a single page web application or mobile application).

What is a benefit of the pub/sub pattern?

Pub/Sub makes discovery of services easier, more natural and less error prone. Instead of maintaining a roster of peers that an application can send messages to, a publisher will simply post messages to a topic. Then, any interested party will subscribe its endpoint to the topic, and start receiving these messages.

What is the difference between pub/sub and message queue?

To broadcast a message, a component called a publisher simply pushes a message to the topic. Unlike message queues, which batch messages until they are retrieved, message topics transfer messages with no or very little queuing, and push them out immediately to all subscribers.


1 Answers

The event subscription actor can implement an event subscription interface that contains an "event-available" method. It can pass that interface to an "subscribe-to-event" method on the event router actor interface.

The event router actor interface can keep a reference to the subscription interface as part of its state. When the event of interest to the subscriber occurs, it can call the "event-available" method on the interface that it received and saved earlier. All of this can be done without explicitly creating an actor proxy to communicate with the event subscription actor (the actors serialization infrastructure does this under the hood).

Here's a very basic example that omits the event type, assumes just one subscriber etc., but should give you an idea of the technique.

Interfaces:

interface IEventRouter : IActor
{
    void Subscribe(IEventSubscriber subscriber);
}

interface IEventSubscriber : IActor
{
    void EventAvailable();
}

Event subscriber code:

class EventSubscriber : Actor, IEventSubscriber
{
    void SubscribeToEvent()
    {
        IEventRouter router = ActorProxy.Create<IEventRouter>("fabric:/MyApp/MyEventRouterActor");
        router.Subscribe(this);
    }

    public void EventAvailable()
    {
        // Process the event
    }
}

Event router code:

// Define actor state
[DataContract]
class RouterState
{
    [DataMember]
    public IEventSubscriber Subscriber;
}

// Define actor
class EventRouter : Actor<RouterState>, IEventRouter
{
    public void Subscribe(IEventSubscriber subscriber)
    {
        this.State.Subscriber = subscriber;
    }

    void OnEventAvailable()
    {
        this.State.Subscriber.EventAvailable();
    }
}       
like image 94
AbhishekRam-MSFT Avatar answered Sep 21 '22 14:09

AbhishekRam-MSFT