Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Service Fabric inter service communication

I currently have one Service Fabric application that is composed of multiple Services. What I'm trying to achieve is a Queuing mechanism so one Service can publish a message to a queue, and another Service can receive messages from the same queue.

The following doesn't work (for the Listener service, there is nothing to dequeue):

PublisherService:

protected override async Task RunAsync(CancellationToken cancellationToken)
{
    var myQueue = await StateManager.GetOrAddAsync<IReliableQueue<string>>("fooQueue");
    while (true)
    {
        cancellationToken.ThrowIfCancellationRequested();
        using (var tx = this.StateManager.CreateTransaction())
        {
            // Put some message in the queue
            await myQueue.EnqueueAsync(tx, "Foobar");

            await tx.CommitAsync();
        }

        await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
    }
}

ListenerService:

protected override async Task RunAsync(CancellationToken cancellationToken)
{
    var myQueue = await StateManager.GetOrAddAsync<IReliableQueue<string>>("fooQueue");
    while (true)
    {
        cancellationToken.ThrowIfCancellationRequested();
        using (var tx = this.StateManager.CreateTransaction())
        {
            var result = await myQueue.TryDequeueAsync(tx);

            if (result.HasValue)
            {
                ServiceEventSource.Current.ServiceMessage(this.Context, "New message receieved: {0}", result.Value.ToString());
            }

            await tx.CommitAsync();
        }

        await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
    }
}

It looks like the scope of a queue is limited to a single Service. This doesn't appear to be a limitation specified in the documentation.

So my questions are:

  • is this actually some undocumented limitation?
  • or is there something wrong in the code above?
  • how could I achieve the scenario above (one service adds messages to a queue, another service retrieves messages from the same queue)?

Obviously I could use an Azure Service Bus, but I can't for several reasons:

  • in my actual real-world scenario, I will have several queues (variable number) so it would require creating Service Bus Queues on demand (which is not exactly a fast operation)
  • adds a dependency to another Azure service (so increases the failure probability for the whole system)
  • costs more
  • more complex deployment
  • etc.
like image 476
ken2k Avatar asked Nov 25 '25 20:11

ken2k


2 Answers

ReliableQueues are local to a service yes, because its intent is to store state for that particular service. That state is replicated to other instances. It is like a normal System.Collections.Generic.Queue<T> in .Net.

For a low cost solution maybe you can use Azure Storage Queues. Yes, it adds a dependency but it has a high availability. It is a tradeoff that only you can decide to accept or not.

On the other hand, think out of the box:

Create a stateful service with multiple ReliableQueues and expose methods other services can call using stand remoting communication like:

class QueuingService
{
    void AddToQueue<T>(string queuename, T input) { .. }
    void DeQueue(string queuename) { .. }
}

This creates of course a dependency but it has all the safety mechanisms Service Fabric provides and does not cost you much. But then again, you are building a poor mans service bus/azure storage queue yourself.

About the docs, no it does not says so with many words that a reliable queue is tied to 1 service but it depends on how you interpret this

Service Fabric offers a stateful programming model available to .NET developers via Reliable Collections. Specifically, Service Fabric provides reliable dictionary and reliable queue classes. When you use these classes, your state (my interpretation: The state of the service) is partitioned (for scalability), replicated (for availability), and transacted within a partition (for ACID semantics).

like image 198
Peter Bons Avatar answered Nov 27 '25 11:11

Peter Bons


Check out the Priority Queue Service, which was created for this purpose.

like image 34
Todd Abel Avatar answered Nov 27 '25 12:11

Todd Abel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!